feat: 添加小程序码场景下名片 ID 解析及处理逻辑

This commit is contained in:
2026-10-27 15:57:29 +08:00
parent d356416518
commit 4c5f070e7c

View File

@@ -369,11 +369,22 @@ export default {
}
},
onLoad(options) {
this.cardId = this.resolveCardId(options);
this.applyPageTitle();
this.loadDetail();
this.loadExchange();
this.loadDesignEnabled();
// 2.21 小程序码入口: scene 里只有名片**主键 id**(微信 scene 仅 32 字符,
// 装不下 30 字符的业务 card_id), 需先换成 card_id 才能查详情。
const directId = this.pickDirectCardId(options);
const sceneId = this.pickSceneRelId(options);
if (directId) {
this.cardId = directId;
this.loadDetail();
this.loadExchange();
this.loadDesignEnabled();
} else if (sceneId) {
this.resolveCardIdByRelId(sceneId);
} else {
this.isLoading = false;
this.errorMsg = '缺少名片编号';
}
},
// H5 直接刷新/深链进入时, uni-page-head 可能在路由 meta 就绪前就挂载,
// 此时标题会回落到 document.title(uni-app H5 默认值 "title");
@@ -406,33 +417,78 @@ export default {
};
},
methods: {
// 名片 ID 解析(2.21 小程序码场景):
// ① 常规路由: query 带 card_id
// ② 微信小程序码: 扫「小程序码」进来时, 微信把码里的 scene 放在
// options.scene(URL 编码过), page 本身不带 query
// → 需 decodeURIComponent 后按 key=value&... 解析出 card_id
// 两者都取不到时返回空串(页面显示"缺少名片编号", 保持原有报错口径)
resolveCardId(options) {
// 2.21 小程序码: 名片 ID 解析
//
// 微信 getwxacodeunlimit 的 scene 限制 32 可见字符, 装不下业务 card_id
// (形如 c_1_shop_20261013111612_9588EA, 30 字符) → scene 里放的是
// 名片**主键 id**(数字)。故两条入口:
// ① 常规路由: query 带 card_id → 直接用
// ② 小程序码: options.scene = "id=123" → 先按主键换业务 card_id
//
// 常规入口(①)
pickDirectCardId(options) {
const opt = options || {};
if (opt.card_id) {
return decodeURIComponent(opt.card_id);
}
if (opt.scene) {
try {
const scene = decodeURIComponent(opt.scene);
const pairs = scene.split('&');
for (let i = 0; i < pairs.length; i++) {
const kv = pairs[i].split('=');
if (kv[0] === 'card_id' && kv[1]) {
return decodeURIComponent(kv.slice(1).join('='));
}
}
return decodeURIComponent(opt.card_id);
} catch (e) {
// scene 解析失败(非预期编码)→ 回落空串, 走"缺少名片编号"提示
return opt.card_id;
}
}
return '';
},
// 小程序码入口(②): 从 scene 里取名片主键 id
// scene 形态: "id=123" (微信会先 encodeURIComponent, 需先解码)
pickSceneRelId(options) {
const opt = options || {};
if (!opt.scene) {
return 0;
}
let scene = '';
try {
scene = decodeURIComponent(opt.scene);
} catch (e) {
scene = opt.scene;
}
// 兼容 "id=123" 与裸 "123"
let val = scene;
const pairs = scene.split('&');
for (let i = 0; i < pairs.length; i++) {
const kv = pairs[i].split('=');
if (kv[0] === 'id' && kv[1]) {
val = kv[1];
break;
}
}
val = String(val).replace(/[^0-9]/g, '');
return val ? parseInt(val, 10) : 0;
},
// 按主键 id 换业务 card_id(后端 /card/api/diy/card 支持 id 参数寻址)
resolveCardIdByRelId(relId) {
this.isLoading = true;
this.$api.sendRequest({
url: '/card/api/diy/card',
data: { id: relId },
success: res => {
const card = (res.code == 0 && res.data && res.data.card) ? res.data.card : null;
const bizId = (card && card.card_id) ? card.card_id : '';
if (!bizId) {
this.isLoading = false;
this.errorMsg = '名片不存在或已被删除';
this.relation = null;
return;
}
this.cardId = bizId;
this.loadDetail();
this.loadExchange();
this.loadDesignEnabled();
},
fail: () => {
this.isLoading = false;
this.errorMsg = '网络异常,请稍后重试';
},
});
},
// 导航栏标题统一出口: 小程序/鸿蒙/App 走原生导航栏;
// H5 额外同步 document.title, 避免 uni-page-head 标题回落到默认值 "title"。
applyPageTitle() {