feat: 添加小程序码场景下名片 ID 解析及处理逻辑
This commit is contained in:
@@ -369,11 +369,22 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onLoad(options) {
|
onLoad(options) {
|
||||||
this.cardId = this.resolveCardId(options);
|
|
||||||
this.applyPageTitle();
|
this.applyPageTitle();
|
||||||
|
// 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.loadDetail();
|
||||||
this.loadExchange();
|
this.loadExchange();
|
||||||
this.loadDesignEnabled();
|
this.loadDesignEnabled();
|
||||||
|
} else if (sceneId) {
|
||||||
|
this.resolveCardIdByRelId(sceneId);
|
||||||
|
} else {
|
||||||
|
this.isLoading = false;
|
||||||
|
this.errorMsg = '缺少名片编号';
|
||||||
|
}
|
||||||
},
|
},
|
||||||
// H5 直接刷新/深链进入时, uni-page-head 可能在路由 meta 就绪前就挂载,
|
// H5 直接刷新/深链进入时, uni-page-head 可能在路由 meta 就绪前就挂载,
|
||||||
// 此时标题会回落到 document.title(uni-app H5 默认值 "title");
|
// 此时标题会回落到 document.title(uni-app H5 默认值 "title");
|
||||||
@@ -406,33 +417,78 @@ export default {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
// 名片 ID 解析(2.21 小程序码场景):
|
// 2.21 小程序码: 名片 ID 解析
|
||||||
// ① 常规路由: query 带 card_id
|
//
|
||||||
// ② 微信小程序码: 扫「小程序码」进来时, 微信把码里的 scene 放在
|
// 微信 getwxacodeunlimit 的 scene 限制 32 可见字符, 装不下业务 card_id
|
||||||
// options.scene(URL 编码过), page 本身不带 query
|
// (形如 c_1_shop_20261013111612_9588EA, 30 字符) → scene 里放的是
|
||||||
// → 需 decodeURIComponent 后按 key=value&... 解析出 card_id
|
// 名片**主键 id**(数字)。故两条入口:
|
||||||
// 两者都取不到时返回空串(页面显示"缺少名片编号", 保持原有报错口径)
|
// ① 常规路由: query 带 card_id → 直接用
|
||||||
resolveCardId(options) {
|
// ② 小程序码: options.scene = "id=123" → 先按主键换业务 card_id
|
||||||
|
//
|
||||||
|
// 常规入口(①)
|
||||||
|
pickDirectCardId(options) {
|
||||||
const opt = options || {};
|
const opt = options || {};
|
||||||
if (opt.card_id) {
|
if (opt.card_id) {
|
||||||
return decodeURIComponent(opt.card_id);
|
|
||||||
}
|
|
||||||
if (opt.scene) {
|
|
||||||
try {
|
try {
|
||||||
const scene = decodeURIComponent(opt.scene);
|
return decodeURIComponent(opt.card_id);
|
||||||
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('='));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// scene 解析失败(非预期编码)→ 回落空串, 走"缺少名片编号"提示
|
return opt.card_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return '';
|
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 走原生导航栏;
|
// 导航栏标题统一出口: 小程序/鸿蒙/App 走原生导航栏;
|
||||||
// H5 额外同步 document.title, 避免 uni-page-head 标题回落到默认值 "title"。
|
// H5 额外同步 document.title, 避免 uni-page-head 标题回落到默认值 "title"。
|
||||||
applyPageTitle() {
|
applyPageTitle() {
|
||||||
|
|||||||
Reference in New Issue
Block a user