删除不再使用的文件和配置,优化项目结构以提升可维护性;新增环境变量配置示例,更新 Docker 和部署相关文件以支持灵活的端口设置;重构数据库连接逻辑,增强错误处理和配置管理,确保更好的兼容性和稳定性。
This commit is contained in:
@@ -1,415 +1,120 @@
|
||||
/**
|
||||
* Soul创业派对 - 设置页
|
||||
* 账号绑定功能
|
||||
*/
|
||||
const app = getApp()
|
||||
|
||||
Page({
|
||||
data: {
|
||||
statusBarHeight: 44,
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
version: '1.0.0',
|
||||
|
||||
// 绑定信息
|
||||
phoneNumber: '',
|
||||
wechatId: '',
|
||||
alipayAccount: '',
|
||||
addressSummary: '管理收货地址',
|
||||
|
||||
// 自动提现(默认开启)
|
||||
autoWithdrawEnabled: true,
|
||||
|
||||
// 绑定弹窗
|
||||
navBarHeight: 88,
|
||||
user: null,
|
||||
showBindModal: false,
|
||||
bindType: '', // phone | wechat | alipay
|
||||
bindValue: ''
|
||||
bindType: 'phone',
|
||||
bindValue: '',
|
||||
isBinding: false,
|
||||
bindError: ''
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.setData({
|
||||
statusBarHeight: app.globalData.statusBarHeight,
|
||||
isLoggedIn: app.globalData.isLoggedIn,
|
||||
userInfo: app.globalData.userInfo
|
||||
})
|
||||
this.loadBindingInfo()
|
||||
const statusBarHeight = app.globalData.statusBarHeight || 44
|
||||
const navBarHeight = app.globalData.navBarHeight || (statusBarHeight + 44)
|
||||
this.setData({ statusBarHeight, navBarHeight })
|
||||
this.syncUser()
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadBindingInfo()
|
||||
this.syncUser()
|
||||
},
|
||||
|
||||
// 加载绑定信息
|
||||
loadBindingInfo() {
|
||||
const { userInfo, isLoggedIn } = app.globalData
|
||||
if (isLoggedIn && userInfo) {
|
||||
// 从本地存储或用户信息中获取绑定数据
|
||||
const phoneNumber = wx.getStorageSync('user_phone') || userInfo.phone || ''
|
||||
const wechatId = wx.getStorageSync('user_wechat') || userInfo.wechat || ''
|
||||
const alipayAccount = wx.getStorageSync('user_alipay') || userInfo.alipay || ''
|
||||
// 默认开启自动提现
|
||||
const autoWithdrawEnabled = wx.getStorageSync('auto_withdraw_enabled') !== false
|
||||
|
||||
this.setData({
|
||||
isLoggedIn: true,
|
||||
userInfo,
|
||||
phoneNumber,
|
||||
wechatId,
|
||||
alipayAccount,
|
||||
autoWithdrawEnabled
|
||||
})
|
||||
this.loadAddressSummary()
|
||||
}
|
||||
},
|
||||
|
||||
// 加载地址摘要(共 N 个地址)
|
||||
async loadAddressSummary() {
|
||||
try {
|
||||
const userId = app.globalData.userInfo?.id
|
||||
if (!userId) return
|
||||
const res = await app.request('/api/user/addresses', { data: { userId } })
|
||||
if (res.success && res.list && res.list.length > 0) {
|
||||
this.setData({ addressSummary: `共${res.list.length}个收货地址` })
|
||||
} else {
|
||||
this.setData({ addressSummary: '管理收货地址' })
|
||||
}
|
||||
} catch (e) {
|
||||
this.setData({ addressSummary: '管理收货地址' })
|
||||
}
|
||||
syncUser() {
|
||||
const user = app.globalData.userInfo || null
|
||||
this.setData({ user })
|
||||
},
|
||||
|
||||
// 跳转收货地址列表(增删改查),未登录时提示先登录
|
||||
goToAddressList() {
|
||||
if (!app.globalData.isLoggedIn || !app.globalData.userInfo) {
|
||||
wx.showToast({ title: '请先登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
goBack() {
|
||||
wx.navigateBack({ fail: () => wx.switchTab({ url: '/pages/my/my' }) })
|
||||
},
|
||||
|
||||
goAddresses() {
|
||||
wx.navigateTo({ url: '/pages/address-list/address-list' })
|
||||
},
|
||||
|
||||
// 切换自动提现
|
||||
async toggleAutoWithdraw(e) {
|
||||
const enabled = e.detail.value
|
||||
|
||||
// 检查是否绑定了支付方式
|
||||
if (enabled && !this.data.wechatId && !this.data.alipayAccount) {
|
||||
wx.showToast({ title: '请先绑定微信号或支付宝', icon: 'none' })
|
||||
this.setData({ autoWithdrawEnabled: false })
|
||||
return
|
||||
}
|
||||
|
||||
// 开启时需要确认
|
||||
if (enabled) {
|
||||
wx.showModal({
|
||||
title: '开启自动提现',
|
||||
content: `收益将自动打款到您的${this.data.alipayAccount ? '支付宝' : '微信'}账户,确认开启吗?`,
|
||||
success: async (res) => {
|
||||
if (res.confirm) {
|
||||
this.setData({ autoWithdrawEnabled: true })
|
||||
wx.setStorageSync('auto_withdraw_enabled', true)
|
||||
|
||||
// 同步到服务器
|
||||
try {
|
||||
await app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: {
|
||||
userId: app.globalData.userInfo?.id,
|
||||
autoWithdraw: true,
|
||||
withdrawAccount: this.data.alipayAccount || this.data.wechatId
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.log('同步自动提现设置失败', e)
|
||||
}
|
||||
|
||||
wx.showToast({ title: '已开启自动提现', icon: 'success' })
|
||||
} else {
|
||||
this.setData({ autoWithdrawEnabled: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.setData({ autoWithdrawEnabled: false })
|
||||
wx.setStorageSync('auto_withdraw_enabled', false)
|
||||
wx.showToast({ title: '已关闭自动提现', icon: 'success' })
|
||||
}
|
||||
},
|
||||
|
||||
// 绑定手机号
|
||||
bindPhone() {
|
||||
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: 'phone',
|
||||
bindValue: ''
|
||||
bindType: type,
|
||||
bindValue,
|
||||
bindError: ''
|
||||
})
|
||||
},
|
||||
|
||||
// 微信号输入
|
||||
onWechatInput(e) {
|
||||
this.setData({ wechatId: e.detail.value })
|
||||
},
|
||||
|
||||
// 保存微信号
|
||||
async saveWechat() {
|
||||
const { wechatId } = this.data
|
||||
if (!wechatId || wechatId.length < 6) return
|
||||
|
||||
wx.setStorageSync('user_wechat', wechatId)
|
||||
|
||||
// 更新用户信息
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.wechat = wechatId
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
|
||||
// 同步到服务器
|
||||
try {
|
||||
await app.request('/api/user/update', {
|
||||
method: 'POST',
|
||||
data: {
|
||||
userId: app.globalData.userInfo?.id,
|
||||
wechat: wechatId
|
||||
}
|
||||
})
|
||||
wx.showToast({ title: '微信号已保存', icon: 'success' })
|
||||
} catch (e) {
|
||||
console.log('保存微信号失败', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 输入绑定值
|
||||
onBindInput(e) {
|
||||
let value = e.detail.value
|
||||
if (this.data.bindType === 'phone') {
|
||||
value = value.replace(/\D/g, '').slice(0, 11)
|
||||
}
|
||||
this.setData({ bindValue: value })
|
||||
},
|
||||
|
||||
// 确认绑定
|
||||
confirmBind() {
|
||||
const { bindType, bindValue } = this.data
|
||||
|
||||
if (!bindValue) {
|
||||
wx.showToast({ title: '请输入内容', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
// 验证
|
||||
if (bindType === 'phone' && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
||||
wx.showToast({ title: '请输入正确的手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
if (bindType === 'wechat' && bindValue.length < 6) {
|
||||
wx.showToast({ title: '微信号至少6位', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
if (bindType === 'alipay' && !bindValue.includes('@') && !/^1[3-9]\d{9}$/.test(bindValue)) {
|
||||
wx.showToast({ title: '请输入正确的支付宝账号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
// 保存绑定信息到本地
|
||||
if (bindType === 'phone') {
|
||||
wx.setStorageSync('user_phone', bindValue)
|
||||
this.setData({ phoneNumber: bindValue })
|
||||
} else if (bindType === 'wechat') {
|
||||
wx.setStorageSync('user_wechat', bindValue)
|
||||
this.setData({ wechatId: bindValue })
|
||||
} else if (bindType === 'alipay') {
|
||||
wx.setStorageSync('user_alipay', bindValue)
|
||||
this.setData({ alipayAccount: bindValue })
|
||||
}
|
||||
|
||||
// 同步到服务器
|
||||
this.syncProfileToServer()
|
||||
|
||||
this.setData({ showBindModal: false })
|
||||
wx.showToast({ title: '绑定成功', icon: 'success' })
|
||||
},
|
||||
|
||||
// 同步资料到服务器
|
||||
async syncProfileToServer() {
|
||||
try {
|
||||
const userId = app.globalData.userInfo?.id
|
||||
if (!userId) return
|
||||
|
||||
const res = await app.request('/api/user/profile', {
|
||||
method: 'POST',
|
||||
data: {
|
||||
userId,
|
||||
phone: this.data.phoneNumber || undefined,
|
||||
wechatId: this.data.wechatId || undefined
|
||||
}
|
||||
})
|
||||
|
||||
if (res.success) {
|
||||
console.log('[Settings] 资料同步成功')
|
||||
// 更新本地用户信息
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.phone = this.data.phoneNumber
|
||||
app.globalData.userInfo.wechatId = this.data.wechatId
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('[Settings] 资料同步失败:', e)
|
||||
}
|
||||
},
|
||||
|
||||
// 获取微信头像(新版授权)
|
||||
async getWechatAvatar() {
|
||||
try {
|
||||
const res = await wx.getUserProfile({
|
||||
desc: '用于完善会员资料'
|
||||
})
|
||||
|
||||
if (res.userInfo) {
|
||||
const { nickName, avatarUrl } = res.userInfo
|
||||
|
||||
// 更新本地
|
||||
this.setData({
|
||||
userInfo: {
|
||||
...this.data.userInfo,
|
||||
nickname: nickName,
|
||||
avatar: avatarUrl
|
||||
}
|
||||
})
|
||||
|
||||
// 同步到服务器
|
||||
const userId = app.globalData.userInfo?.id
|
||||
if (userId) {
|
||||
await app.request('/api/user/profile', {
|
||||
method: 'POST',
|
||||
data: { userId, nickname: nickName, avatar: avatarUrl }
|
||||
})
|
||||
}
|
||||
|
||||
// 更新全局
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.nickname = nickName
|
||||
app.globalData.userInfo.avatar = avatarUrl
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
|
||||
wx.showToast({ title: '头像更新成功', icon: 'success' })
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('[Settings] 获取头像失败:', e)
|
||||
wx.showToast({ title: '获取头像失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
// 一键获取微信手机号(button组件回调)
|
||||
async onGetPhoneNumber(e) {
|
||||
console.log('[Settings] 获取手机号回调:', e.detail)
|
||||
|
||||
if (e.detail.errMsg !== 'getPhoneNumber:ok') {
|
||||
wx.showToast({ title: '授权失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 需要将code发送到服务器解密获取手机号
|
||||
const code = e.detail.code
|
||||
if (!code) {
|
||||
// 如果没有code,弹出手动输入
|
||||
this.bindPhone()
|
||||
return
|
||||
}
|
||||
|
||||
wx.showLoading({ title: '获取中...', mask: true })
|
||||
|
||||
// 调用服务器解密手机号(传入userId以便同步到数据库)
|
||||
const userId = app.globalData.userInfo?.id
|
||||
const res = await app.request('/api/miniprogram/phone', {
|
||||
method: 'POST',
|
||||
data: { code, userId }
|
||||
})
|
||||
|
||||
wx.hideLoading()
|
||||
|
||||
if (res.success && res.phoneNumber) {
|
||||
wx.setStorageSync('user_phone', res.phoneNumber)
|
||||
this.setData({ phoneNumber: res.phoneNumber })
|
||||
|
||||
// 更新用户信息
|
||||
if (app.globalData.userInfo) {
|
||||
app.globalData.userInfo.phone = res.phoneNumber
|
||||
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
||||
}
|
||||
|
||||
// 同步到服务器
|
||||
this.syncProfileToServer()
|
||||
|
||||
wx.showToast({ title: '手机号绑定成功', icon: 'success' })
|
||||
} else {
|
||||
// 获取失败,弹出手动输入
|
||||
this.bindPhone()
|
||||
}
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
console.log('[Settings] 获取手机号失败:', e)
|
||||
// 获取失败,弹出手动输入
|
||||
this.bindPhone()
|
||||
}
|
||||
},
|
||||
|
||||
// 关闭绑定弹窗
|
||||
closeBindModal() {
|
||||
this.setData({ showBindModal: false })
|
||||
if (!this.data.isBinding) this.setData({ showBindModal: false, bindValue: '', bindError: '' })
|
||||
},
|
||||
|
||||
// 清除缓存
|
||||
clearCache() {
|
||||
wx.showModal({
|
||||
title: '清除缓存',
|
||||
content: '确定要清除本地缓存吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 保留登录信息,只清除其他缓存
|
||||
const token = wx.getStorageSync('token')
|
||||
const userInfo = wx.getStorageSync('userInfo')
|
||||
wx.clearStorageSync()
|
||||
if (token) wx.setStorageSync('token', token)
|
||||
if (userInfo) wx.setStorageSync('userInfo', userInfo)
|
||||
wx.showToast({ title: '缓存已清除', icon: 'success' })
|
||||
}
|
||||
onBindInput(e) {
|
||||
this.setData({ bindValue: (e.detail && e.detail.value) || '', bindError: '' })
|
||||
},
|
||||
|
||||
submitBind() {
|
||||
const { bindType, bindValue, user } = 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: user && user.id,
|
||||
[bindType]: bindValue
|
||||
}
|
||||
}).then(() => {
|
||||
const newUser = { ...user, [bindType]: bindValue }
|
||||
app.globalData.userInfo = newUser
|
||||
wx.setStorageSync('userInfo', newUser)
|
||||
this.setData({
|
||||
user: newUser,
|
||||
showBindModal: false,
|
||||
bindValue: '',
|
||||
isBinding: false
|
||||
})
|
||||
wx.showToast({ title: '绑定成功', icon: 'success' })
|
||||
}).catch(() => {
|
||||
this.setData({ bindError: '绑定失败,请重试', isBinding: false })
|
||||
})
|
||||
},
|
||||
|
||||
// 退出登录
|
||||
handleLogout() {
|
||||
logout() {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '确定要退出登录吗?',
|
||||
title: '提示',
|
||||
content: '确定退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
app.logout()
|
||||
this.setData({
|
||||
isLoggedIn: false,
|
||||
userInfo: null,
|
||||
phoneNumber: '',
|
||||
wechatId: '',
|
||||
alipayAccount: ''
|
||||
})
|
||||
wx.showToast({ title: '已退出登录', icon: 'success' })
|
||||
setTimeout(() => wx.navigateBack(), 1500)
|
||||
app.globalData.userInfo = null
|
||||
app.globalData.isLoggedIn = false
|
||||
app.globalData.purchasedSections = []
|
||||
app.globalData.hasFullBook = false
|
||||
wx.removeStorageSync('userInfo')
|
||||
wx.removeStorageSync('token')
|
||||
wx.switchTab({ url: '/pages/index/index' })
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 联系客服 - 跳转到Soul派对房
|
||||
contactService() {
|
||||
wx.showToast({ title: '请在Soul派对房联系客服', icon: 'none' })
|
||||
},
|
||||
|
||||
// 阻止冒泡
|
||||
stopPropagation() {},
|
||||
|
||||
goBack() { wx.navigateBack() }
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user