const app = getApp(); Page({ data: { isLoading: false }, onLoad: function (options) { if (app.globalData.isLoggedIn) { this.goBack(); } }, onGetPhoneNumber: function (e) { if (this.data.isLoading) return; const detail = e.detail; if (!detail.code) { wx.showToast({ title: '授权失败', icon: 'none' }); return; } this.setData({ isLoading: true }); wx.login({ success: (loginRes) => { if (loginRes.code) { this.doLogin(loginRes.code, detail.code); } else { this.setData({ isLoading: false }); wx.showToast({ title: '获取登录凭证失败', icon: 'none' }); } }, fail: () => { this.setData({ isLoading: false }); wx.showToast({ title: '登录失败', icon: 'none' }); } }); }, doLogin: function (loginCode, phoneCode) { const requestUrl = `${app.globalData.baseUrl}/api/wechat/login`; console.log('[Login] baseUrl:', app.globalData.baseUrl); console.log('[Login] 请求地址:', requestUrl); console.log('[Login] loginCode:', loginCode ? loginCode.substring(0, 10) + '...' : 'empty'); console.log('[Login] phoneCode:', phoneCode ? phoneCode.substring(0, 10) + '...' : 'empty'); wx.request({ url: requestUrl, method: 'POST', data: { code: loginCode, phoneCode: phoneCode }, timeout: 30000, success: (response) => { this.setData({ isLoading: false }); console.log('[Login] 响应状态码:', response.statusCode); console.log('[Login] 响应数据:', JSON.stringify(response.data)); if (response.data && response.data.success) { app.globalData.token = response.data.token; app.globalData.userInfo = response.data.user; app.globalData.isLoggedIn = true; wx.setStorageSync('token', response.data.token); wx.setStorageSync('userInfo', response.data.user); wx.showToast({ title: '登录成功', icon: 'success' }); setTimeout(() => { this.goBack(); }, 1500); } else { console.warn('[Login] 登录失败,服务端返回:', response.data); wx.showToast({ title: response.data && response.data.message || '登录失败', icon: 'none' }); } }, fail: (err) => { this.setData({ isLoading: false }); console.error('[Login] 网络请求失败:', JSON.stringify(err)); console.error('[Login] errMsg:', err.errMsg); console.error('[Login] errno:', err.errno); wx.showToast({ title: '网络请求失败,请重试', icon: 'none' }); }, complete: () => { if (this.data.isLoading) { this.setData({ isLoading: false }); } } }); }, goBack: function () { const pages = getCurrentPages(); if (pages.length > 1) { wx.navigateBack(); } else { wx.switchTab({ url: '/pages/index/index' }); } }, goToAgreement: function () { wx.showModal({ title: '用户协议', content: '用户协议内容:您同意使用本服务即表示同意遵守相关条款...', showCancel: false }); }, goToPrivacy: function () { wx.showModal({ title: '隐私政策', content: '隐私政策内容:我们重视您的隐私,将妥善保管您的个人信息...', showCancel: false }); } });