utils.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import numeral from 'numeral';
  2. import dayjs from 'dayjs';
  3. export const browser = () => {
  4. const u = navigator.userAgent;
  5. return {
  6. trident: u.indexOf('Trident') > -1, //IE内核
  7. presto: u.indexOf('Presto') > -1, //opera内核
  8. webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
  9. gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
  10. mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
  11. ios: !!u.match(/Mac OS X/), //ios终端
  12. // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
  13. android: u.indexOf('COLEXIUSTUDENT') > -1 || u.indexOf('Adr') > -1, //android终端
  14. iPhone: u.indexOf('COLEXIUAPPI') > -1, //是否为iPhone或者QQHD浏览器
  15. isApp:
  16. u.indexOf('COLEXIUAPPI') > -1 ||
  17. u.indexOf('COLEXIUAPPA') > -1 ||
  18. u.indexOf('Adr') > -1,
  19. iPad: u.indexOf('iPad') > -1, //是否iPad
  20. webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部
  21. weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增)
  22. alipay: u.indexOf('AlipayClient') > -1, //是否支付宝
  23. huawei: !!u.match(/huawei/i) || !!u.match(/honor/i),
  24. xiaomi: !!u.match(/mi\s/i) || !!u.match(/redmi/i) || !!u.match(/mix/i)
  25. };
  26. };
  27. // 获取授权的code码
  28. export const getUrlCode = (name = 'code') => {
  29. // 截取url中的code方法
  30. const url = location.search;
  31. const theRequest: any = new Object();
  32. if (url.indexOf('?') != -1) {
  33. const str = url.substr(1);
  34. const strs = str.split('&');
  35. for (let i = 0; i < strs.length; i++) {
  36. theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
  37. }
  38. }
  39. console.log(theRequest, 'theRequest');
  40. return theRequest[name];
  41. };
  42. export const getRandomKey = () => {
  43. const key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000);
  44. return key;
  45. };
  46. export function checkPhone(phone: string) {
  47. const phoneRule =
  48. /^((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}$/;
  49. return phoneRule.test(phone);
  50. }
  51. /**
  52. * @description 格式化日期控件显示内容
  53. * @param type
  54. * @param option
  55. * @returns OBJECT
  56. */
  57. export const formatterDatePicker = (type: any, option: any) => {
  58. if (type === 'year') {
  59. option.text += '年';
  60. }
  61. if (type === 'month') {
  62. option.text += '月';
  63. }
  64. if (type === 'day') {
  65. option.text += '日';
  66. }
  67. return option;
  68. };
  69. /**
  70. * 数字转成汉字
  71. * @params num === 要转换的数字
  72. * @return 汉字
  73. * */
  74. export const toChinesNum = (num: any) => {
  75. const changeNum = [
  76. '零',
  77. '一',
  78. '二',
  79. '三',
  80. '四',
  81. '五',
  82. '六',
  83. '七',
  84. '八',
  85. '九'
  86. ];
  87. const unit = ['', '十', '百', '千', '万'];
  88. num = parseInt(num);
  89. const getWan = (temp: any) => {
  90. const strArr = temp.toString().split('').reverse();
  91. let newNum = '';
  92. const newArr: string[] = [];
  93. strArr.forEach((item: any, index: any) => {
  94. newArr.unshift(
  95. item === '0' ? changeNum[item] : changeNum[item] + unit[index]
  96. );
  97. });
  98. const numArr: number[] = [];
  99. newArr.forEach((m, n) => {
  100. if (m !== '零') numArr.push(n);
  101. });
  102. if (newArr.length > 1) {
  103. newArr.forEach((m, n) => {
  104. if (newArr[newArr.length - 1] === '零') {
  105. if (n <= numArr[numArr.length - 1]) {
  106. newNum += m;
  107. }
  108. } else {
  109. newNum += m;
  110. }
  111. });
  112. } else {
  113. newNum = newArr[0];
  114. }
  115. return newNum;
  116. };
  117. const overWan = Math.floor(num / 10000);
  118. let noWan: any = num % 10000;
  119. if (noWan.toString().length < 4) {
  120. noWan = '0' + noWan;
  121. }
  122. return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num);
  123. };
  124. // 秒转分
  125. export const getSecondRPM = (second: number, type?: string) => {
  126. if (isNaN(second)) return '00:00';
  127. const mm = Math.floor(second / 60)
  128. .toString()
  129. .padStart(2, '0');
  130. const dd = Math.floor(second % 60)
  131. .toString()
  132. .padStart(2, '0');
  133. if (type === 'cn') {
  134. return mm + '分' + dd + '秒';
  135. } else {
  136. return mm + ':' + dd;
  137. }
  138. };
  139. export const getWeekCh = (week: number, type = 0) => {
  140. const template = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  141. const template2 = [
  142. '星期天',
  143. '星期一',
  144. '星期二',
  145. '星期三',
  146. '星期四',
  147. '星期五',
  148. '星期六'
  149. ];
  150. return type ? template2[week] : template[week];
  151. };
  152. export const numberFormat = (num: number, type?: string) => {
  153. if (type === 'percent') {
  154. return numeral(num).format('0.0%');
  155. }
  156. return numeral(num).format('0,0');
  157. };
  158. export const moneyFormat = (value: number, format = '0,0.00') => {
  159. return numeral(value).format(format);
  160. };
  161. export const dateFormat = (
  162. value: string | Date,
  163. format = 'YYYY-MM-DD HH:mm:ss'
  164. ) => {
  165. return dayjs(value).format(format);
  166. };