import dayjs from 'dayjs' import numeral from 'numeral' import { Toast } from 'vant' import { state as helpState } from './helpState' import qs from 'query-string' export const browser = () => { const u = navigator.userAgent // app = navigator.appVersion; const isAndroid = /(?:Android)/.test(u); const isFireFox = /(?:Firefox)/.test(u); function isIpadFun() { const ua = window.navigator.userAgent; let IsIPad = false; if (/ipad/i.test(ua)) { IsIPad = true; } // iPad from IOS13 const macApp = ua.match(/Macintosh/i) != null; if (macApp) { // need to distinguish between Macbook and iPad const canvas = document.createElement('canvas'); if (canvas != null) { const context: any = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (context) { const info = context.getExtension('WEBGL_debug_renderer_info'); if (info) { const renderer = context.getParameter(info.UNMASKED_RENDERER_WEBGL); if (renderer.indexOf('Apple') != -1) IsIPad = true; } } } } return IsIPad; } let instance: any if (u.indexOf('ORCHESTRASTUDENT') > -1) { instance = (window as any).ORCHESTRA || (window as any).webkit?.messageHandlers?.ORCHESTRA } else { instance = (window as any).COLEXIU || (window as any).webkit?.messageHandlers?.COLEXIU } return { trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 ios: !!u.match(/Mac OS X/), //ios终端 // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端 iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器 isApp: instance ? true : false, // 临时处理 // isApp: // u.indexOf('COLEXIUAPPI') > -1 || // u.indexOf('COLEXIUAPPA') > -1 || // u.indexOf('ORCHESTRASTUDENT') > -1 || // u.indexOf('Adr') > -1, isTablet: /(?:iPad|PlayBook)/.test(u) || (isAndroid && !/(?:Mobile)/.test(u)) || (isFireFox && /(?:Tablet)/.test(u)) || isIpadFun(), isTeacher: u.indexOf('COLEXIUTEACHER') > -1, isStudent: u.indexOf('COLEXIUSTUDENT') > -1, isOrchestraStudent: u.indexOf('ORCHESTRASTUDENT') > -1, // 判断是否是管乐团学生端 orchestraAndroid: u.indexOf('ORCHESTRAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端 orchestraIPhone: u.indexOf('ORCHESTRAAPPI') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增) alipay: u.indexOf('AlipayClient') > -1, //是否支付宝 huawei: !!u.match(/huawei/i) || !!u.match(/honor/i) || !!u.match(/HarmonyOS/i), xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i) } } export const getHttpOrigin = () => { return window.location.origin } /** * @description 格式化日期控件显示内容 * @param type * @param option * @returns OBJECT */ export const formatterDatePicker = (type: any, option: any) => { if (type === 'year') { option.text += '年' } if (type === 'month') { option.text += '月' } if (type === 'day') { option.text += '日' } return option } /** * 数字转成汉字 * @params num === 要转换的数字 * @return 汉字 * */ export const toChinesNum = (num: any) => { const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] const unit = ['', '十', '百', '千', '万'] num = parseInt(num) const getWan = (temp: any) => { const strArr = temp.toString().split('').reverse() let newNum = '' const newArr: string[] = [] strArr.forEach((item: any, index: any) => { newArr.unshift( item === '0' ? changeNum[item] : changeNum[item] + unit[index] ) }) const numArr: number[] = [] newArr.forEach((m, n) => { if (m !== '零') numArr.push(n) }) if (newArr.length > 1) { newArr.forEach((m, n) => { if (newArr[newArr.length - 1] === '零') { if (n <= numArr[numArr.length - 1]) { newNum += m } } else { newNum += m } }) } else { newNum = newArr[0] } return newNum } const overWan = Math.floor(num / 10000) let noWan: any = num % 10000 if (noWan.toString().length < 4) { noWan = '0' + noWan } return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num) } export const getRandomKey = () => { const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000) return key } export function checkPhone(phone: string) { const phoneRule = /^((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}$/ return phoneRule.test(phone) } /** * 删除token */ export const removeAuth = () => { sessionStorage.removeItem('Authorization') } /** * 设置token * @param token * @returns {void} */ export const setAuth = (token: any) => { sessionStorage.setItem('Authorization', token) } /** * 获取token */ export const getAuth = () => { return sessionStorage.getItem('Authorization') } /** * 开始加载 */ export const openLoading = () => { if (helpState.loadingCount === 0) { helpState.loadingCount++ Toast.loading({ message: '加载中...', forbidClick: true, loadingType: 'spinner', duration: 0 }) } } /** * 关闭加载 */ export const closeLoading = () => { // console.log(helpState.loadingCount, +new Date()); if (helpState.loadingCount <= 0) return setTimeout(() => { helpState.loadingCount-- if (helpState.loadingCount === 0) { Toast.clear() } }, 200) } export const getWeekCh = (week: number, type = 0) => { const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] const template2 = [ '星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六' ] return type ? template2[week] : template[week] } export const formatterDate = (type: string, val: any) => { if (type === 'year') { return `${val}年` } if (type === 'month') { return `${val}月` } if (type === 'day') { return `${val}日` } if (type === 'hour') { return `${val}时` } if (type === 'minute') { return `${val}分` } return val } export const numberFormat = (num: number, type?: string) => { if (type === 'percent') { return numeral(num).format('0.0%') } return numeral(num).format('0,0') } export const moneyFormat = (value: number, format = '0,0.00') => { return numeral(value).format(format) } export const dateFormat = ( value: string | Date, format = 'YYYY-MM-DD HH:mm:ss' ) => { return dayjs(value).format(format) } // 获取授权的code码 export const getUrlCode = (name = 'code') => { let search: any = {} try { search = { ...qs.parse(location.search), ...qs.parse(location.hash.split('?')[1]) } } catch (error) { // } return search[name] } export const getGradeCh = (grade: number) => { const template = [ '一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '七年级', '八年级', '九年级' ] return template[grade] } // 秒转分 export const getSecondRPM = (second: number, type?: string) => { if (isNaN(second)) return '00:00' const mm = Math.floor(second / 60) .toString() .padStart(2, '0') const dd = Math.floor(second % 60) .toString() .padStart(2, '0') if (type === 'cn') { return mm + '分' + dd + '秒' } else { return mm + ':' + dd } }