const API = require('../../utils/api.js') Page({ data: { isLoggedIn: false, userInfo: null, phoneNumber: '', version: '1.0.0', showLoginPopup: false, loginForm: { name: '', mobile: '' }, menuList: [ { icon: 'user', title: '个人资料', url: '' }, { icon: 'notification', title: '消息通知', url: '' }, { icon: 'lock-on', title: '账号安全', url: '' }, { icon: 'help-circle', title: '帮助中心', url: '' }, { icon: 'info-circle', title: '关于我们', url: '' }, ] }, onLoad() { this.checkLoginStatus() this.setData({ version: this.getAppVersion() }) const app = getApp() app.eventBus.on('user-login', this.onUserLogin) }, onUnload() { const app = getApp() app.eventBus.off('user-login', this.onUserLogin) }, onShow() { this.checkLoginStatus() }, onUserLogin(user) { this.setData({ isLoggedIn: true, userInfo: user, phoneNumber: this.maskPhoneNumber(user.mobile || '') }) }, checkLoginStatus() { const app = getApp() const user = app.globalData.user if (user && user.security && user.security.token) { this.setData({ isLoggedIn: true, userInfo: user, phoneNumber: this.maskPhoneNumber(user.mobile || '') }) } }, maskPhoneNumber(phone) { if (!phone || phone.length < 7) return phone return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') }, getAppVersion() { const accountInfo = wx.getAccountInfoSync() return accountInfo.miniProgram.version || '1.0.0' }, onShowLoginPopup() { this.setData({ showLoginPopup: true }) }, onCloseLoginPopup() { this.setData({ showLoginPopup: false, loginForm: { name: '', mobile: '' } }) }, onNameInput(e) { this.setData({ 'loginForm.name': e.detail.value }) }, onMobileInput(e) { this.setData({ 'loginForm.mobile': e.detail.value }) }, onLoginSubmit() { const { name, mobile } = this.data.loginForm if (!name.trim()) { wx.showToast({ title: '请输入姓名', icon: 'none' }) return } if (!mobile.trim()) { wx.showToast({ title: '请输入手机号', icon: 'none' }) return } const phoneReg = /^1[3-9]\d{9}$/ if (!phoneReg.test(mobile.trim())) { wx.showToast({ title: '手机号格式不正确', icon: 'none' }) return } wx.showLoading({ title: '登录中...' }) wx.login({ success: (res) => { if (res.code) { API.user.wxSignin({ phoneNumber: mobile.trim(), name: name.trim(), code: res.code }) .then((data) => { if (data.code == 0) { const app = getApp() app.globalData.user = data.data.user this.setData({ isLoggedIn: true, userInfo: app.globalData.user, phoneNumber: this.maskPhoneNumber(app.globalData.user.profile.mobile || ''), showLoginPopup: false, loginForm: { name: '', mobile: '' } }) wx.showToast({ title: '登录成功', icon: 'success' }) } else { wx.showToast({ title: '登录失败', icon: 'none' }) } wx.hideLoading() }) } else { wx.showToast({ title: '登录失败', icon: 'none' }) } } }).catch(err => { console.error('登录失败', err) wx.showToast({ title: '登录失败,请重试', icon: 'none' }) }).finally(() => { wx.hideLoading() }) }, onMenuTap(e) { const { index } = e.currentTarget.dataset const item = this.data.menuList[index] if (!this.data.isLoggedIn) { wx.showToast({ title: '请先登录', icon: 'none' }) return } if (item.url) { wx.navigateTo({ url: item.url }) } else { wx.showToast({ title: '功能开发中', icon: 'none' }) } }, onLogout() { wx.showModal({ title: '退出登录', content: '确定要退出登录吗?', confirmColor: '#4c6ef5', success: (res) => { if (res.confirm) { wx.showLoading({ title: '退出中...' }) API.user.signout().then(() => { this.doLogout() }).catch(() => { this.doLogout() }).finally(() => { wx.hideLoading() }) } } }) }, doLogout() { const app = getApp() app.globalData.user = null wx.removeStorageSync('admin_user_info') wx.removeStorageSync('admin_ai_session_id') wx.removeStorageSync('admin_ai_chat_history') this.setData({ isLoggedIn: false, userInfo: null, phoneNumber: '' }) wx.showToast({ title: '已退出登录', icon: 'success' }) }, onPopupContentTap() { // 阻止冒泡,防止点击弹窗内容时关闭弹窗 }, onClearCache() { wx.showModal({ title: '清除缓存', content: '确定要清除所有缓存数据吗?', confirmColor: '#4c6ef5', success: (res) => { if (res.confirm) { wx.clearStorage({ success: () => { const app = getApp() app.globalData.user = null this.setData({ isLoggedIn: false, userInfo: null, phoneNumber: '' }) wx.showToast({ title: '缓存已清除', icon: 'success' }) } }) } } }) } })