优化小程序阅读页面逻辑,动态更新章节价格和免费状态,确保用户体验流畅。移除硬编码价格,支持通过接口返回的值进行展示。更新用户地址管理逻辑,简化获取地址的流程,提升代码可读性。
This commit is contained in:
@@ -250,15 +250,18 @@ Page({
|
||||
if (accessManager.canAccessFullContent(accessState)) {
|
||||
app.markSectionAsRead(id)
|
||||
}
|
||||
// 始终用接口返回的 price/isFree 更新 section(不写死 1 元)
|
||||
const section = this.data.section || {}
|
||||
if (res.price !== undefined && res.price !== null) section.price = Number(res.price)
|
||||
if (res.isFree !== undefined) section.isFree = !!res.isFree
|
||||
// 0元即免费:接口返回 price 为 0 或 isFree 为 true 时,不展示付费墙
|
||||
const isFreeByPrice = res.price === 0 || res.price === '0' || Number(res.price) === 0
|
||||
const isFreeByFlag = res.isFree === true
|
||||
if (isFreeByPrice || isFreeByFlag) {
|
||||
const section = this.data.section || {}
|
||||
if (res.price !== undefined && res.price !== null) section.price = Number(res.price)
|
||||
if (res.isFree !== undefined) section.isFree = !!res.isFree
|
||||
this.setData({ section, showPaywall: false, canAccess: true, accessState: 'free' })
|
||||
app.markSectionAsRead(id)
|
||||
} else {
|
||||
this.setData({ section })
|
||||
}
|
||||
setTimeout(() => this.drawShareCard(), 600)
|
||||
}
|
||||
@@ -304,12 +307,12 @@ Page({
|
||||
return { id, title: appendixTitles[id] || '附录', isFree: true, price: 0 }
|
||||
}
|
||||
|
||||
// 普通章节
|
||||
// 普通章节:price 不写死,由 loadContent 从 config/接口 填充
|
||||
return {
|
||||
id: id,
|
||||
title: this.getSectionTitle(id),
|
||||
isFree: id === '1.1',
|
||||
price: 1
|
||||
price: undefined
|
||||
}
|
||||
},
|
||||
|
||||
@@ -710,7 +713,7 @@ Page({
|
||||
return
|
||||
}
|
||||
|
||||
const price = this.data.section?.price || 1
|
||||
const price = this.data.section?.price ?? this.data.sectionPrice ?? 1
|
||||
console.log('[Pay] 开始支付流程:', { sectionId: this.data.sectionId, price })
|
||||
wx.hideLoading()
|
||||
await this.processPayment('section', this.data.sectionId, price)
|
||||
|
||||
@@ -63,67 +63,8 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
// 一键获取收货地址
|
||||
getAddress() {
|
||||
wx.chooseAddress({
|
||||
success: (res) => {
|
||||
console.log('[Settings] 获取地址成功:', res)
|
||||
const fullAddress = `${res.provinceName || ''}${res.cityName || ''}${res.countyName || ''}${res.detailInfo || ''}`
|
||||
|
||||
if (fullAddress.trim()) {
|
||||
wx.setStorageSync('user_address', fullAddress)
|
||||
this.setData({ address: fullAddress })
|
||||
|
||||
// 更新用户信息
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.address = fullAddress
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
|
||||
// 同步到服务器
|
||||
this.syncAddressToServer(fullAddress)
|
||||
|
||||
wx.showToast({ title: '地址已获取', icon: 'success' })
|
||||
}
|
||||
},
|
||||
fail: (e) => {
|
||||
console.log('[Settings] 获取地址失败:', e)
|
||||
if (e.errMsg?.includes('cancel')) {
|
||||
// 用户取消,不提示
|
||||
return
|
||||
}
|
||||
if (e.errMsg?.includes('auth deny') || e.errMsg?.includes('authorize')) {
|
||||
wx.showModal({
|
||||
title: '需要授权',
|
||||
content: '请在设置中允许获取收货地址',
|
||||
confirmText: '去设置',
|
||||
success: (res) => {
|
||||
if (res.confirm) wx.openSetting()
|
||||
}
|
||||
})
|
||||
} else {
|
||||
wx.showToast({ title: '获取失败,请重试', icon: 'none' })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 同步地址到服务器
|
||||
async syncAddressToServer(address) {
|
||||
try {
|
||||
const userId = app.globalData.userInfo?.id
|
||||
if (!userId) return
|
||||
|
||||
await app.request('/api/miniprogram/user/update', {
|
||||
method: 'POST',
|
||||
data: { userId, address }
|
||||
})
|
||||
console.log('[Settings] 地址已同步到服务器')
|
||||
} catch (e) {
|
||||
console.log('[Settings] 同步地址失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 收货地址已改为「地址管理」页(goToAddresses)
|
||||
|
||||
// 切换自动提现
|
||||
async toggleAutoWithdraw(e) {
|
||||
const enabled = e.detail.value
|
||||
|
||||
Reference in New Issue
Block a user