utils.ts 3.8 KB

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