utils.ts 8.0 KB

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