const userApi = require('../../api/user') const navigation = require('../../utils/navigation') const format = require('../../utils/format') const { getTempRecord, clearTempRecord, setTempRecord } = require('../../utils/storage') const CHANGE_SCENE_OPTIONS = ['MANUAL_RECHARGE', 'MANUAL_INCREASE', 'MANUAL_DEDUCT'] const VIP_LEVEL_OPTIONS = ['普通', '青铜', '白银', '黄金'] Page({ data: { user: null, asset: null, balanceHistory: [], changeSceneIndex: 0, vipLevelIndex: 0, balanceForm: { amount: '', externalOrderNo: '', remark: '' }, vipRemark: '' }, onLoad(options) { const cached = getTempRecord('currentUser', null) if (cached && (!options || !options.id || String(cached.id) === String(options.id))) { this.setUser(cached) } }, onShow() { if (!navigation.ensureLogin('进入用户详情')) { return } this.loadAssetData() }, onUnload() { clearTempRecord('currentUser') }, async loadAssetData() { if (!this.data.user) { return } try { const [asset, balanceHistory] = await Promise.all([ userApi.getUserAsset(this.data.user.id).catch(() => null), userApi.getUserBalanceHistory(this.data.user.id).catch(() => []) ]) this.setData({ asset, balanceHistory: (Array.isArray(balanceHistory) ? balanceHistory : []).map(item => ({ ...item, displayChangeTime: format.formatDateTime(item.changeTime), amountBadgeClass: Number(item.amount) >= 0 ? 'success-badge' : 'danger-badge' })) }) } catch (error) { wx.showToast({ title: error.message || '用户资产数据加载失败', icon: 'none' }) } }, handleEdit() { setTempRecord('currentUser', this.data.user) navigation.navigate('/pages/user-form/index', { mode: 'edit', id: this.data.user.id }).catch(() => undefined) }, handlePermissionAssign() { setTempRecord('currentUser', this.data.user) navigation.navigate('/pages/permission-form/index', { mode: 'assign', userId: this.data.user.id }).catch(() => undefined) }, handleBalanceInput(event) { const field = event.currentTarget.dataset.field this.setData({ [`balanceForm.${field}`]: event.detail.value }) }, handleVipRemarkInput(event) { this.setData({ vipRemark: event.detail.value }) }, handleSceneChange(event) { this.setData({ changeSceneIndex: Number(event.detail.value) }) }, handleVipLevelChange(event) { this.setData({ vipLevelIndex: Number(event.detail.value) }) }, async handleBalanceChange() { try { await userApi.changeUserBalance(this.data.user.id, { amount: Number(this.data.balanceForm.amount), changeScene: CHANGE_SCENE_OPTIONS[this.data.changeSceneIndex], externalOrderNo: this.data.balanceForm.externalOrderNo || undefined, remark: this.data.balanceForm.remark || undefined }) wx.showToast({ title: '余额已调整', icon: 'none' }) this.setData({ balanceForm: { amount: '', externalOrderNo: '', remark: '' } }) this.loadAssetData() } catch (error) { wx.showToast({ title: error.message || '余额调整失败', icon: 'none' }) } }, async handleVipOverride() { try { await userApi.overrideUserVip(this.data.user.id, { vipLevel: VIP_LEVEL_OPTIONS[this.data.vipLevelIndex], remark: this.data.vipRemark || undefined }) wx.showToast({ title: 'VIP 等级已更新', icon: 'none' }) this.loadAssetData() } catch (error) { wx.showToast({ title: error.message || 'VIP 更新失败', icon: 'none' }) } }, async handleVipRecalculate() { try { await userApi.recalculateUserVip(this.data.user.id) wx.showToast({ title: '已恢复自动判级', icon: 'none' }) this.loadAssetData() } catch (error) { wx.showToast({ title: error.message || 'VIP 重算失败', icon: 'none' }) } }, async handleResetPassword() { try { await userApi.resetPassword(this.data.user.id) wx.showToast({ title: '密码已重置', icon: 'none' }) } catch (error) { wx.showToast({ title: error.message || '密码重置失败', icon: 'none' }) } }, handleUserAction(event) { const action = event.currentTarget.dataset.action if (action === 'disable') { this.updateUserStatus(this.data.user.status === 0 ? 1 : 0) return } if (action === 'delete') { this.confirmDelete() return } if (action === 'unbind') { this.confirmUnbind() } }, async updateUserStatus(status) { try { await userApi.updateUserStatus(this.data.user.id, status) wx.showToast({ title: '用户状态已更新', icon: 'none' }) const user = { ...this.data.user, status } this.setUser(user) setTempRecord('currentUser', user) } catch (error) { wx.showToast({ title: error.message || '状态更新失败', icon: 'none' }) } }, confirmDelete() { wx.showModal({ title: '确认删除', content: '删除后该账号将不再出现在前端可见列表中,确认继续吗?', success: async result => { if (!result.confirm) { return } try { await userApi.deleteUser(this.data.user.id) wx.showToast({ title: '用户已删除', icon: 'none' }) navigation.goBack('/pages/users/index') } catch (error) { wx.showToast({ title: error.message || '删除失败', icon: 'none' }) } } }) }, confirmUnbind() { wx.showModal({ title: '确认解绑', content: '解绑后,该用户将无法登录当前店铺,确认继续吗?', success: async result => { if (!result.confirm) { return } try { await userApi.unbindUser(this.data.user.id) wx.showToast({ title: '已解除当前店铺绑定', icon: 'none' }) } catch (error) { wx.showToast({ title: error.message || '解绑失败', icon: 'none' }) } } }) }, setUser(user) { this.setData({ user: user ? { ...user, displayTypeText: format.getUserTypeText(user.type), displayShopName: format.buildShopName(user), displayStatusText: format.getUserStatusText(user.status), displayCreateTime: format.formatDateTime(user.createTime) } : null }) } })