utils.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. let instance: any
  10. if (u.indexOf('ORCHESTRASTUDENT') > -1) {
  11. instance =
  12. (window as any).ORCHESTRA ||
  13. (window as any).webkit?.messageHandlers?.ORCHESTRA
  14. } else {
  15. instance =
  16. (window as any).COLEXIU ||
  17. (window as any).webkit?.messageHandlers?.COLEXIU
  18. }
  19. return {
  20. trident: u.indexOf('Trident') > -1, //IE内核
  21. presto: u.indexOf('Presto') > -1, //opera内核
  22. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  23. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  24. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  25. ios: !!u.match(/Mac OS X/), //ios终端
  26. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  27. android: u.indexOf('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  28. iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
  29. isApp: instance ? true : false, // 临时处理
  30. // isApp:
  31. // u.indexOf('COLEXIUAPPI') > -1 ||
  32. // u.indexOf('COLEXIUAPPA') > -1 ||
  33. // u.indexOf('ORCHESTRASTUDENT') > -1 ||
  34. // u.indexOf('Adr') > -1,
  35. isTeacher: u.indexOf('COLEXIUTEACHER') > -1,
  36. isStudent: u.indexOf('COLEXIUSTUDENT') > -1,
  37. isOrchestraStudent: u.indexOf('ORCHESTRASTUDENT') > -1, // 判断是否是管乐团学生端
  38. orchestraAndroid: u.indexOf('ORCHESTRAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  39. orchestraIPhone: u.indexOf('ORCHESTRAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  40. iPad: u.indexOf('iPad') > -1, //是否iPad
  41. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  42. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  43. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  44. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  45. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  46. }
  47. }
  48. /**
  49. * @description 格式化日期控件显示内容
  50. * @param type
  51. * @param option
  52. * @returns OBJECT
  53. */
  54. export const formatterDatePicker = (type: any, option: any) => {
  55. if (type === 'year') {
  56. option.text += '年'
  57. }
  58. if (type === 'month') {
  59. option.text += '月'
  60. }
  61. if (type === 'day') {
  62. option.text += '日'
  63. }
  64. return option
  65. }
  66. /**
  67. * 数字转成汉字
  68. * @params num === 要转换的数字
  69. * @return 汉字
  70. * */
  71. export const toChinesNum = (num: any) => {
  72. const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  73. const unit = ['', '十', '百', '千', '万']
  74. num = parseInt(num)
  75. const getWan = (temp: any) => {
  76. const strArr = temp.toString().split('').reverse()
  77. let newNum = ''
  78. const newArr: string[] = []
  79. strArr.forEach((item: any, index: any) => {
  80. newArr.unshift(
  81. item === '0' ? changeNum[item] : changeNum[item] + unit[index]
  82. )
  83. })
  84. const numArr: number[] = []
  85. newArr.forEach((m, n) => {
  86. if (m !== '零') numArr.push(n)
  87. })
  88. if (newArr.length > 1) {
  89. newArr.forEach((m, n) => {
  90. if (newArr[newArr.length - 1] === '零') {
  91. if (n <= numArr[numArr.length - 1]) {
  92. newNum += m
  93. }
  94. } else {
  95. newNum += m
  96. }
  97. })
  98. } else {
  99. newNum = newArr[0]
  100. }
  101. return newNum
  102. }
  103. const overWan = Math.floor(num / 10000)
  104. let noWan: any = num % 10000
  105. if (noWan.toString().length < 4) {
  106. noWan = '0' + noWan
  107. }
  108. return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
  109. }
  110. export const getRandomKey = () => {
  111. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  112. return key
  113. }
  114. /**
  115. * 删除token
  116. */
  117. export const removeAuth = () => {
  118. sessionStorage.removeItem('Authorization')
  119. }
  120. /**
  121. * 设置token
  122. * @param token
  123. * @returns {void}
  124. */
  125. export const setAuth = (token: any) => {
  126. sessionStorage.setItem('Authorization', token)
  127. }
  128. /**
  129. * 获取token
  130. */
  131. export const getAuth = () => {
  132. sessionStorage.getItem('Authorization')
  133. }
  134. /**
  135. * 开始加载
  136. */
  137. export const openLoading = () => {
  138. if (helpState.loadingCount === 0) {
  139. helpState.loadingCount++
  140. Toast.loading({
  141. message: '加载中...',
  142. forbidClick: true,
  143. loadingType: 'spinner',
  144. duration: 0
  145. })
  146. }
  147. }
  148. /**
  149. * 关闭加载
  150. */
  151. export const closeLoading = () => {
  152. // console.log(helpState.loadingCount, +new Date());
  153. if (helpState.loadingCount <= 0) return
  154. setTimeout(() => {
  155. helpState.loadingCount--
  156. if (helpState.loadingCount === 0) {
  157. Toast.clear()
  158. }
  159. }, 200)
  160. }
  161. export const getWeekCh = (week: number, type = 0) => {
  162. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  163. const template2 = [
  164. '星期天',
  165. '星期一',
  166. '星期二',
  167. '星期三',
  168. '星期四',
  169. '星期五',
  170. '星期六'
  171. ]
  172. return type ? template2[week] : template[week]
  173. }
  174. export const formatterDate = (type: string, val: any) => {
  175. if (type === 'year') {
  176. return `${val}年`
  177. }
  178. if (type === 'month') {
  179. return `${val}月`
  180. }
  181. if (type === 'day') {
  182. return `${val}日`
  183. }
  184. if (type === 'hour') {
  185. return `${val}时`
  186. }
  187. if (type === 'minute') {
  188. return `${val}分`
  189. }
  190. return val
  191. }
  192. export const numberFormat = (num: number, type?: string) => {
  193. if (type === 'percent') {
  194. return numeral(num).format('0.0%')
  195. }
  196. return numeral(num).format('0,0')
  197. }
  198. export const moneyFormat = (value: number, format = '0,0.00') => {
  199. return numeral(value).format(format)
  200. }
  201. export const dateFormat = (
  202. value: string | Date,
  203. format = 'YYYY-MM-DD HH:mm:ss'
  204. ) => {
  205. return dayjs(value).format(format)
  206. }
  207. // 获取授权的code码
  208. export const getUrlCode = (name = 'code') => {
  209. let search: any = {}
  210. try {
  211. search = {
  212. ...qs.parse(location.search),
  213. ...qs.parse(location.hash.split('?')[1])
  214. }
  215. } catch (error) {
  216. //
  217. }
  218. return search[name]
  219. }