utils.ts 5.3 KB

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