utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. export const browser = () => {
  2. const u = navigator.userAgent;
  3. return {
  4. trident: u.indexOf('Trident') > -1, //IE内核
  5. presto: u.indexOf('Presto') > -1, //opera内核
  6. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  7. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  8. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  9. ios: !!u.match(/Mac OS X/), //ios终端
  10. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  11. android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端
  12. iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器
  13. isApp:
  14. u.indexOf('DAYAAPPI') > -1 ||
  15. u.indexOf('DAYAAPPA') > -1 ||
  16. u.indexOf('Adr') > -1,
  17. iPad: u.indexOf('iPad') > -1, //是否iPad
  18. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  19. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  20. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  21. huawei:
  22. !!u.match(/huawei/i) || !!u.match(/honor/i) || !!u.match(/HarmonyOS/i),
  23. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  24. };
  25. };
  26. export const getRandomKey = () => {
  27. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000);
  28. return key;
  29. };
  30. /**
  31. * 删除token
  32. */
  33. export const removeAuth = () => {
  34. sessionStorage.removeItem('Authorization');
  35. };
  36. /**
  37. * 设置token
  38. * @param token
  39. * @returns {void}
  40. */
  41. export const setAuth = (token: any) => {
  42. sessionStorage.setItem('Authorization', token);
  43. };
  44. /**
  45. * 获取token
  46. */
  47. export const getAuth = () => {
  48. sessionStorage.getItem('Authorization');
  49. };
  50. export function checkPhone(phone: string) {
  51. const phoneRule =
  52. /^((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}$/;
  53. return phoneRule.test(phone);
  54. }
  55. /**
  56. * @description 格式化日期控件显示内容
  57. * @param type
  58. * @param option
  59. * @returns OBJECT
  60. */
  61. export const formatterDatePicker = (type: any, option: any) => {
  62. if (type === 'year') {
  63. option.text += '年';
  64. }
  65. if (type === 'month') {
  66. option.text += '月';
  67. }
  68. if (type === 'day') {
  69. option.text += '日';
  70. }
  71. return option;
  72. };
  73. /**
  74. * 数字转成汉字
  75. * @params num === 要转换的数字
  76. * @return 汉字
  77. * */
  78. export const toChinesNum = (num: any) => {
  79. const changeNum = [
  80. '零',
  81. '一',
  82. '二',
  83. '三',
  84. '四',
  85. '五',
  86. '六',
  87. '七',
  88. '八',
  89. '九'
  90. ];
  91. const unit = ['', '十', '百', '千', '万'];
  92. num = parseInt(num);
  93. const getWan = (temp: any) => {
  94. const strArr = temp.toString().split('').reverse();
  95. let newNum = '';
  96. const newArr: string[] = [];
  97. strArr.forEach((item: any, index: any) => {
  98. newArr.unshift(
  99. item === '0' ? changeNum[item] : changeNum[item] + unit[index]
  100. );
  101. });
  102. const numArr: number[] = [];
  103. newArr.forEach((m, n) => {
  104. if (m !== '零') numArr.push(n);
  105. });
  106. if (newArr.length > 1) {
  107. newArr.forEach((m, n) => {
  108. if (newArr[newArr.length - 1] === '零') {
  109. if (n <= numArr[numArr.length - 1]) {
  110. newNum += m;
  111. }
  112. } else {
  113. newNum += m;
  114. }
  115. });
  116. } else {
  117. newNum = newArr[0];
  118. }
  119. return newNum;
  120. };
  121. const overWan = Math.floor(num / 10000);
  122. let noWan: any = num % 10000;
  123. if (noWan.toString().length < 4) {
  124. noWan = '0' + noWan;
  125. }
  126. return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num);
  127. };
  128. // 教务地址
  129. export function vaildTeachingUrl() {
  130. let url = window.location.hostname;
  131. let returnUrl = '';
  132. if (/dev/.test(url)) {
  133. // dev 环境
  134. returnUrl = 'http://dev.gym.lexiaoya.cn';
  135. } else if (/test/.test(url)) {
  136. // dev 环境
  137. returnUrl = 'http://test.gym.lexiaoya.cn';
  138. } else {
  139. // 默认线上环境
  140. returnUrl = 'https://gym.lexiaoya.cn';
  141. }
  142. return returnUrl;
  143. }