utils.ts 3.5 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('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  17. iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
  18. isApp:
  19. u.indexOf('COLEXIUAPPI') > -1 ||
  20. u.indexOf('COLEXIUAPPA') > -1 ||
  21. u.indexOf('Adr') > -1,
  22. isTeacher: u.indexOf('COLEXIUTEACHER') > -1,
  23. isStudent: u.indexOf('COLEXIUSTUDENT') > -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. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  28. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  29. }
  30. }
  31. export const getRandomKey = () => {
  32. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  33. return key
  34. }
  35. /**
  36. * 删除token
  37. */
  38. export const removeAuth = () => {
  39. sessionStorage.removeItem('Authorization')
  40. }
  41. /**
  42. * 设置token
  43. * @param token
  44. * @returns {void}
  45. */
  46. export const setAuth = (token: any) => {
  47. sessionStorage.setItem('Authorization', token)
  48. }
  49. /**
  50. * 获取token
  51. */
  52. export const getAuth = () => {
  53. sessionStorage.getItem('Authorization')
  54. }
  55. /**
  56. * 开始加载
  57. */
  58. export const openLoading = () => {
  59. if (helpState.loadingCount === 0) {
  60. helpState.loadingCount++
  61. Toast.loading({
  62. message: '加载中...',
  63. forbidClick: true,
  64. loadingType: 'spinner',
  65. duration: 0
  66. })
  67. }
  68. }
  69. /**
  70. * 关闭加载
  71. */
  72. export const closeLoading = () => {
  73. // console.log(helpState.loadingCount, +new Date());
  74. if (helpState.loadingCount <= 0) return
  75. setTimeout(() => {
  76. helpState.loadingCount--
  77. if (helpState.loadingCount === 0) {
  78. Toast.clear()
  79. }
  80. }, 200)
  81. }
  82. export const getWeekCh = (week: number, type = 0) => {
  83. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  84. const template2 = [
  85. '星期天',
  86. '星期一',
  87. '星期二',
  88. '星期三',
  89. '星期四',
  90. '星期五',
  91. '星期六'
  92. ]
  93. return type ? template2[week] : template[week]
  94. }
  95. export const formatterDate = (type: string, val: any) => {
  96. if (type === 'year') {
  97. return `${val}年`
  98. }
  99. if (type === 'month') {
  100. return `${val}月`
  101. }
  102. if (type === 'day') {
  103. return `${val}日`
  104. }
  105. if (type === 'hour') {
  106. return `${val}时`
  107. }
  108. if (type === 'minute') {
  109. return `${val}分`
  110. }
  111. return val
  112. }
  113. export const numberFormat = (num: number, type?: string) => {
  114. if (type === 'percent') {
  115. return numeral(num).format('0.0%')
  116. }
  117. return numeral(num).format('0,0')
  118. }
  119. export const moneyFormat = (value: number, format = '0,0.00') => {
  120. return numeral(value).format(format)
  121. }
  122. export const dateFormat = (
  123. value: string | Date,
  124. format = 'YYYY-MM-DD HH:mm:ss'
  125. ) => {
  126. return dayjs(value).format(format)
  127. }