oms/backend/app/lib/platformCode.js
liufumingren 69b26bf7cc feat(platform): 统一系统预设平台编码,外部平台标识按预设转换
- 新增 app/lib/platformCode.js:17 个预设平台(1688/DW/DYCS/DYGX/DYXD/JD/KS/MAOCHAO/
  MTYYJK/OFFLINE/OTHER/PDD/SPH/TB/TGC/TM/XHS)+ 别名表(中英文、大小写)+ 未知落 OTHER
- 店管家硬编码 PlatformType → 预设映射(Pinduoduo→PDD、TouTiao→DYXD、WxVideo→SPH、
  Alibaba→1688、Taobao→TB、KuaiShou→KS、Tmall→TM、JingDong→JD…)
- 拉单落库统一:orders.platform_type、order_status_*.source_platform_code/name
  写预设编码与本系统中文名(推 WMS 的 sourcePlatformCode/Name 同源)
- 店铺商品 platform_type 同样落预设编码
- 新增 scripts/migrate-platform-codes.js(npm run platforms:migrate):
  按同一口径迁移历史数据,默认 dry-run;已执行迁移 14 类(订单 27 / 店铺商品 44 / 状态表 15+13)
- 前端新增 constants/platform.ts(code→中文名):店铺商品「平台类型」列显示中文名,
  订单详情新增「来源平台」行
- 新增 platformCode 单测(5 项,含未知平台落 OTHER)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-03 17:31:44 +08:00

151 lines
4.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 系统预设平台编码。
*
* 外部打单平台(店管家等)返回的是各自的平台标识(Pinduoduo / TouTiao / WxVideo…),
* 订单落库、推送 WMS(奇门 sourcePlatformCode/sourcePlatformName)需要统一口径,
* 因此全系统只认这里的一套预设编码,外部标识通过别名表映射过来。
*/
const PLATFORM_PRESETS = [
{ code: '1688', name: '阿里巴巴' },
{ code: 'DW', name: '得物' },
{ code: 'DYCS', name: '抖音超市' },
{ code: 'DYGX', name: '抖音供销' },
{ code: 'DYXD', name: '抖音小店' },
{ code: 'JD', name: '京东' },
{ code: 'KS', name: '快手' },
{ code: 'MAOCHAO', name: '天猫超市' },
{ code: 'MTYYJK', name: '美团医药健康' },
{ code: 'OFFLINE', name: '线下' },
{ code: 'OTHER', name: '其他' },
{ code: 'PDD', name: '拼多多' },
{ code: 'SPH', name: '视频号' },
{ code: 'TB', name: '淘宝' },
{ code: 'TGC', name: '淘工厂' },
{ code: 'TM', name: '天猫' },
{ code: 'XHS', name: '小红书' },
];
/** 预设编码 -> 名称 */
const PLATFORM_NAME_BY_CODE = new Map(PLATFORM_PRESETS.map(item => [ item.code, item.name ]));
/** 未知平台统一落到「其他」 */
const PLATFORM_UNKNOWN_CODE = 'OTHER';
/**
* 外部标识 / 中文名 -> 预设编码(全部按小写比对)。
* 只放通用别名;各打单平台自己的枚举(如店管家 PlatformType)在对应 service 里硬编码。
*/
const PLATFORM_ALIASES = {
// 1688 / 阿里巴巴
1688: '1688',
ali: '1688',
alibaba: '1688',
阿里巴巴: '1688',
// 得物
dw: 'DW',
dewu: 'DW',
得物: 'DW',
// 抖音系
dycs: 'DYCS',
抖音超市: 'DYCS',
dygx: 'DYGX',
抖音供销: 'DYGX',
dyxd: 'DYXD',
douyin: 'DYXD',
doudian: 'DYXD',
toutiao: 'DYXD',
抖店: 'DYXD',
抖音: 'DYXD',
抖音小店: 'DYXD',
// 京东
jd: 'JD',
jingdong: 'JD',
京东: 'JD',
// 快手
ks: 'KS',
kuaishou: 'KS',
快手: 'KS',
// 天猫超市
maochao: 'MAOCHAO',
tmcs: 'MAOCHAO',
天猫超市: 'MAOCHAO',
// 美团医药健康
mtyyjk: 'MTYYJK',
meituan: 'MTYYJK',
美团: 'MTYYJK',
美团医药健康: 'MTYYJK',
// 线下 / 其他
offline: 'OFFLINE',
线下: 'OFFLINE',
other: 'OTHER',
其他: 'OTHER',
// 拼多多
pdd: 'PDD',
pinduoduo: 'PDD',
拼多多: 'PDD',
// 视频号
sph: 'SPH',
wxvideo: 'SPH',
wechatvideo: 'SPH',
视频号: 'SPH',
// 淘宝
tb: 'TB',
taobao: 'TB',
淘宝: 'TB',
// 淘工厂
tgc: 'TGC',
taobaofactory: 'TGC',
淘工厂: 'TGC',
// 天猫
tm: 'TM',
tmall: 'TM',
天猫: 'TM',
// 小红书
xhs: 'XHS',
xiaohongshu: 'XHS',
redbook: 'XHS',
小红书: 'XHS',
};
/**
* 解析外部平台标识为预设编码:先查别名表,本身就是预设编码时直接返回,未知落到 OTHER
* @param {*} raw 外部平台标识(大小写、中英文均可)
* @return {string} 预设编码
*/
function resolvePlatformCode(raw) {
const key = String(raw == null ? '' : raw).trim().toLowerCase();
if (!key) return PLATFORM_UNKNOWN_CODE;
if (PLATFORM_NAME_BY_CODE.has(key.toUpperCase())) return key.toUpperCase();
return PLATFORM_ALIASES[key] || PLATFORM_UNKNOWN_CODE;
}
/**
* 预设编码对应的中文名,未知编码原样返回(便于排查)
* @param {*} code 平台编码
* @return {string} 平台名称
*/
function platformNameOf(code) {
const key = String(code == null ? '' : code).trim().toUpperCase();
return PLATFORM_NAME_BY_CODE.get(key) || String(code == null ? '' : code);
}
/**
* 解析为「编码 + 名称」:订单表 source_platform_code / source_platform_name 直接落这两个值
* @param {*} raw 外部平台标识
* @return {{code: string, name: string}} 预设平台
*/
function resolvePlatform(raw) {
const code = resolvePlatformCode(raw);
return { code, name: platformNameOf(code) };
}
module.exports = {
PLATFORM_PRESETS,
PLATFORM_UNKNOWN_CODE,
resolvePlatform,
resolvePlatformCode,
platformNameOf,
};