utils.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. export function checkPhone(phone: string) {
  115. const phoneRule =
  116. /^((13[0-9])|(14(0|[5-7]|9))|(15([0-3]|[5-9]))|(16(2|[5-7]))|(17[0-8])|(18[0-9])|(19([0-3]|[5-9])))\d{8}$/
  117. return phoneRule.test(phone)
  118. }
  119. /**
  120. * 删除token
  121. */
  122. export const removeAuth = () => {
  123. sessionStorage.removeItem('Authorization')
  124. }
  125. /**
  126. * 设置token
  127. * @param token
  128. * @returns {void}
  129. */
  130. export const setAuth = (token: any) => {
  131. sessionStorage.setItem('Authorization', token)
  132. }
  133. /**
  134. * 获取token
  135. */
  136. export const getAuth = () => {
  137. return sessionStorage.getItem('Authorization')
  138. }
  139. /**
  140. * 开始加载
  141. */
  142. export const openLoading = () => {
  143. if (helpState.loadingCount === 0) {
  144. helpState.loadingCount++
  145. Toast.loading({
  146. message: '加载中...',
  147. forbidClick: true,
  148. loadingType: 'spinner',
  149. duration: 0
  150. })
  151. }
  152. }
  153. /**
  154. * 关闭加载
  155. */
  156. export const closeLoading = () => {
  157. // console.log(helpState.loadingCount, +new Date());
  158. if (helpState.loadingCount <= 0) return
  159. setTimeout(() => {
  160. helpState.loadingCount--
  161. if (helpState.loadingCount === 0) {
  162. Toast.clear()
  163. }
  164. }, 200)
  165. }
  166. export const getWeekCh = (week: number, type = 0) => {
  167. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  168. const template2 = [
  169. '星期天',
  170. '星期一',
  171. '星期二',
  172. '星期三',
  173. '星期四',
  174. '星期五',
  175. '星期六'
  176. ]
  177. return type ? template2[week] : template[week]
  178. }
  179. export const formatterDate = (type: string, val: any) => {
  180. if (type === 'year') {
  181. return `${val}年`
  182. }
  183. if (type === 'month') {
  184. return `${val}月`
  185. }
  186. if (type === 'day') {
  187. return `${val}日`
  188. }
  189. if (type === 'hour') {
  190. return `${val}时`
  191. }
  192. if (type === 'minute') {
  193. return `${val}分`
  194. }
  195. return val
  196. }
  197. export const numberFormat = (num: number, type?: string) => {
  198. if (type === 'percent') {
  199. return numeral(num).format('0.0%')
  200. }
  201. return numeral(num).format('0,0')
  202. }
  203. export const moneyFormat = (value: number, format = '0,0.00') => {
  204. return numeral(value).format(format)
  205. }
  206. export const dateFormat = (
  207. value: string | Date,
  208. format = 'YYYY-MM-DD HH:mm:ss'
  209. ) => {
  210. return dayjs(value).format(format)
  211. }
  212. // 获取授权的code码
  213. export const getUrlCode = (name = 'code') => {
  214. let search: any = {}
  215. try {
  216. search = {
  217. ...qs.parse(location.search),
  218. ...qs.parse(location.hash.split('?')[1])
  219. }
  220. } catch (error) {
  221. //
  222. }
  223. return search[name]
  224. }
  225. export const getGradeCh = (grade: number) => {
  226. const template = [
  227. '一年级',
  228. '二年级',
  229. '三年级',
  230. '四年级',
  231. '五年级',
  232. '六年级',
  233. '七年级',
  234. '八年级',
  235. '九年级'
  236. ]
  237. return template[grade]
  238. }
  239. // 秒转分
  240. export const getSecondRPM = (second: number, type?: string) => {
  241. if (isNaN(second)) return '00:00'
  242. const mm = Math.floor(second / 60)
  243. .toString()
  244. .padStart(2, '0')
  245. const dd = Math.floor(second % 60)
  246. .toString()
  247. .padStart(2, '0')
  248. if (type === 'cn') {
  249. return mm + '分' + dd + '秒'
  250. } else {
  251. return mm + ':' + dd
  252. }
  253. }