重构推荐码处理逻辑,整合 scene 解析功能,优化扫码参数接收。更新阅读页面逻辑,支持通过 scene 解析 mid、id 和 ref,提升用户体验。新增二维码生成逻辑,确保与 scene 生成闭环。

This commit is contained in:
乘风
2026-02-12 19:17:50 +08:00
parent a71e14d2e6
commit 808c959a20
4 changed files with 269 additions and 53 deletions

View File

@@ -3,6 +3,8 @@
* 开发: 卡若
*/
const { parseScene } = require('./utils/scene.js')
App({
globalData: {
// API基础地址 - 连接真实后端
@@ -83,29 +85,17 @@ App({
this.handleReferralCode(options)
},
// 处理推荐码绑定(支持 query.ref 与扫码 scenescene 可能为 ref=SOULXXX 或 id=1.1_ref=xxx后端会把 & 转成 _
// 扫码进入时options.scene 为场景值(1047),自定义 scene 在 options.query.scene
// 处理推荐码绑定:官方以 options.scene 接收扫码参数(可同时带 mid/id + ref与 utils/scene 解析闭环
handleReferralCode(options) {
const query = options?.query || {}
let refCode = query.ref || query.referralCode
// 扫码进入时自定义 scene 在 query.sceneoptions.scene 为场景值(1047)
const sceneStr = query?.scene || (typeof options?.scene === 'string' ? options.scene : null)
const sceneStr = (options && (typeof options.scene === 'string' ? options.scene : '')) || ''
if (sceneStr) {
const scene = (typeof sceneStr === 'string' ? decodeURIComponent(sceneStr) : '').trim()
// 支持 _ 或 & 作为参数分隔符(后端生成小程序码时会把 & 转为 _
const parts = scene.split(/[&_]/)
for (const part of parts) {
const eq = part.indexOf('=')
if (eq > 0) {
const key = part.slice(0, eq)
const val = part.slice(eq + 1)
if (key === 'ref') refCode = val
if (key === 'id' && val) this.globalData.initialSectionId = val
if (key === 'mid' && val) this.globalData.initialSectionMid = parseInt(val, 10) || 0
}
}
const parsed = parseScene(sceneStr)
if (parsed.mid) this.globalData.initialSectionMid = parsed.mid
if (parsed.id) this.globalData.initialSectionId = parsed.id
if (parsed.ref) refCode = parsed.ref
}
if (refCode) {
console.log('[App] 检测到推荐码:', refCode)