Enhance mini program environment configuration and chapter loading logic

- Introduced a debugging environment configuration in app.js, allowing for dynamic API endpoint selection based on the environment.
- Implemented lazy loading for chapter parts in chapters.js, improving performance by only fetching necessary data.
- Updated UI elements in chapters.wxml to reflect changes in chapter loading and added loading indicators.
- Enhanced error handling and data management for chapter retrieval, ensuring a smoother user experience.
- Added functionality for switching API environments in settings.js, visible only in development mode.
This commit is contained in:
Alex-larget
2026-03-14 18:04:05 +08:00
parent 1edceda4db
commit d82ef6d8e4
22 changed files with 1184 additions and 899 deletions

View File

@@ -5,12 +5,42 @@
const { parseScene } = require('./utils/scene.js')
// 调试环境配置:''=自动 | localhost | souldev | soulapi
const DEBUG_ENV_OPTIONS = [
{ key: '', label: '自动', url: null },
{ key: 'localhost', label: '本地', url: 'http://localhost:8080' },
{ key: 'souldev', label: '测试', url: 'https://souldev.quwanzhi.com' },
{ key: 'soulapi', label: '正式', url: 'https://soulapi.quwanzhi.com' }
]
const STORAGE_KEY_DEBUG_ENV = 'debug_env_override'
// 根据小程序环境版本或调试覆盖选择 API 地址
function getBaseUrlByEnv(override) {
if (override) {
const opt = DEBUG_ENV_OPTIONS.find(o => o.key === override)
if (opt && opt.url) return opt.url
}
try {
const accountInfo = wx.getAccountInfoSync()
const env = accountInfo?.miniProgram?.envVersion || 'release'
const urls = {
develop: 'http://localhost:8080', // 开发版(本地调试)
trial: 'https://souldev.quwanzhi.com', // 体验版
release: 'https://soulapi.quwanzhi.com' // 正式版
}
return urls[env] || urls.release
} catch (e) {
console.warn('[App] 获取环境失败,使用正式版:', e)
return 'https://soulapi.quwanzhi.com'
}
}
App({
globalData: {
// API基础地址 - 连接真实后端
// baseUrl: 'https://soulapi.quwanzhi.com',
// baseUrl: 'https://souldev.quwanzhi.com',
baseUrl: 'http://localhost:8080',
// API基础地址 - 由 getBaseUrlByEnv() 在 onLaunch 时按环境自动设置
baseUrl: '',
// 调试环境覆盖:'' | localhost | souldev | soulapi持久化到 storage
debugEnvOverride: '',
// 小程序配置 - 真实AppID
@@ -70,6 +100,8 @@ App({
},
onLaunch(options) {
this.globalData.debugEnvOverride = wx.getStorageSync(STORAGE_KEY_DEBUG_ENV) || ''
this.globalData.baseUrl = getBaseUrlByEnv(this.globalData.debugEnvOverride)
this.globalData.readSectionIds = wx.getStorageSync('readSectionIds') || []
// 获取系统信息
this.getSystemInfo()
@@ -248,6 +280,28 @@ App({
return ''
},
// 调试:获取当前 API 环境信息
getDebugEnvInfo() {
const override = this.globalData.debugEnvOverride || ''
const opt = DEBUG_ENV_OPTIONS.find(o => o.key === override)
return {
override,
label: opt ? opt.label : '自动',
baseUrl: this.globalData.baseUrl
}
},
// 调试:一键切换 API 环境(自动→本地→测试→正式→自动),持久化到 storage
switchDebugEnv() {
const idx = DEBUG_ENV_OPTIONS.findIndex(o => o.key === this.globalData.debugEnvOverride)
const nextIdx = (idx + 1) % DEBUG_ENV_OPTIONS.length
const next = DEBUG_ENV_OPTIONS[nextIdx]
this.globalData.debugEnvOverride = next.key
this.globalData.baseUrl = getBaseUrlByEnv(next.key)
wx.setStorageSync(STORAGE_KEY_DEBUG_ENV, next.key)
return next
},
/**
* 自定义导航栏「返回」:有上一页则返回,否则跳转首页(解决从分享进入时点返回无效的问题)
*/