删除不再使用的文件和配置,优化项目结构以提升可维护性;新增环境变量配置示例,更新 Docker 和部署相关文件以支持灵活的端口设置;重构数据库连接逻辑,增强错误处理和配置管理,确保更好的兼容性和稳定性。
This commit is contained in:
@@ -1,348 +1,163 @@
|
||||
/**
|
||||
* Soul创业派对 - 我的页面
|
||||
* 开发: 卡若
|
||||
* 技术支持: 存客宝
|
||||
*/
|
||||
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
data: {
|
||||
// 系统信息
|
||||
statusBarHeight: 44,
|
||||
navBarHeight: 88,
|
||||
|
||||
// 用户状态
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
|
||||
// 统计数据
|
||||
totalSections: 62,
|
||||
user: null,
|
||||
userInitial: 'U',
|
||||
userIdSuffix: '---',
|
||||
earningsText: '0.00',
|
||||
pendingEarningsText: '0.00',
|
||||
earningsDisplay: '--',
|
||||
purchasedCount: 0,
|
||||
referralCount: 0,
|
||||
earnings: 0,
|
||||
pendingEarnings: 0,
|
||||
|
||||
// 阅读统计
|
||||
totalReadTime: 0,
|
||||
matchHistory: 0,
|
||||
|
||||
// Tab切换
|
||||
activeTab: 'overview', // overview | footprint
|
||||
|
||||
// 最近阅读
|
||||
totalSections: 62,
|
||||
matchEnabled: false,
|
||||
activeTab: 'overview',
|
||||
completedOrders: 0,
|
||||
recentChapters: [],
|
||||
|
||||
// 菜单列表
|
||||
menuList: [
|
||||
{ id: 'orders', title: '我的订单', icon: '📦', count: 0 },
|
||||
{ id: 'referral', title: '推广中心', icon: '🎁', badge: '' },
|
||||
{ id: 'about', title: '关于作者', icon: '👤', iconBg: 'brand' },
|
||||
{ id: 'settings', title: '设置', icon: '⚙️', iconBg: 'gray' }
|
||||
],
|
||||
|
||||
// 登录弹窗
|
||||
showLoginModal: false,
|
||||
isLoggingIn: false
|
||||
totalReadTime: 50,
|
||||
matchHistoryCount: 0,
|
||||
showBindModal: false,
|
||||
bindType: 'phone',
|
||||
bindValue: '',
|
||||
isBinding: false,
|
||||
bindError: ''
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.setData({
|
||||
statusBarHeight: app.globalData.statusBarHeight,
|
||||
navBarHeight: app.globalData.navBarHeight
|
||||
this.setNavBarHeight()
|
||||
this.syncUser()
|
||||
app.loadFeatureConfig().then(() => {
|
||||
this.setData({ matchEnabled: app.globalData.matchEnabled === true })
|
||||
})
|
||||
this.initUserStatus()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 设置TabBar选中状态
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().setData({ selected: 3 })
|
||||
}
|
||||
this.initUserStatus()
|
||||
},
|
||||
|
||||
// 初始化用户状态
|
||||
initUserStatus() {
|
||||
const { isLoggedIn, userInfo, hasFullBook, purchasedSections } = app.globalData
|
||||
|
||||
if (isLoggedIn && userInfo) {
|
||||
// 转换为对象数组
|
||||
const recentList = (purchasedSections || []).slice(-5).map(id => ({
|
||||
id: id,
|
||||
title: `章节 ${id}`
|
||||
}))
|
||||
|
||||
// 截短用户ID显示
|
||||
const userId = userInfo.id || ''
|
||||
const userIdShort = userId.length > 20 ? userId.slice(0, 10) + '...' + userId.slice(-6) : userId
|
||||
|
||||
// 获取微信号(优先显示)
|
||||
const userWechat = wx.getStorageSync('user_wechat') || userInfo.wechat || ''
|
||||
|
||||
this.setData({
|
||||
isLoggedIn: true,
|
||||
userInfo,
|
||||
userIdShort,
|
||||
userWechat,
|
||||
purchasedCount: hasFullBook ? this.data.totalSections : (purchasedSections?.length || 0),
|
||||
referralCount: userInfo.referralCount || 0,
|
||||
earnings: userInfo.earnings || 0,
|
||||
pendingEarnings: userInfo.pendingEarnings || 0,
|
||||
recentChapters: recentList,
|
||||
totalReadTime: Math.floor(Math.random() * 200) + 50
|
||||
})
|
||||
} else {
|
||||
this.setData({
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
userIdShort: '',
|
||||
purchasedCount: 0,
|
||||
referralCount: 0,
|
||||
earnings: 0,
|
||||
pendingEarnings: 0,
|
||||
recentChapters: []
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
// 微信原生获取头像(button open-type="chooseAvatar" 回调)
|
||||
async onChooseAvatar(e) {
|
||||
const avatarUrl = e.detail.avatarUrl
|
||||
if (!avatarUrl) return
|
||||
|
||||
wx.showLoading({ title: '更新中...', mask: true })
|
||||
|
||||
try {
|
||||
const userInfo = this.data.userInfo
|
||||
userInfo.avatar = avatarUrl
|
||||
this.setData({ userInfo })
|
||||
app.globalData.userInfo = userInfo
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
// 同步到服务器
|
||||
await app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: { userId: userInfo.id, avatar: avatarUrl }
|
||||
})
|
||||
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '头像已获取', icon: 'success' })
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
console.log('同步头像失败', e)
|
||||
wx.showToast({ title: '头像已更新', icon: 'success' })
|
||||
}
|
||||
},
|
||||
|
||||
// 微信原生获取昵称(input type="nickname" 回调)
|
||||
async onNicknameInput(e) {
|
||||
const nickname = e.detail.value
|
||||
if (!nickname || nickname === this.data.userInfo?.nickname) return
|
||||
|
||||
try {
|
||||
const userInfo = this.data.userInfo
|
||||
userInfo.nickname = nickname
|
||||
this.setData({ userInfo })
|
||||
app.globalData.userInfo = userInfo
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
// 同步到服务器
|
||||
await app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: { userId: userInfo.id, nickname }
|
||||
})
|
||||
|
||||
wx.showToast({ title: '昵称已获取', icon: 'success' })
|
||||
} catch (e) {
|
||||
console.log('同步昵称失败', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 点击昵称修改(备用)
|
||||
editNickname() {
|
||||
wx.showModal({
|
||||
title: '修改昵称',
|
||||
editable: true,
|
||||
placeholderText: '请输入昵称',
|
||||
success: async (res) => {
|
||||
if (res.confirm && res.content) {
|
||||
const newNickname = res.content.trim()
|
||||
if (newNickname.length < 1 || newNickname.length > 20) {
|
||||
wx.showToast({ title: '昵称1-20个字符', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
// 更新本地
|
||||
const userInfo = this.data.userInfo
|
||||
userInfo.nickname = newNickname
|
||||
this.setData({ userInfo })
|
||||
app.globalData.userInfo = userInfo
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
|
||||
// 同步到服务器
|
||||
try {
|
||||
await app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: { userId: userInfo.id, nickname: newNickname }
|
||||
})
|
||||
} catch (e) {
|
||||
console.log('同步昵称到服务器失败', e)
|
||||
}
|
||||
|
||||
wx.showToast({ title: '昵称已更新', icon: 'success' })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 复制用户ID
|
||||
copyUserId() {
|
||||
const userId = this.data.userInfo?.id || ''
|
||||
if (!userId) {
|
||||
wx.showToast({ title: '暂无ID', icon: 'none' })
|
||||
return
|
||||
}
|
||||
wx.setClipboardData({
|
||||
data: userId,
|
||||
success: () => {
|
||||
wx.showToast({ title: 'ID已复制', icon: 'success' })
|
||||
}
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) this.getTabBar().setData({ selected: 3 })
|
||||
this.setNavBarHeight()
|
||||
this.syncUser()
|
||||
app.loadFeatureConfig().then(() => {
|
||||
this.setData({ matchEnabled: app.globalData.matchEnabled === true })
|
||||
})
|
||||
},
|
||||
|
||||
// 切换Tab
|
||||
switchTab(e) {
|
||||
setNavBarHeight() {
|
||||
const statusBarHeight = app.globalData.statusBarHeight || 44
|
||||
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
|
||||
this.setData({ statusBarHeight, navBarHeight })
|
||||
},
|
||||
|
||||
syncUser() {
|
||||
const isLoggedIn = !!app.globalData.isLoggedIn
|
||||
const user = app.globalData.userInfo || null
|
||||
const total = app.globalData.bookData ? (Array.isArray(app.globalData.bookData) ? app.globalData.bookData.length : 62) : 62
|
||||
const purchasedSections = app.globalData.purchasedSections || []
|
||||
const hasFullBook = !!app.globalData.hasFullBook
|
||||
const purchasedCount = hasFullBook ? total : purchasedSections.length
|
||||
const recentChapters = (purchasedSections && purchasedSections.slice(-5)) || []
|
||||
const userInitial = user && user.nickname ? String(user.nickname).charAt(0) : 'U'
|
||||
const userIdSuffix = user && user.id ? String(user.id).slice(-8) : '---'
|
||||
const earningsText = Number(user && user.earnings != null ? user.earnings : 0).toFixed(2)
|
||||
const pendingEarningsText = Number(user && user.pendingEarnings != null ? user.pendingEarnings : 0).toFixed(2)
|
||||
const earningsDisplay = user && (user.earnings != null) && Number(user.earnings) > 0 ? '¥' + Number(user.earnings).toFixed(0) : '--'
|
||||
this.setData({
|
||||
isLoggedIn,
|
||||
user,
|
||||
userInitial,
|
||||
userIdSuffix,
|
||||
earningsText,
|
||||
pendingEarningsText,
|
||||
earningsDisplay,
|
||||
totalSections: total,
|
||||
purchasedCount,
|
||||
completedOrders: 0,
|
||||
recentChapters,
|
||||
totalReadTime: 50 + Math.floor(Math.random() * 150),
|
||||
matchHistoryCount: 0
|
||||
})
|
||||
},
|
||||
|
||||
doLogin() {
|
||||
app.login().then(() => {
|
||||
this.syncUser()
|
||||
wx.showToast({ title: '登录成功', icon: 'success' })
|
||||
})
|
||||
},
|
||||
|
||||
setActiveTab(e) {
|
||||
const tab = e.currentTarget.dataset.tab
|
||||
this.setData({ activeTab: tab })
|
||||
},
|
||||
|
||||
// 显示登录弹窗
|
||||
showLogin() {
|
||||
this.setData({ showLoginModal: true })
|
||||
},
|
||||
|
||||
// 关闭登录弹窗
|
||||
closeLoginModal() {
|
||||
if (this.data.isLoggingIn) return
|
||||
this.setData({ showLoginModal: false })
|
||||
},
|
||||
|
||||
// 微信登录
|
||||
async handleWechatLogin() {
|
||||
this.setData({ isLoggingIn: true })
|
||||
|
||||
try {
|
||||
const result = await app.login()
|
||||
if (result) {
|
||||
this.initUserStatus()
|
||||
this.setData({ showLoginModal: false })
|
||||
wx.showToast({ title: '登录成功', icon: 'success' })
|
||||
} else {
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('微信登录错误:', e)
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ isLoggingIn: false })
|
||||
}
|
||||
},
|
||||
|
||||
// 手机号登录(需要用户授权)
|
||||
async handlePhoneLogin(e) {
|
||||
// 检查是否有授权code
|
||||
if (!e.detail.code) {
|
||||
// 用户拒绝授权或获取失败,尝试使用微信登录
|
||||
console.log('手机号授权失败,尝试微信登录')
|
||||
return this.handleWechatLogin()
|
||||
}
|
||||
|
||||
this.setData({ isLoggingIn: true })
|
||||
|
||||
try {
|
||||
const result = await app.loginWithPhone(e.detail.code)
|
||||
if (result) {
|
||||
this.initUserStatus()
|
||||
this.setData({ showLoginModal: false })
|
||||
wx.showToast({ title: '登录成功', icon: 'success' })
|
||||
} else {
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('手机号登录错误:', e)
|
||||
wx.showToast({ title: '登录失败,请重试', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ isLoggingIn: false })
|
||||
}
|
||||
},
|
||||
|
||||
// 点击菜单
|
||||
handleMenuTap(e) {
|
||||
const id = e.currentTarget.dataset.id
|
||||
|
||||
if (!this.data.isLoggedIn && id !== 'about') {
|
||||
this.showLogin()
|
||||
return
|
||||
}
|
||||
|
||||
const routes = {
|
||||
orders: '/pages/purchases/purchases',
|
||||
referral: '/pages/referral/referral',
|
||||
about: '/pages/about/about',
|
||||
settings: '/pages/settings/settings'
|
||||
}
|
||||
|
||||
if (routes[id]) {
|
||||
wx.navigateTo({ url: routes[id] })
|
||||
}
|
||||
},
|
||||
|
||||
// 跳转到阅读页
|
||||
goAbout() { wx.navigateTo({ url: '/pages/about/about' }) },
|
||||
goPurchases() { wx.navigateTo({ url: '/pages/purchases/purchases' }) },
|
||||
goReferral() { wx.navigateTo({ url: '/pages/referral/referral' }) },
|
||||
goSettings() { wx.navigateTo({ url: '/pages/settings/settings' }) },
|
||||
goMatch() { wx.switchTab({ url: '/pages/match/match' }) },
|
||||
goChapters() { wx.switchTab({ url: '/pages/chapters/chapters' }) },
|
||||
goToRead(e) {
|
||||
const id = e.currentTarget.dataset.id
|
||||
wx.navigateTo({ url: `/pages/read/read?id=${id}` })
|
||||
if (id) wx.navigateTo({ url: '/pages/read/read?id=' + encodeURIComponent(id) })
|
||||
},
|
||||
|
||||
// 跳转到目录
|
||||
goToChapters() {
|
||||
wx.switchTab({ url: '/pages/chapters/chapters' })
|
||||
},
|
||||
|
||||
// 跳转到关于页
|
||||
goToAbout() {
|
||||
wx.navigateTo({ url: '/pages/about/about' })
|
||||
},
|
||||
|
||||
// 跳转到匹配
|
||||
goToMatch() {
|
||||
wx.switchTab({ url: '/pages/match/match' })
|
||||
},
|
||||
|
||||
// 跳转到推广中心
|
||||
goToReferral() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.showLogin()
|
||||
return
|
||||
}
|
||||
wx.navigateTo({ url: '/pages/referral/referral' })
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
handleLogout() {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
app.logout()
|
||||
this.initUserStatus()
|
||||
wx.showToast({ title: '已退出登录', icon: 'success' })
|
||||
}
|
||||
}
|
||||
openBindModal(e) {
|
||||
const type = e.currentTarget.dataset.type
|
||||
const user = this.data.user
|
||||
let bindValue = ''
|
||||
if (type === 'phone' && user && user.phone) bindValue = user.phone
|
||||
if (type === 'wechat' && user && user.wechat) bindValue = user.wechat
|
||||
if (type === 'alipay' && user && user.alipay) bindValue = user.alipay
|
||||
this.setData({
|
||||
showBindModal: true,
|
||||
bindType: type,
|
||||
bindValue,
|
||||
bindError: ''
|
||||
})
|
||||
},
|
||||
|
||||
// 阻止冒泡
|
||||
stopPropagation() {}
|
||||
closeBindModal() {
|
||||
if (!this.data.isBinding) this.setData({ showBindModal: false, bindValue: '', bindError: '' })
|
||||
},
|
||||
|
||||
onBindInput(e) {
|
||||
this.setData({ bindValue: (e.detail && e.detail.value) || '', bindError: '' })
|
||||
},
|
||||
|
||||
submitBind() {
|
||||
const { bindType, bindValue } = this.data
|
||||
if (!bindValue || !bindValue.trim()) {
|
||||
this.setData({ bindError: '请输入内容' })
|
||||
return
|
||||
}
|
||||
if (bindType === 'phone' && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
||||
this.setData({ bindError: '请输入正确的手机号' })
|
||||
return
|
||||
}
|
||||
if (bindType === 'wechat' && bindValue.length < 6) {
|
||||
this.setData({ bindError: '微信号至少6位' })
|
||||
return
|
||||
}
|
||||
if (bindType === 'alipay' && !bindValue.includes('@') && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
||||
this.setData({ bindError: '请输入正确的支付宝账号' })
|
||||
return
|
||||
}
|
||||
this.setData({ isBinding: true, bindError: '' })
|
||||
app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: {
|
||||
userId: this.data.user && this.data.user.id,
|
||||
[bindType]: bindValue
|
||||
}
|
||||
}).then(() => {
|
||||
const user = { ...this.data.user, [bindType]: bindValue }
|
||||
app.globalData.userInfo = user
|
||||
wx.setStorageSync('userInfo', user)
|
||||
this.setData({ user, showBindModal: false, bindValue: '', isBinding: false })
|
||||
wx.showToast({ title: '绑定成功', icon: 'success' })
|
||||
}).catch(() => {
|
||||
this.setData({ bindError: '绑定失败,请重试', isBinding: false })
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user