import React from 'react';
import moment from 'moment';
import { Badge, Avatar } from 'antd';
import { Hotax } from './config';
const ProvinceCodes = {
11: '北京',
12: '天津',
13: '河北省',
14: '山西省',
15: '内蒙古自治区',
21: '辽宁省',
22: '吉林省',
23: '黑龙江省',
31: '上海',
32: '江苏省',
33: '浙江省',
34: '安徽省',
35: '福建省',
36: '江西省',
37: '山东省',
41: '河南省',
42: '湖北省',
43: '湖南省',
44: '广东省',
45: '广西壮族自治区',
46: '海南省',
50: '重庆',
51: '四川省',
52: '贵州省',
53: '云南省',
54: '西藏自治区',
61: '陕西省',
62: '甘肃省',
63: '青海省',
64: '宁夏回族自治区',
65: '新疆维吾尔自治区',
71: '台湾',
81: '香港特别行政区',
82: '澳门特别行政区',
99: '海外',
// 43湖南校区编号超过99,省份代码顺延为90
90: '湖南省',
};
export function fixedZero(val) {
return val * 1 < 10 ? `0${val}` : val;
}
export function getTimeDistance(type) {
const now = new Date();
const oneDay = 1000 * 60 * 60 * 24;
if (type === 'today') {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
return [moment(now), moment(now.getTime() + (oneDay - 1000))];
}
if (type === 'week') {
let day = now.getDay();
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
if (day === 0) {
day = 6;
} else {
day -= 1;
}
const beginTime = now.getTime() - (day * oneDay);
return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))];
}
if (type === 'month') {
const year = now.getFullYear();
const month = now.getMonth();
const nextDate = moment(now).add(1, 'months');
const nextYear = nextDate.year();
const nextMonth = nextDate.month();
return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000)];
}
if (type === 'year') {
const year = now.getFullYear();
return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}
}
export function getPlainNode(nodeList, parentPath = '') {
const arr = [];
nodeList.forEach((node) => {
const item = node;
item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
item.exact = true;
if (item.children && !item.component) {
arr.push(...getPlainNode(item.children, item.path));
} else {
if (item.children && item.component) {
item.exact = false;
}
arr.push(item);
}
});
return arr;
}
export function digitUppercase(n) {
const fraction = ['角', '分'];
const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
const unit = [
['元', '万', '亿'],
['', '拾', '佰', '仟'],
];
let num = Math.abs(n);
let s = '';
fraction.forEach((item, index) => {
s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/零./, '');
});
s = s || '整';
num = Math.floor(num);
for (let i = 0; i < unit[0].length && num > 0; i += 1) {
let p = '';
for (let j = 0; j < unit[1].length && num > 0; j += 1) {
p = digit[num % 10] + unit[1][j] + p;
num = Math.floor(num / 10);
}
s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
}
return s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}
function getRelation(str1, str2) {
if (str1 === str2) {
console.warn('Two path are equal!'); // eslint-disable-line
}
const arr1 = str1.split('/');
const arr2 = str2.split('/');
if (arr2.every((item, index) => item === arr1[index])) {
return 1;
} else if (arr1.every((item, index) => item === arr2[index])) {
return 2;
}
return 3;
}
function getRenderArr(routes) {
let renderArr = [];
renderArr.push(routes[0]);
for (let i = 1; i < routes.length; i += 1) {
let isAdd = false;
// 是否包含
isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
// 去重
renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
if (isAdd) {
renderArr.push(routes[i]);
}
}
return renderArr;
}
/**
* Get router routing configuration
* { path:{name,...param}}=>Array<{name,path ...param}>
* @param {string} path
* @param {routerData} routerData
*/
export function getRoutes(path, routerData) {
let routes = Object.keys(routerData).filter(routePath =>
routePath.indexOf(path) === 0 && routePath !== path);
// Replace path to '' eg. path='user' /user/name => name
routes = routes.map(item => item.replace(path, ''));
// Get the route to be rendered to remove the deep rendering
const renderArr = getRenderArr(routes);
// Conversion and stitching parameters
const renderRoutes = renderArr.map((item) => {
const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
return {
...routerData[`${path}${item}`],
key: `${path}${item}`,
path: `${path}${item}`,
exact,
};
});
return renderRoutes;
}
/* eslint no-useless-escape:0 */
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g;
export function isUrl(path) {
return reg.test(path);
}
export function addRowKey(data) {
if (!data) {
return [];
}
return data.map((item) => {
return { key: item.id, ...item };
});
}
export function renderCategory(domain) {
switch (domain) {
case Hotax.DOMAIN_CP:
return '供应商';
case Hotax.DOMAIN_LJ:
return '平台方';
case Hotax.DOMAIN_PJ:
return '渠道商';
default:
return '';
}
}
export function renderStatus(status, delText, normalText) {
if (status === Hotax.STATUS_NORMAL) {
return (
);
} else {
return (
);
}
}
export function statusCodeToName(status) {
if (status === Hotax.STATUS_NORMAL) {
return '正常';
} else if (status === Hotax.STATUS_DELETE) {
return '已删除';
} else {
return '';
}
}
export function renderBindStatus(status, delText, normalText) {
if (status === Hotax.ACCOUNT_BINDING) {
return (
);
} else {
return (
);
}
}
export function renderVideoQuality(quality) {
if (quality === 'high') {
return '高清';
} else {
return '标清';
}
}
export function renderAvatar(avatar, name) {
const colorList = [
'#ff4d4f', // red-5
'#ff7a45', // volcano-5
'#ffa940', // orange-5
'#ffc53d', // gold-5
'#ffec3d', // yellow-5
'#bae637', // lime-5
'#73d13d', // green-5
'#36cfc9', // cyan-5
'#40a9ff', // blue-5
'#597ef7', // geekblue-5
'#9254de', // purple-5
'#f759ab', // magenta-5
];
return (
{name[0]}
);
}
export function renderGender(gender) {
if (gender === 'MALE') {
return '男';
} else if (gender === 'FEMALE') {
return '女';
} else {
return '未知';
}
}
export function genAbsolutePicUrl(path) {
return `${Hotax.OSS_HOST}/${path}`;
}
export function statusToBool(status) {
if (status === Hotax.STATUS_NORMAL) {
return true;
} else if (status === Hotax.STATUS_DELETE) {
return false;
} else {
return true;
}
}
export function boolToStatus(bool) {
if (bool) {
return Hotax.STATUS_NORMAL;
} else {
return Hotax.STATUS_DELETE;
}
}
export function renderProductType(type) {
if (type === Hotax.PRODUCT_COURSE) {
return '课程';
} else if (type === Hotax.PRODUCT_SUPPORT) {
return '配套';
} else if (type === Hotax.PRODUCT_PACKAGE) {
return '套餐包';
} else {
return '';
}
}
export function toDecimal2(x) {
let f = parseFloat(x);
if (isNaN(f)) {
return false;
}
f = Math.round(x * 100) / 100;
let s = f.toString();
let rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}
export function provinceCodeToName(pcode) {
return ProvinceCodes[pcode];
}
export function provinceNameToCode(pname) {
const match = Object.keys(ProvinceCodes).filter((code) => {
if (ProvinceCodes[code] === pname) {
return true;
}
return false;
});
return match[0];
}
export function checkProductType(typeStr) {
switch (typeStr) {
case Hotax.PRODUCT_COURSE:
return '课程';
case Hotax.PRODUCT_SUPPORT:
return '配套';
case Hotax.PRODUCT_TRAINING:
return '师训';
case Hotax.PRODUCT_PACKAGE:
return '套餐包';
default:
return '';
}
}
export function renderOrderStatus(status, isPlainText) {
const map = {
[Hotax.ORDER_UNPAID]: {
name: '未支付',
type: 'default',
},
[Hotax.ORDER_CANCEL]: {
name: '已作废',
type: 'error',
},
[Hotax.ORDER_PAYOK]: {
name: '已支付',
type: 'processing',
},
[Hotax.ORDER_COMPLETE]: {
name: '已完成',
type: 'success',
},
[Hotax.ORDER_FORSEND]: {
name: '待发货',
type: 'processing',
},
[Hotax.ORDER_SENT]: {
name: '已发货',
type: 'processing',
},
[Hotax.ORDER_RECEIVED]: {
name: '已收货',
type: 'warning',
},
[Hotax.ORDER_REFUND]: {
name: '退款中',
type: 'warning',
},
};
const obj = map[status] || { name: '未定义', type: 'error' };
if (isPlainText) {
return obj.name;
} else {
return ;
}
}
export function renderOrderSplitStatus(status) {
switch (status) {
case Hotax.ORDER_NOSPLIT:
return '未拆单';
case Hotax.ORDER_UNSPLIT:
return '无需拆单';
case Hotax.ORDER_SPLITED:
return '已拆单';
default:
return '';
}
}
export function renderGoodsType(status) {
switch (status) {
case Hotax.GOODS_VIRTUAL:
return '虚拟';
case Hotax.GOODS_ENTITY:
return '实体';
default:
return '';
}
}
export function getResourceTypeName(type) {
switch (type) {
case Hotax.RESOURCE_VIDEO:
return '视频';
case Hotax.RESOURCE_AUDIO:
return '音频';
case Hotax.RESOURCE_IMAGE:
return '图片';
case Hotax.RESOURCE_AUDIOBOOK:
return '有声读物';
default:
return '';
}
}
// 视频相关常量
const resourceTypes = {
[Hotax.RESOURCE_AUDIO]: '音频',
[Hotax.RESOURCE_VIDEO]: '视频',
};
const resourceQuality = {
[Hotax.QUALITY_FLUENT]: '流畅',
[Hotax.QUALITY_STANDARD]: '标清',
[Hotax.QUALITY_HIGH]: '高清',
[Hotax.QUALITY_SUPERCLEAR]: '超清',
};
// 价格相关常量
const chargeUnitMap = {
[Hotax.CHARGE_UNIT_DAY]: '天',
[Hotax.CHARGE_UNIT_SEASON]: '季',
[Hotax.CHARGE_UNIT_HALF_YEAR]: '半年',
[Hotax.CHARGE_UNIT_YEAR]: '年',
[Hotax.CHARGE_UNIT_ITEM]: '件',
};
const durationMap = {
[Hotax.CHARGE_UNIT_SEASON]: Hotax.DURATION_SEASON,
[Hotax.CHARGE_UNIT_HALF_YEAR]: Hotax.DURATION_HALF_YEAR,
[Hotax.CHARGE_UNIT_YEAR]: Hotax.DURATION_YEAR,
[Hotax.CHARGE_UNIT_DAY]: Hotax.DURATION_DAY,
[Hotax.CHARGE_UNIT_ITEM]: Hotax.DURATION_ITEM,
};
const sortMap = {
[Hotax.CHARGE_UNIT_ITEM]: -1,
[Hotax.CHARGE_UNIT_DAY]: 0,
[Hotax.CHARGE_UNIT_SEASON]: 1,
[Hotax.CHARGE_UNIT_HALF_YEAR]: 2,
[Hotax.CHARGE_UNIT_YEAR]: 3,
};
export { resourceTypes, resourceQuality, chargeUnitMap, durationMap, sortMap };