更新小程序,优化单页模式下的用户引导逻辑,确保用户在朋友圈等环境中能够顺利登录和访问完整内容。调整章节内容获取逻辑,确保未授权用户无法访问完整内容。新增手机号同步功能,提升用户资料管理体验。

This commit is contained in:
Alex-larget
2026-03-10 20:20:03 +08:00
parent 3b942fd7a4
commit dc3597c906
8 changed files with 165 additions and 19 deletions

View File

@@ -8,8 +8,8 @@ const { parseScene } = require('./utils/scene.js')
App({
globalData: {
// API基础地址 - 连接真实后端
// baseUrl: 'https://soulapi.quwanzhi.com',
baseUrl: 'https://souldev.quwanzhi.com',
baseUrl: 'https://soulapi.quwanzhi.com',
// baseUrl: 'https://souldev.quwanzhi.com',
// baseUrl: 'http://localhost:8080',
@@ -61,6 +61,10 @@ App({
// TabBar相关
currentTab: 0,
// 是否处于「单页模式」(如朋友圈文章里的单页预览)
// 用于在受限环境下给出引导文案,提示用户点击底部「前往小程序」进入完整体验
isSinglePageMode: false,
// 更新检测:上次检测时间戳,避免频繁请求
lastUpdateCheck: 0
},
@@ -69,6 +73,16 @@ App({
this.globalData.readSectionIds = wx.getStorageSync('readSectionIds') || []
// 获取系统信息
this.getSystemInfo()
// 场景值兜底1154 为「朋友圈单页模式」进入
try {
const launchOpts = wx.getLaunchOptionsSync ? wx.getLaunchOptionsSync() : null
if (launchOpts && launchOpts.scene === 1154) {
this.globalData.isSinglePageMode = true
}
} catch (e) {
console.warn('[App] 读取 LaunchOptions 失败:', e)
}
// 检查登录状态
this.checkLoginStatus()
@@ -208,6 +222,11 @@ App({
const systemInfo = wx.getSystemInfoSync()
this.globalData.systemInfo = systemInfo
this.globalData.statusBarHeight = systemInfo.statusBarHeight || 44
// 微信在单页模式下会在 systemInfo.mode 标记 singlePage
if (systemInfo.mode === 'singlePage') {
this.globalData.isSinglePageMode = true
}
// 计算导航栏高度
const menuButton = wx.getMenuButtonBoundingClientRect()
@@ -219,6 +238,33 @@ App({
}
},
/**
* 若当前处于朋友圈等「单页模式」,在尝试登录/购买前给用户友好提示,
* 引导用户点击底部「前往小程序」进入完整小程序再操作。
* 返回 false 表示应中断当前操作。
*/
ensureFullAppForAuth() {
// 每次调用时再做一次兜底检测,避免全局标记遗漏
try {
const sys = wx.getSystemInfoSync()
if (sys && sys.mode === 'singlePage') {
this.globalData.isSinglePageMode = true
}
} catch (e) {
console.warn('[App] ensureFullAppForAuth getSystemInfoSync error:', e)
}
if (!this.globalData.isSinglePageMode) return true
wx.showModal({
title: '请前往完整小程序',
content: '当前为朋友圈单页,仅支持部分浏览。如需登录和解锁内容,请点击底部「前往小程序」后再操作。',
showCancel: false,
confirmText: '我知道了',
})
return false
},
// 检查登录状态
checkLoginStatus() {
try {
@@ -386,6 +432,9 @@ App({
// 登录方法 - 获取openId用于支付加固错误处理避免审核报“登录报错”
async login() {
if (!this.ensureFullAppForAuth()) {
return null
}
try {
const loginRes = await new Promise((resolve, reject) => {
wx.login({ success: resolve, fail: reject })
@@ -446,6 +495,9 @@ App({
// 获取openId (支付必需)
async getOpenId() {
if (!this.ensureFullAppForAuth()) {
return null
}
// 先检查缓存
const cachedOpenId = wx.getStorageSync('openId')
if (cachedOpenId) {
@@ -499,6 +551,9 @@ App({
// 手机号登录:需同时传 wx.login 的 code 与 getPhoneNumber 的 phoneCode
async loginWithPhone(phoneCode) {
if (!this.ensureFullAppForAuth()) {
return null
}
try {
const loginRes = await new Promise((resolve, reject) => {
wx.login({ success: resolve, fail: reject })