utils.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import dayjs from 'dayjs'
  2. import numeral from 'numeral'
  3. import Cookies from 'js-cookie'
  4. import { state as helpState } from './helpState'
  5. export const browser = () => {
  6. const u = navigator.userAgent
  7. // app = navigator.appVersion;
  8. return {
  9. trident: u.indexOf('Trident') > -1, //IE内核
  10. presto: u.indexOf('Presto') > -1, //opera内核
  11. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  12. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  13. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  14. ios: !!u.match(/Mac OS X/), //ios终端
  15. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  16. android: u.indexOf('COLEXIUAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  17. iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
  18. isApp:
  19. u.indexOf('COLEXIUAPPI') > -1 ||
  20. u.indexOf('COLEXIUAPPA') > -1 ||
  21. u.indexOf('Adr') > -1,
  22. iPad: u.indexOf('iPad') > -1, //是否iPad
  23. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  24. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  25. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  26. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  27. }
  28. }
  29. export const getRandomKey = () => {
  30. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000)
  31. return key
  32. }
  33. /**
  34. * 删除token
  35. */
  36. export const removeAuth = () => {
  37. Cookies.remove('token')
  38. }
  39. /**
  40. * 设置token
  41. * @param token
  42. * @returns {void}
  43. */
  44. export const setAuth = (token: any) => {
  45. // 7天过期
  46. Cookies.set('token', token, { expires: 7 })
  47. }
  48. /**
  49. * 获取token
  50. */
  51. export const getAuth = () => {
  52. let token = Cookies.get('token')
  53. token = token ? JSON.parse(token) : {}
  54. return token.token || null
  55. }
  56. /**
  57. * 获取登录用户类型
  58. * @returns {string}
  59. */
  60. export const getUserType = () => {
  61. let token = Cookies.get('token')
  62. token = token ? JSON.parse(token) : {}
  63. return token.userType || null
  64. }
  65. export const getWeekCh = (week: number, type = 0) => {
  66. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  67. const template2 = [
  68. '星期天',
  69. '星期一',
  70. '星期二',
  71. '星期三',
  72. '星期四',
  73. '星期五',
  74. '星期六'
  75. ]
  76. return type ? template2[week] : template[week]
  77. }
  78. export const formatterDate = (type: string, val: any) => {
  79. if (type === 'year') {
  80. return `${val}年`
  81. }
  82. if (type === 'month') {
  83. return `${val}月`
  84. }
  85. if (type === 'day') {
  86. return `${val}日`
  87. }
  88. if (type === 'hour') {
  89. return `${val}时`
  90. }
  91. if (type === 'minute') {
  92. return `${val}分`
  93. }
  94. return val
  95. }
  96. export const moneyFormat = (value: number) => {
  97. return numeral(value).format('0,0.00')
  98. }
  99. export const dateFormat = (
  100. value: string | Date,
  101. format = 'YYYY-MM-DD HH:mm:ss'
  102. ) => {
  103. return dayjs(value).format(format)
  104. }