import { api_login, api_queryUserInfo } from "../../api/login" // pages/login/login.ts const app = getApp() Page({ /** * 页面的初始数据 */ data: { isAgree: false, }, /** * 生命周期函数--监听页面加载 */ onLoad() {}, getLogin(e: any) { const { detail } = e // if(!this.data.isAgree) { // wx.showToast({ // title: '请先阅读并勾选协议', // icon: 'none' // }) // return // } wx.login({ success: (res) => { const params = { code: res.code, iv: detail.iv, encryptedData: detail.encryptedData } this.onLoginSync(params) } }) }, onAgree() { this.setData({ isAgree: !this.data.isAgree }) }, /** 微信登录 */ async onLoginSync(params: { code: string, iv: string, encryptedData: string }) { wx.showLoading({ mask: true, title: "登录中...", }); try { // 开始登录 const { data } = await api_login({ autoRegister: false, client_id: 'cooleshow-student-wxlite', client_secret: 'cooleshow-student-wxlite', deviceNum: app.globalData.deviceNum, extra: JSON.stringify({ ivStr: params.iv, encryptedData: params.encryptedData }), grant_type: 'password', loginType: 'WECHAT_MA', multiUser: false, username: app.globalData.appId, password: params.code }) console.log(data, 'data') if(data.code === 200) { const userToken = data.data.token_type + " " + data.data.access_token; wx.setStorageSync("token", userToken); app.globalData.isLogin = true; const users = await api_queryUserInfo({ wxAppId: app.globalData.appId }) wx.hideLoading() if(users.data.code === 200) { app.globalData.userInfo = users.data.data // 登录成功先跳转到首页 wx.navigateTo({ url: '../index/index', }) } else { wx.removeStorageSync("token"); app.globalData.isLogin = false wx.showToast({ title: users.data.msg, icon: "none" }); } } else { app.globalData.isLogin = false; } } catch(e) { console.log('error', e) wx.hideLoading() } }, onProtocol(e: any) { console.log(e) wx.navigateTo({ url: '../protocol/register' }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })