utils.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import dayjs from 'dayjs'
  2. import numeral from 'numeral'
  3. import { Toast } from 'vant'
  4. import { state as helpState } from './helpState'
  5. export const browser = () => {
  6. const u = navigator.userAgent
  7. // app = navigator.appVersion;
  8. return {
  9. trident: u.indexOf('Trident') > -1, //IE内核
  10. presto: u.indexOf('Presto') > -1, //opera内核
  11. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  12. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  13. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  14. ios: !!u.match(/Mac OS X/), //ios终端
  15. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  16. android: u.indexOf('ORCHESTRAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  17. iPhone: u.indexOf('ORCHESTRAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  18. isApp:
  19. u.indexOf('ORCHESTRAAPPI') > -1 ||
  20. u.indexOf('ORCHESTRAAPPA') > -1,
  21. isTeacher: u.indexOf('ORCHESTRATEACHER') > -1,
  22. isStudent: u.indexOf('ORCHESTRASTUDENT') > -1,
  23. isSchool: u.indexOf('ORCHESTRASCHOOL') > -1,
  24. iPad: u.indexOf('iPad') > -1, //是否iPad
  25. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  26. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  27. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  28. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  29. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  30. }
  31. }
  32. // 获取授权的code码
  33. export const getUrlCode = (name = 'code') => {
  34. // 截取url中的code方法
  35. const url = location.search;
  36. const theRequest: any = new Object();
  37. if (url.indexOf("?") != -1) {
  38. const str = url.substr(1);
  39. const strs = str.split("&");
  40. for (let i = 0; i < strs.length; i++) {
  41. theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
  42. }
  43. }
  44. console.log(theRequest, 'theRequest');
  45. return theRequest[name];
  46. };
  47. export const getRandomKey = () => {
  48. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  49. return key
  50. }
  51. /**
  52. * 删除token
  53. */
  54. export const removeAuth = () => {
  55. sessionStorage.removeItem('Authorization')
  56. }
  57. /**
  58. * 设置token
  59. * @param token
  60. * @returns {void}
  61. */
  62. export const setAuth = (token: any) => {
  63. sessionStorage.setItem('Authorization', token)
  64. }
  65. /**
  66. * 获取token
  67. */
  68. export const getAuth = () => {
  69. sessionStorage.getItem('Authorization')
  70. }
  71. /**
  72. * 开始加载
  73. */
  74. export const openLoading = () => {
  75. if (helpState.loadingCount === 0) {
  76. helpState.loadingCount++
  77. Toast.loading({
  78. message: '加载中...',
  79. forbidClick: true,
  80. loadingType: 'spinner',
  81. duration: 0
  82. })
  83. }
  84. }
  85. /**
  86. * 关闭加载
  87. */
  88. export const closeLoading = () => {
  89. // console.log(helpState.loadingCount, +new Date());
  90. if (helpState.loadingCount <= 0) return
  91. setTimeout(() => {
  92. helpState.loadingCount--
  93. if (helpState.loadingCount === 0) {
  94. Toast.clear()
  95. }
  96. }, 200)
  97. }
  98. export const getWeekCh = (week: number, type = 0) => {
  99. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  100. const template2 = [
  101. '星期天',
  102. '星期一',
  103. '星期二',
  104. '星期三',
  105. '星期四',
  106. '星期五',
  107. '星期六'
  108. ]
  109. return type ? template2[week] : template[week]
  110. }
  111. export const numberFormat = (num: number, type?: string) => {
  112. if (type === 'percent') {
  113. return numeral(num).format('0.0%')
  114. }
  115. return numeral(num).format('0,0')
  116. }
  117. export const moneyFormat = (value: number, format = '0,0.00') => {
  118. return numeral(value).format(format)
  119. }
  120. export const dateFormat = (
  121. value: string | Date,
  122. format = 'YYYY-MM-DD HH:mm:ss'
  123. ) => {
  124. return dayjs(value).format(format)
  125. }