utils.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import dayjs from 'dayjs'
  2. import numeral from 'numeral'
  3. import Cookies from 'js-cookie'
  4. import { state as helpState } from './helpState'
  5. import mitt from '@/helpers/mitt'
  6. // import { platform } from 'os'
  7. import EventEmitter from 'eventemitter3';
  8. export const eventGlobal = new EventEmitter();
  9. export const browser = () => {
  10. const u = navigator.userAgent
  11. // app = navigator.appVersion;
  12. return {
  13. trident: u.indexOf('Trident') > -1, //IE内核
  14. presto: u.indexOf('Presto') > -1, //opera内核
  15. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  16. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  17. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  18. ios: !!u.match(/Mac OS X/), //ios终端
  19. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  20. android: u.indexOf('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  21. iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
  22. isApp:
  23. u.indexOf('COLEXIUAPPI') > -1 ||
  24. u.indexOf('COLEXIUAPPA') > -1 ||
  25. u.indexOf('Adr') > -1,
  26. iPad: u.indexOf('iPad') > -1, //是否iPad
  27. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  28. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  29. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  30. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  31. }
  32. }
  33. export const getRandomKey = () => {
  34. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  35. return key
  36. }
  37. /**
  38. * 设置用户信息
  39. */
  40. export const setUserInfo = (userInfo: any) => {
  41. Cookies.set('userInfo', userInfo, { expires: 7 })
  42. }
  43. /**
  44. * 删除token
  45. */
  46. export const removeAuth = () => {
  47. Cookies.remove('token')
  48. Cookies.remove('userInfo')
  49. }
  50. /**
  51. * 设置token
  52. * @param token
  53. * @returns {void}
  54. */
  55. export const setAuth = (token: any) => {
  56. // 7天过期
  57. Cookies.set('token', token, { expires: 7 })
  58. }
  59. /**
  60. * 获取用户信息
  61. */
  62. export const getUserInfo = () => {
  63. let userInfo = Cookies.get('userInfo')
  64. userInfo = userInfo ? JSON.parse(userInfo) : {}
  65. return userInfo || null
  66. }
  67. /**
  68. * 获取token
  69. */
  70. export const getAuth = () => {
  71. let token = Cookies.get('token')
  72. token = token ? JSON.parse(token) : {}
  73. return token.token || null
  74. }
  75. /**
  76. * 获取登录用户类型
  77. * @returns {string}
  78. */
  79. export const getUserType = () => {
  80. let token = Cookies.get('token')
  81. token = token ? JSON.parse(token) : {}
  82. return token.loginUserType || null
  83. }
  84. export const getWeekCh = (week: number, type = 0) => {
  85. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  86. const template2 = [
  87. '星期天',
  88. '星期一',
  89. '星期二',
  90. '星期三',
  91. '星期四',
  92. '星期五',
  93. '星期六'
  94. ]
  95. return type ? template2[week] : template[week]
  96. }
  97. export const formatterDate = (type: string, val: any) => {
  98. if (type === 'year') {
  99. return `${val}年`
  100. }
  101. if (type === 'month') {
  102. return `${val}月`
  103. }
  104. if (type === 'day') {
  105. return `${val}日`
  106. }
  107. if (type === 'hour') {
  108. return `${val}时`
  109. }
  110. if (type === 'minute') {
  111. return `${val}分`
  112. }
  113. return val
  114. }
  115. export const moneyFormat = (value: number) => {
  116. return numeral(value).format('0,0.00')
  117. }
  118. export const dateFormat = (
  119. value: string | Date,
  120. format = 'YYYY-MM-DD HH:mm:ss'
  121. ) => {
  122. return dayjs(value).format(format)
  123. }
  124. export function vaildTeachingUrl() {
  125. const url = window.location.href
  126. let returnUrl = ''
  127. if (/dev/.test(url)) {
  128. //线上
  129. returnUrl = 'https://dev.colexiu.com'
  130. } else if (/ponline/.test(url)) {
  131. returnUrl = 'https://ponline.colexiu.com'
  132. } else if (/online/.test(url)) {
  133. // dev 环境
  134. returnUrl = 'https://online.colexiu.com'
  135. } else if (/test/.test(url)) {
  136. // dev 环境
  137. returnUrl = 'https://dev.colexiu.com'
  138. } else {
  139. // 默认dev环境
  140. returnUrl = 'https://dev.colexiu.com'
  141. }
  142. return returnUrl
  143. }
  144. // 获取当前连接
  145. export const getBaseUrl = (platform?: string) => {
  146. const url = window.location.origin
  147. const pathname = platform || window.location.pathname
  148. return `${url}${pathname}`
  149. }
  150. export const getCodeBaseUrl = (platform?: string) => {
  151. let url = window.location.origin
  152. url = url.replace('www.', 'online.')
  153. const pathname = platform || window.location.pathname
  154. return `${url}${pathname}`
  155. }