diff --git a/miniprogram/app.js b/miniprogram/app.js index 23c26251..dc48a58b 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -83,11 +83,14 @@ App({ }, // 处理推荐码绑定(支持 query.ref 与扫码 scene;scene 可能为 ref=SOULXXX 或 id=1.1_ref=xxx,后端会把 & 转成 _) + // 扫码进入时:options.scene 为场景值(1047),自定义 scene 在 options.query.scene handleReferralCode(options) { const query = options?.query || {} let refCode = query.ref || query.referralCode - if (options?.scene) { - const scene = (typeof options.scene === 'string' ? decodeURIComponent(options.scene) : '').trim() + // 扫码进入时自定义 scene 在 query.scene;options.scene 为场景值(1047) + const sceneStr = query?.scene || (typeof options?.scene === 'string' ? options.scene : null) + if (sceneStr) { + const scene = (typeof sceneStr === 'string' ? decodeURIComponent(sceneStr) : '').trim() // 支持 _ 或 & 作为参数分隔符(后端生成小程序码时会把 & 转为 _) const parts = scene.split(/[&_]/) for (const part of parts) { diff --git a/miniprogram/pages/read/read.js b/miniprogram/pages/read/read.js index 20e59199..c33e3657 100644 --- a/miniprogram/pages/read/read.js +++ b/miniprogram/pages/read/read.js @@ -73,11 +73,30 @@ Page({ }, async onLoad(options) { - // 扫码进入时 id 可能在 app.globalData.initialSectionId(scene 里 id=1.1 由 app 解析) + // 扫码进入时:options.id 无;id 可能在 app.globalData.initialSectionId(App 解析 query.scene) + // 或直接在 options.scene 中(页面 query 为 ?scene=id%3D1.1,后端把 & 转成 _) let id = options.id || app.globalData.initialSectionId + if (!id && options.scene) { + const scene = (typeof options.scene === 'string' ? decodeURIComponent(options.scene) : '').trim() + const parts = scene.split(/[&_]/) + for (const part of parts) { + const eq = part.indexOf('=') + if (eq > 0 && part.slice(0, eq) === 'id') { + id = part.slice(eq + 1) + break + } + } + } if (app.globalData.initialSectionId) delete app.globalData.initialSectionId const ref = options.ref + if (!id) { + console.warn('[Read] 未获取到章节 id,options:', options) + wx.showToast({ title: '章节参数缺失', icon: 'none' }) + this.setData({ accessState: 'error', loading: false }) + return + } + this.setData({ statusBarHeight: app.globalData.statusBarHeight, navBarHeight: app.globalData.navBarHeight, diff --git a/miniprogram/project.private.config.json b/miniprogram/project.private.config.json index b62aec12..d1d083dd 100644 --- a/miniprogram/project.private.config.json +++ b/miniprogram/project.private.config.json @@ -23,12 +23,19 @@ "condition": { "miniprogram": { "list": [ + { + "name": "ces", + "pathName": "pages/read/read", + "query": "scene=id%3D1.1", + "scene": null, + "launchMode": "default" + }, { "name": "pages/read/read", "pathName": "pages/read/read", "query": "id=1.2", - "scene": null, - "launchMode": "default" + "launchMode": "default", + "scene": null } ] }