utils.ts 4.2 KB

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