utils.js 12 KB

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