utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. import React from 'react';
  2. import moment from 'moment';
  3. import { Badge, Avatar } from 'antd';
  4. import { Hotax } from './config';
  5. const ProvinceCodes = {
  6. 11: '北京',
  7. 12: '天津',
  8. 13: '河北省',
  9. 14: '山西省',
  10. 15: '内蒙古自治区',
  11. 21: '辽宁省',
  12. 22: '吉林省',
  13. 23: '黑龙江省',
  14. 31: '上海',
  15. 32: '江苏省',
  16. 33: '浙江省',
  17. 34: '安徽省',
  18. 35: '福建省',
  19. 36: '江西省',
  20. 37: '山东省',
  21. 41: '河南省',
  22. 42: '湖北省',
  23. 43: '湖南省',
  24. 44: '广东省',
  25. 45: '广西壮族自治区',
  26. 46: '海南省',
  27. 50: '重庆',
  28. 51: '四川省',
  29. 52: '贵州省',
  30. 53: '云南省',
  31. 54: '西藏自治区',
  32. 61: '陕西省',
  33. 62: '甘肃省',
  34. 63: '青海省',
  35. 64: '宁夏回族自治区',
  36. 65: '新疆维吾尔自治区',
  37. 71: '台湾',
  38. 81: '香港特别行政区',
  39. 82: '澳门特别行政区',
  40. 99: '海外',
  41. // 43湖南校区编号超过99,省份代码顺延为90
  42. 90: '湖南省',
  43. };
  44. export function fixedZero(val) {
  45. return val * 1 < 10 ? `0${val}` : val;
  46. }
  47. export function getTimeDistance(type) {
  48. const now = new Date();
  49. const oneDay = 1000 * 60 * 60 * 24;
  50. if (type === 'today') {
  51. now.setHours(0);
  52. now.setMinutes(0);
  53. now.setSeconds(0);
  54. return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  55. }
  56. if (type === 'week') {
  57. let day = now.getDay();
  58. now.setHours(0);
  59. now.setMinutes(0);
  60. now.setSeconds(0);
  61. if (day === 0) {
  62. day = 6;
  63. } else {
  64. day -= 1;
  65. }
  66. const beginTime = now.getTime() - (day * oneDay);
  67. return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))];
  68. }
  69. if (type === 'month') {
  70. const year = now.getFullYear();
  71. const month = now.getMonth();
  72. const nextDate = moment(now).add(1, 'months');
  73. const nextYear = nextDate.year();
  74. const nextMonth = nextDate.month();
  75. return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000)];
  76. }
  77. if (type === 'year') {
  78. const year = now.getFullYear();
  79. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  80. }
  81. }
  82. export function getPlainNode(nodeList, parentPath = '') {
  83. const arr = [];
  84. nodeList.forEach((node) => {
  85. const item = node;
  86. item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
  87. item.exact = true;
  88. if (item.children && !item.component) {
  89. arr.push(...getPlainNode(item.children, item.path));
  90. } else {
  91. if (item.children && item.component) {
  92. item.exact = false;
  93. }
  94. arr.push(item);
  95. }
  96. });
  97. return arr;
  98. }
  99. export function digitUppercase(n) {
  100. const fraction = ['角', '分'];
  101. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  102. const unit = [
  103. ['元', '万', '亿'],
  104. ['', '拾', '佰', '仟'],
  105. ];
  106. let num = Math.abs(n);
  107. let s = '';
  108. fraction.forEach((item, index) => {
  109. s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/零./, '');
  110. });
  111. s = s || '整';
  112. num = Math.floor(num);
  113. for (let i = 0; i < unit[0].length && num > 0; i += 1) {
  114. let p = '';
  115. for (let j = 0; j < unit[1].length && num > 0; j += 1) {
  116. p = digit[num % 10] + unit[1][j] + p;
  117. num = Math.floor(num / 10);
  118. }
  119. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
  120. }
  121. return s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
  122. }
  123. function getRelation(str1, str2) {
  124. if (str1 === str2) {
  125. console.warn('Two path are equal!'); // eslint-disable-line
  126. }
  127. const arr1 = str1.split('/');
  128. const arr2 = str2.split('/');
  129. if (arr2.every((item, index) => item === arr1[index])) {
  130. return 1;
  131. } else if (arr1.every((item, index) => item === arr2[index])) {
  132. return 2;
  133. }
  134. return 3;
  135. }
  136. function getRenderArr(routes) {
  137. let renderArr = [];
  138. renderArr.push(routes[0]);
  139. for (let i = 1; i < routes.length; i += 1) {
  140. let isAdd = false;
  141. // 是否包含
  142. isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
  143. // 去重
  144. renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
  145. if (isAdd) {
  146. renderArr.push(routes[i]);
  147. }
  148. }
  149. return renderArr;
  150. }
  151. /**
  152. * Get router routing configuration
  153. * { path:{name,...param}}=>Array<{name,path ...param}>
  154. * @param {string} path
  155. * @param {routerData} routerData
  156. */
  157. export function getRoutes(path, routerData) {
  158. let routes = Object.keys(routerData).filter(routePath =>
  159. routePath.indexOf(path) === 0 && routePath !== path);
  160. // Replace path to '' eg. path='user' /user/name => name
  161. routes = routes.map(item => item.replace(path, ''));
  162. // Get the route to be rendered to remove the deep rendering
  163. const renderArr = getRenderArr(routes);
  164. // Conversion and stitching parameters
  165. const renderRoutes = renderArr.map((item) => {
  166. const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
  167. return {
  168. ...routerData[`${path}${item}`],
  169. key: `${path}${item}`,
  170. path: `${path}${item}`,
  171. exact,
  172. };
  173. });
  174. return renderRoutes;
  175. }
  176. /* eslint no-useless-escape:0 */
  177. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g;
  178. export function isUrl(path) {
  179. return reg.test(path);
  180. }
  181. export function addRowKey(data) {
  182. if (!data) {
  183. return [];
  184. }
  185. return data.map((item) => {
  186. return { key: item.id, ...item };
  187. });
  188. }
  189. export function renderCategory(domain) {
  190. switch (domain) {
  191. case Hotax.DOMAIN_CP:
  192. return '供应商';
  193. case Hotax.DOMAIN_LJ:
  194. return '平台方';
  195. case Hotax.DOMAIN_PJ:
  196. return '渠道商';
  197. default:
  198. return '';
  199. }
  200. }
  201. export function renderStatus(status, delText, normalText) {
  202. if (status === Hotax.STATUS_NORMAL) {
  203. return (
  204. <Badge status="success" text={normalText || '正常'} />
  205. );
  206. } else {
  207. return (
  208. <Badge status="error" text={delText || '删除'} />
  209. );
  210. }
  211. }
  212. export function statusCodeToName(status) {
  213. if (status === Hotax.STATUS_NORMAL) {
  214. return '正常';
  215. } else if (status === Hotax.STATUS_DELETE) {
  216. return '已删除';
  217. } else {
  218. return '';
  219. }
  220. }
  221. export function renderBindStatus(status, delText, normalText) {
  222. if (status === Hotax.ACCOUNT_BINDING) {
  223. return (
  224. <Badge status="warning" text={normalText || '已绑定'} />
  225. );
  226. } else {
  227. return (
  228. <Badge status="success" text={delText || '未绑定'} />
  229. );
  230. }
  231. }
  232. export function renderVideoQuality(quality) {
  233. if (quality === 'high') {
  234. return '高清';
  235. } else {
  236. return '标清';
  237. }
  238. }
  239. export function renderAvatar(avatar, name) {
  240. const colorList = [
  241. '#ff4d4f', // red-5
  242. '#ff7a45', // volcano-5
  243. '#ffa940', // orange-5
  244. '#ffc53d', // gold-5
  245. '#ffec3d', // yellow-5
  246. '#bae637', // lime-5
  247. '#73d13d', // green-5
  248. '#36cfc9', // cyan-5
  249. '#40a9ff', // blue-5
  250. '#597ef7', // geekblue-5
  251. '#9254de', // purple-5
  252. '#f759ab', // magenta-5
  253. ];
  254. return (
  255. <Avatar
  256. src={avatar}
  257. style={{
  258. backgroundColor: colorList[Math.floor(Math.random() * (colorList.length - 1))],
  259. verticalAlign: 'middle',
  260. }}
  261. >
  262. {name[0]}
  263. </Avatar>
  264. );
  265. }
  266. export function renderGender(gender) {
  267. if (gender === 'MALE') {
  268. return '男';
  269. } else if (gender === 'FEMALE') {
  270. return '女';
  271. } else {
  272. return '未知';
  273. }
  274. }
  275. export function genAbsolutePicUrl(path) {
  276. return `${Hotax.OSS_HOST}/${path}`;
  277. }
  278. export function statusToBool(status) {
  279. if (status === Hotax.STATUS_NORMAL) {
  280. return true;
  281. } else if (status === Hotax.STATUS_DELETE) {
  282. return false;
  283. } else {
  284. return true;
  285. }
  286. }
  287. export function boolToStatus(bool) {
  288. if (bool) {
  289. return Hotax.STATUS_NORMAL;
  290. } else {
  291. return Hotax.STATUS_DELETE;
  292. }
  293. }
  294. export function renderProductType(type) {
  295. if (type === Hotax.PRODUCT_COURSE) {
  296. return '课程';
  297. } else if (type === Hotax.PRODUCT_SUPPORT) {
  298. return '配套';
  299. } else if (type === Hotax.PRODUCT_PACKAGE) {
  300. return '套餐包';
  301. } else {
  302. return '';
  303. }
  304. }
  305. export function toDecimal2(x) {
  306. let f = parseFloat(x);
  307. if (isNaN(f)) {
  308. return false;
  309. }
  310. f = Math.round(x * 100) / 100;
  311. let s = f.toString();
  312. let rs = s.indexOf('.');
  313. if (rs < 0) {
  314. rs = s.length;
  315. s += '.';
  316. }
  317. while (s.length <= rs + 2) {
  318. s += '0';
  319. }
  320. return s;
  321. }
  322. export function provinceCodeToName(pcode) {
  323. return ProvinceCodes[pcode];
  324. }
  325. export function provinceNameToCode(pname) {
  326. const match = Object.keys(ProvinceCodes).filter((code) => {
  327. if (ProvinceCodes[code] === pname) {
  328. return true;
  329. }
  330. return false;
  331. });
  332. return match[0];
  333. }
  334. export function checkProductType(typeStr) {
  335. switch (typeStr) {
  336. case Hotax.PRODUCT_COURSE:
  337. return '课程';
  338. case Hotax.PRODUCT_SUPPORT:
  339. return '配套';
  340. case Hotax.PRODUCT_TRAINING:
  341. return '师训';
  342. case Hotax.PRODUCT_PACKAGE:
  343. return '套餐包';
  344. default:
  345. return '';
  346. }
  347. }
  348. export function renderOrderStatus(status, isPlainText) {
  349. const map = {
  350. [Hotax.ORDER_UNPAID]: {
  351. name: '未支付',
  352. type: 'default',
  353. },
  354. [Hotax.ORDER_CANCEL]: {
  355. name: '已作废',
  356. type: 'error',
  357. },
  358. [Hotax.ORDER_PAYOK]: {
  359. name: '已支付',
  360. type: 'processing',
  361. },
  362. [Hotax.ORDER_COMPLETE]: {
  363. name: '已完成',
  364. type: 'success',
  365. },
  366. [Hotax.ORDER_FORSEND]: {
  367. name: '待发货',
  368. type: 'processing',
  369. },
  370. [Hotax.ORDER_SENT]: {
  371. name: '已发货',
  372. type: 'processing',
  373. },
  374. [Hotax.ORDER_RECEIVED]: {
  375. name: '已收货',
  376. type: 'warning',
  377. },
  378. [Hotax.ORDER_REFUND]: {
  379. name: '退款中',
  380. type: 'warning',
  381. },
  382. };
  383. const obj = map[status] || { name: '未定义', type: 'error' };
  384. if (isPlainText) {
  385. return obj.name;
  386. } else {
  387. return <Badge status={obj.type} text={obj.name} />;
  388. }
  389. }
  390. export function renderOrderSplitStatus(status) {
  391. switch (status) {
  392. case Hotax.ORDER_NOSPLIT:
  393. return '未拆单';
  394. case Hotax.ORDER_UNSPLIT:
  395. return '无需拆单';
  396. case Hotax.ORDER_SPLITED:
  397. return '已拆单';
  398. default:
  399. return '';
  400. }
  401. }
  402. export function renderGoodsType(status) {
  403. switch (status) {
  404. case Hotax.GOODS_VIRTUAL:
  405. return '虚拟';
  406. case Hotax.GOODS_ENTITY:
  407. return '实体';
  408. default:
  409. return '';
  410. }
  411. }
  412. export function getResourceTypeName(type) {
  413. switch (type) {
  414. case Hotax.RESOURCE_VIDEO:
  415. return '视频';
  416. case Hotax.RESOURCE_AUDIO:
  417. return '音频';
  418. case Hotax.RESOURCE_IMAGE:
  419. return '图片';
  420. case Hotax.RESOURCE_AUDIOBOOK:
  421. return '有声读物';
  422. default:
  423. return '';
  424. }
  425. }
  426. // 视频相关常量
  427. const resourceTypes = {
  428. [Hotax.RESOURCE_AUDIO]: '音频',
  429. [Hotax.RESOURCE_VIDEO]: '视频',
  430. };
  431. const resourceQuality = {
  432. [Hotax.QUALITY_FLUENT]: '流畅',
  433. [Hotax.QUALITY_STANDARD]: '标清',
  434. [Hotax.QUALITY_HIGH]: '高清',
  435. [Hotax.QUALITY_SUPERCLEAR]: '超清',
  436. };
  437. // 价格相关常量
  438. const chargeUnitMap = {
  439. [Hotax.CHARGE_UNIT_DAY]: '天',
  440. [Hotax.CHARGE_UNIT_SEASON]: '季',
  441. [Hotax.CHARGE_UNIT_HALF_YEAR]: '半年',
  442. [Hotax.CHARGE_UNIT_YEAR]: '年',
  443. [Hotax.CHARGE_UNIT_ITEM]: '件',
  444. };
  445. const durationMap = {
  446. [Hotax.CHARGE_UNIT_SEASON]: Hotax.DURATION_SEASON,
  447. [Hotax.CHARGE_UNIT_HALF_YEAR]: Hotax.DURATION_HALF_YEAR,
  448. [Hotax.CHARGE_UNIT_YEAR]: Hotax.DURATION_YEAR,
  449. [Hotax.CHARGE_UNIT_DAY]: Hotax.DURATION_DAY,
  450. [Hotax.CHARGE_UNIT_ITEM]: Hotax.DURATION_ITEM,
  451. };
  452. const sortMap = {
  453. [Hotax.CHARGE_UNIT_ITEM]: -1,
  454. [Hotax.CHARGE_UNIT_DAY]: 0,
  455. [Hotax.CHARGE_UNIT_SEASON]: 1,
  456. [Hotax.CHARGE_UNIT_HALF_YEAR]: 2,
  457. [Hotax.CHARGE_UNIT_YEAR]: 3,
  458. };
  459. export { resourceTypes, resourceQuality, chargeUnitMap, durationMap, sortMap };