149 lines
4.0 KiB
JavaScript
149 lines
4.0 KiB
JavaScript
|
|
import { injectApp, waitLogin, gatewayHttpClient } from '@jdmini/api'
|
|||
|
|
import request from './utils/request'
|
|||
|
|
|
|||
|
|
App(injectApp()({
|
|||
|
|
globalData: {
|
|||
|
|
userInfo: null,
|
|||
|
|
currentSession: null,
|
|||
|
|
openid: null,
|
|||
|
|
jdToken: null
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
async getQrcode(scene, page = 'pages/game/join/join') {
|
|||
|
|
try {
|
|||
|
|
const response = await gatewayHttpClient.request('/wx/v1/api/app/qrcode', 'POST', {
|
|||
|
|
scene: scene,
|
|||
|
|
page: page,
|
|||
|
|
check_path: false, // 关闭路径检查,因为可能是动态路由
|
|||
|
|
env_version: 'release' // 开发版本
|
|||
|
|
},
|
|||
|
|
{
|
|||
|
|
responseType: 'arraybuffer'
|
|||
|
|
}
|
|||
|
|
)
|
|||
|
|
console.log('二维码API响应:', response)
|
|||
|
|
// 检查响应是否是对象,如果是则可能包含data字段
|
|||
|
|
if (response && typeof response === 'object' && response.data) {
|
|||
|
|
return response.data
|
|||
|
|
}
|
|||
|
|
return response
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('获取二维码失败:', error)
|
|||
|
|
throw error
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
async onLaunch() {
|
|||
|
|
console.log('App启动')
|
|||
|
|
|
|||
|
|
// 等待JD登录完成,获取openid
|
|||
|
|
await this.initJDLogin()
|
|||
|
|
|
|||
|
|
// JD登录完成后,尝试后端登录
|
|||
|
|
//await this.loginToBackend()
|
|||
|
|
// console.log(await this.getQrcode('a=1&b=2'))
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 初始化JD登录,获取openid和token
|
|||
|
|
async initJDLogin() {
|
|||
|
|
try {
|
|||
|
|
// 等待JD登录完成
|
|||
|
|
await waitLogin()
|
|||
|
|
|
|||
|
|
// 获取JD用户信息
|
|||
|
|
const jdToken = wx.getStorageSync('jdwx-token')
|
|||
|
|
const jdUserInfo = wx.getStorageSync('jdwx-userinfo')
|
|||
|
|
|
|||
|
|
if (jdUserInfo && jdUserInfo.openId) {
|
|||
|
|
this.globalData.openid = jdUserInfo.openId
|
|||
|
|
this.globalData.jdToken = jdToken
|
|||
|
|
console.log('JD登录成功,openid:', jdUserInfo.openId)
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('JD登录失败:', error)
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 登录到后端
|
|||
|
|
async loginToBackend() {
|
|||
|
|
try {
|
|||
|
|
if (!this.globalData.openid) {
|
|||
|
|
console.log('没有openid,跳过后端登录')
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('尝试后端登录,openid:', this.globalData.openid)
|
|||
|
|
|
|||
|
|
// 调用后端登录接口(不传userInfo,只用openid查询)
|
|||
|
|
const response = await request.post('/auth/login', {
|
|||
|
|
openid: this.globalData.openid
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
if (response && response.player) {
|
|||
|
|
// 用户已注册,保存用户信息
|
|||
|
|
this.globalData.userInfo = response.player
|
|||
|
|
wx.setStorageSync('userInfo', response.player)
|
|||
|
|
console.log('后端登录成功,用户信息:', response.player)
|
|||
|
|
|
|||
|
|
// 触发登录成功事件,通知首页更新
|
|||
|
|
this.triggerLoginSuccess()
|
|||
|
|
return true
|
|||
|
|
} else if (response && response.exists === false) {
|
|||
|
|
// 用户未注册(后端返回 player: null, exists: false)
|
|||
|
|
console.log('用户未注册,需要完善信息')
|
|||
|
|
return false
|
|||
|
|
} else {
|
|||
|
|
// 其他情况
|
|||
|
|
console.log('登录响应异常:', response)
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
} catch (error) {
|
|||
|
|
console.error('后端登录失败:', error)
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 触发登录成功事件
|
|||
|
|
triggerLoginSuccess() {
|
|||
|
|
this.refreshIndexPage()
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 刷新首页数据
|
|||
|
|
refreshIndexPage() {
|
|||
|
|
// 获取所有页面
|
|||
|
|
const pages = getCurrentPages()
|
|||
|
|
if (pages.length > 0) {
|
|||
|
|
// 查找首页
|
|||
|
|
const indexPage = pages.find(page => page.route === 'pages/index/index')
|
|||
|
|
if (indexPage && typeof indexPage.loadUserInfo === 'function') {
|
|||
|
|
console.log('触发首页刷新')
|
|||
|
|
indexPage.loadUserInfo()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 获取当前用户信息
|
|||
|
|
getUserInfo() {
|
|||
|
|
return wx.getStorageSync('userInfo')
|
|||
|
|
},
|
|||
|
|
getUserOpenid() {
|
|||
|
|
return this.globalData.openid
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 设置用户信息
|
|||
|
|
setUserInfo(userInfo) {
|
|||
|
|
this.globalData.userInfo = userInfo
|
|||
|
|
wx.setStorageSync('userInfo', userInfo)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 更新当前牌局
|
|||
|
|
setCurrentSession(session) {
|
|||
|
|
this.globalData.currentSession = session
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
// 获取当前牌局
|
|||
|
|
getCurrentSession() {
|
|||
|
|
return this.globalData.currentSession
|
|||
|
|
}
|
|||
|
|
}))
|