123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- import { api_login, api_queryUserInfo } from "../../api/login"
- // pages/login/login.ts
- const app = getApp<IAppOption>()
- 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() {
- }
- })
|