forked from yoone/WEB
1
0
Fork 0
WEB/src/utils/format.ts

68 lines
1.8 KiB
TypeScript

import { ProSchemaValueEnumObj } from '@ant-design/pro-components';
import { BaseType } from 'typings';
/**
* 将数组转换成 valueEnum 格式
* @param array 数据源数组
* @param options 转换配置
* @returns valueEnum 对象
*/
export function transformToValueEnum<T>(
array: T[],
options: BaseType.EnumTransformOptions = {
value: 'id',
label: 'name',
status: 'status',
color: 'color',
},
): ProSchemaValueEnumObj {
const { value, label, status, color } = options;
return array.reduce((acc, item: any) => {
const key = String(item[value]);
acc[key] = {
text: String(item[label]),
...(status && item[status] ? { status: String(item[status]) } : {}),
...(color && item[color] ? { color: String(item[color]) } : {}),
};
return acc;
}, {} as ProSchemaValueEnumObj);
}
export function formatSource(type: string | undefined, utm?: string) {
if (!type) return 'Unknon';
if (type === 'admin') return 'Web Admin';
if (type === 'typein') return 'Direct';
if (type === 'utm') return 'Source: ' + utm;
return type + ': ' + utm;
}
export function formatShipmentState(state: string) {
switch (state) {
case 'draft':
return '草稿';
case 'waiting-for-scheduling':
return '等待调度';
case 'waiting-for-transit':
return '等待运输';
case 'in-transit':
return '运输中';
case 'delivered':
return '已到达';
case 'exception':
return '异常';
case 'missing':
return '丢失';
case 'cancelled':
return '取消';
case '190': // ORDER_RECEIVED
return '订单待发送';
case '202': // IN_TRANSIT
return '在途中';
case '203': // DELIVERED
return '订单已送达';
default:
return '';
}
}