| | |
| | | } |
| | | |
| | | console.log('=== 准备调用后端wxLogin接口 ===') |
| | | console.log('请求URL:', this.globalData.baseUrl) |
| | | console.log('请求URL:', 'http://localhost:8080/api/auth/wx-login') |
| | | console.log('设备信息:', deviceInfo) |
| | | console.log('请求参数:', requestData) |
| | | console.log('请求开始时间:', new Date().toISOString()) |
| | | |
| | | wx.request({ |
| | | url: this.globalData.baseUrl, |
| | | url: 'http://localhost:8080/api/auth/wx-login', |
| | | method: 'POST', |
| | | header: { |
| | | 'Content-Type': 'application/json' |
| | | }, |
| | | data: { |
| | | query: ` |
| | | mutation wxLogin($input: WxLoginRequest!) { |
| | | wxLogin(input: $input) { |
| | | token |
| | | userInfo { |
| | | userId |
| | | name |
| | | phone |
| | | userType |
| | | employee { |
| | | id |
| | | name |
| | | } |
| | | judge { |
| | | id |
| | | name |
| | | } |
| | | player { |
| | | id |
| | | name |
| | | } |
| | | } |
| | | isNewUser |
| | | loginRecordId |
| | | sessionKey |
| | | } |
| | | } |
| | | `, |
| | | variables: { |
| | | input: requestData |
| | | } |
| | | }, |
| | | data: requestData, |
| | | success: (res) => { |
| | | console.log('=== 后端wxLogin接口响应 ===') |
| | | console.log('响应时间:', new Date().toISOString()) |
| | |
| | | return |
| | | } |
| | | |
| | | if (res.data.errors) { |
| | | console.error('❌ GraphQL错误:', res.data.errors) |
| | | // 检查是否有错误信息(适配不同的错误响应格式) |
| | | if (res.data.error || res.data.message || res.data.success === false) { |
| | | const errorMsg = res.data.error || res.data.message || '登录失败' |
| | | console.error('❌ 登录失败:', errorMsg) |
| | | wx.showToast({ |
| | | title: '登录失败', |
| | | title: errorMsg, |
| | | icon: 'error' |
| | | }) |
| | | return |
| | | } |
| | | |
| | | if (res.data.data && res.data.data.wxLogin) { |
| | | const loginResult = res.data.data.wxLogin |
| | | // 检查响应数据格式:支持直接返回WxLoginResponse或包装格式 |
| | | let loginResult = null |
| | | if (res.data.token && res.data.userInfo) { |
| | | // 直接返回WxLoginResponse格式 |
| | | loginResult = res.data |
| | | } else if (res.data.success && res.data.data) { |
| | | // 包装格式 {success: true, data: WxLoginResponse} |
| | | loginResult = res.data.data |
| | | } |
| | | |
| | | if (loginResult) { |
| | | |
| | | console.log('✅ 登录成功!') |
| | | console.log('用户ID:', loginResult.userInfo.userId) |
| | |
| | | console.log('ℹ️ 老用户或已拒绝手机号授权,跳过授权弹窗') |
| | | } |
| | | } else { |
| | | console.error('❌ 响应数据格式错误,缺少wxLogin字段') |
| | | console.error('❌ 响应数据格式错误,缺少必要字段(token或userInfo)') |
| | | console.error('实际响应:', res.data) |
| | | wx.showToast({ |
| | | title: '登录失败', |
| | | title: '登录数据格式错误', |
| | | icon: 'error' |
| | | }) |
| | | } |
| | |
| | | // GraphQL请求封装 |
| | | graphqlRequest(query, variables = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | // 确保token的一致性:优先使用globalData中的token,如果没有则从storage获取 |
| | | let token = this.globalData.token |
| | | if (!token) { |
| | | token = wx.getStorageSync('token') |
| | | if (token) { |
| | | this.globalData.token = token // 同步到globalData |
| | | } |
| | | } |
| | | |
| | | wx.request({ |
| | | url: this.globalData.baseUrl, |
| | | method: 'POST', |
| | | header: { |
| | | 'Content-Type': 'application/json', |
| | | 'Authorization': this.globalData.token ? `Bearer ${this.globalData.token}` : '' |
| | | 'Authorization': token ? `Bearer ${token}` : '' |
| | | }, |
| | | data: { |
| | | query: query, |
| | |
| | | }, |
| | | success: (res) => { |
| | | console.log('GraphQL响应:', res.data) |
| | | |
| | | // 检查HTTP状态码 |
| | | if (res.statusCode !== 200) { |
| | | console.error('GraphQL HTTP错误:', res.statusCode) |
| | | reject(new Error(`HTTP错误: ${res.statusCode}`)) |
| | | return |
| | | } |
| | | |
| | | // 检查GraphQL错误 |
| | | if (res.data.errors) { |
| | | console.error('GraphQL错误:', res.data.errors) |
| | | reject(res.data.errors) |
| | | } else if (res.data.data) { |
| | | reject(new Error(res.data.errors[0]?.message || 'GraphQL请求错误')) |
| | | return |
| | | } |
| | | |
| | | // 检查数据 |
| | | if (res.data.data !== undefined) { |
| | | resolve(res.data.data) |
| | | } else { |
| | | console.error('GraphQL响应异常:', res.data) |
| | | reject('请求失败') |
| | | reject(new Error('GraphQL响应数据异常')) |
| | | } |
| | | }, |
| | | fail: (err) => { |
| | | reject(err) |
| | | console.error('GraphQL网络请求失败:', err) |
| | | reject(new Error('网络请求失败')) |
| | | } |
| | | }) |
| | | }) |