utils.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import dayjs from 'dayjs'
  2. import numeral from 'numeral'
  3. import { Toast } from 'vant'
  4. import { state as helpState } from './helpState'
  5. import qs from 'query-string'
  6. export const browser = () => {
  7. const u = navigator.userAgent
  8. // app = navigator.appVersion;
  9. return {
  10. trident: u.indexOf('Trident') > -1, //IE内核
  11. presto: u.indexOf('Presto') > -1, //opera内核
  12. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  13. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  14. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  15. ios: !!u.match(/Mac OS X/), //ios终端
  16. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  17. android: u.indexOf('ORCHESTRAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  18. iPhone: u.indexOf('ORCHESTRAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  19. isApp: u.indexOf('ORCHESTRAAPPI') > -1 || u.indexOf('ORCHESTRAAPPA') > -1,
  20. isTeacher: u.indexOf('ORCHESTRATEACHER') > -1,
  21. isStudent: u.indexOf('ORCHESTRASTUDENT') > -1,
  22. isSchool: u.indexOf('ORCHESTRASCHOOL') > -1,
  23. iPad: u.indexOf('iPad') > -1, //是否iPad
  24. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  25. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  26. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  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. // 获取授权的code码
  32. export const getUrlCode = (name = 'code') => {
  33. // 截取url中的code方法
  34. const url = location.search
  35. const theRequest: any = new Object()
  36. if (url.indexOf('?') != -1) {
  37. const str = url.substr(1)
  38. const strs = str.split('&')
  39. for (let i = 0; i < strs.length; i++) {
  40. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1]
  41. }
  42. }
  43. console.log(theRequest, 'theRequest')
  44. return theRequest[name]
  45. }
  46. export const getQuery = (name = 'code') => {
  47. let search: any = {}
  48. try {
  49. search = {
  50. ...qs.parse(location.search),
  51. ...qs.parse(location.hash.split('?')[1])
  52. }
  53. } catch (error) {
  54. //
  55. }
  56. return search[name]
  57. }
  58. export const getRandomKey = () => {
  59. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  60. return key
  61. }
  62. /**
  63. * 删除token
  64. */
  65. export const removeAuth = () => {
  66. sessionStorage.removeItem('Authorization')
  67. }
  68. /**
  69. * 设置token
  70. * @param token
  71. * @returns {void}
  72. */
  73. export const setAuth = (token: any) => {
  74. sessionStorage.setItem('Authorization', token)
  75. }
  76. /**
  77. * 获取token
  78. */
  79. export const getAuth = () => {
  80. sessionStorage.getItem('Authorization')
  81. }
  82. /**
  83. * 开始加载
  84. */
  85. export const openLoading = () => {
  86. if (helpState.loadingCount === 0) {
  87. helpState.loadingCount++
  88. Toast.loading({
  89. message: '加载中...',
  90. forbidClick: true,
  91. loadingType: 'spinner',
  92. duration: 0
  93. })
  94. }
  95. }
  96. /**
  97. * 关闭加载
  98. */
  99. export const closeLoading = () => {
  100. // console.log(helpState.loadingCount, +new Date());
  101. if (helpState.loadingCount <= 0) return
  102. setTimeout(() => {
  103. helpState.loadingCount--
  104. if (helpState.loadingCount === 0) {
  105. Toast.clear()
  106. }
  107. }, 200)
  108. }
  109. export const getWeekCh = (week: number, type = 0) => {
  110. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  111. const template2 = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  112. return type ? template2[week] : template[week]
  113. }
  114. export const numberFormat = (num: number, type?: string) => {
  115. if (type === 'percent') {
  116. return numeral(num).format('0.0%')
  117. }
  118. return numeral(num).format('0,0')
  119. }
  120. export const moneyFormat = (value: number, format = '0,0.00') => {
  121. return numeral(value).format(format)
  122. }
  123. export const dateFormat = (value: string | Date, format = 'YYYY-MM-DD HH:mm:ss') => {
  124. return dayjs(value).format(format)
  125. }
  126. // 秒转分
  127. export const getSecondRPM = (second: number, type?: string) => {
  128. if (isNaN(second)) return '00:00'
  129. const mm = Math.floor(second / 60)
  130. .toString()
  131. .padStart(2, '0')
  132. const dd = Math.floor(second % 60)
  133. .toString()
  134. .padStart(2, '0')
  135. if (type === 'cn') {
  136. return mm + '分' + dd + '秒'
  137. } else {
  138. return mm + ':' + dd
  139. }
  140. }
  141. /**
  142. * @description 格式化日期控件显示内容
  143. * @param type
  144. * @param option
  145. * @returns OBJECT
  146. */
  147. export const formatterDatePicker = (type: any, option: any) => {
  148. if (type === 'year') {
  149. option.text += '年'
  150. }
  151. if (type === 'month') {
  152. option.text += '月'
  153. }
  154. if (type === 'day') {
  155. option.text += '日'
  156. }
  157. return option
  158. }