|
@@ -1,53 +1,98 @@
|
|
|
+import { api_login, api_queryUserInfo } from "../../api/login"
|
|
|
+
|
|
|
// pages/login/login.ts
|
|
|
+const app = getApp<IAppOption>()
|
|
|
Page({
|
|
|
-
|
|
|
/**
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
- code: '',
|
|
|
isAgree: false,
|
|
|
},
|
|
|
-
|
|
|
/**
|
|
|
* 生命周期函数--监听页面加载
|
|
|
*/
|
|
|
- onLoad() {
|
|
|
- // 登录
|
|
|
+ onLoad() {},
|
|
|
+ getLogin(e: any) {
|
|
|
+ console.log(e, 'any')
|
|
|
+ const { detail } = e
|
|
|
+ // if(!this.data.isAgree) {
|
|
|
+ // wx.showToast({
|
|
|
+ // title: '请先阅读并勾选协议',
|
|
|
+ // icon: 'none'
|
|
|
+ // })
|
|
|
+ // return
|
|
|
+ // }
|
|
|
wx.login({
|
|
|
- success: res => {
|
|
|
- // 发送 res.code 到后台换取 openId, sessionKey, unionId
|
|
|
- this.setData({
|
|
|
- code: res.code
|
|
|
- })
|
|
|
- },
|
|
|
- })
|
|
|
- },
|
|
|
- onLogin() {
|
|
|
- if(!this.data.isAgree) {
|
|
|
- wx.showToast({
|
|
|
- title: '请先阅读并勾选协议',
|
|
|
- icon: 'none'
|
|
|
- })
|
|
|
- return
|
|
|
- }
|
|
|
- // 登录
|
|
|
- wx.getUserProfile({
|
|
|
- desc: '获取用户信息',
|
|
|
success: (res) => {
|
|
|
- console.log(res, 'res')
|
|
|
-
|
|
|
- },
|
|
|
- fail: (res) => {
|
|
|
- console.log(res, 'res')
|
|
|
+ const params = {
|
|
|
+ code: res.code,
|
|
|
+ iv: detail.iv,
|
|
|
+ encryptedData: detail.encryptedData
|
|
|
+ }
|
|
|
+ this.onLoginSync(params)
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
- onAgree(e: any) {
|
|
|
+ onAgree() {
|
|
|
this.setData({
|
|
|
isAgree: !this.data.isAgree
|
|
|
})
|
|
|
},
|
|
|
+ /** 微信登录 */
|
|
|
+ async onLoginSync(params: { code: string, iv: string, encryptedData: string }) {
|
|
|
+ try {
|
|
|
+ // 开始登录
|
|
|
+ console.log({
|
|
|
+ autoRegister: false,
|
|
|
+ client_id: 'cooleshow-student-wxlite',
|
|
|
+ client_secret: 'cooleshow-student-wxlite',
|
|
|
+ deviceNum: app.globalData.deviceNum,
|
|
|
+ extra: JSON.stringify({
|
|
|
+ iv: params.iv,
|
|
|
+ encryptedData: params.encryptedData
|
|
|
+ }),
|
|
|
+ grant_type: 'password',
|
|
|
+ loginType: 'WECHAT_MA',
|
|
|
+ multiUser: false,
|
|
|
+ username: app.globalData.appId,
|
|
|
+ password: params.code
|
|
|
+ })
|
|
|
+ const { data } = await api_login({
|
|
|
+ autoRegister: false,
|
|
|
+ client_id: 'cooleshow-student-wxlite',
|
|
|
+ client_secret: 'cooleshow-student-wxlite',
|
|
|
+ deviceNum: app.globalData.deviceNum,
|
|
|
+ extra: JSON.stringify({
|
|
|
+ iv: params.iv,
|
|
|
+ encryptedData: params.encryptedData
|
|
|
+ }),
|
|
|
+ grant_type: 'password',
|
|
|
+ loginType: 'WECHAT_MA',
|
|
|
+ multiUser: false,
|
|
|
+ username: app.globalData.appId,
|
|
|
+ password: params.code
|
|
|
+ })
|
|
|
+
|
|
|
+ if(data.code === 200) {
|
|
|
+ const userToken = data.data.authentication.token_type + " " + data.data.authentication.access_token;
|
|
|
+ wx.setStorageSync("token", userToken);
|
|
|
+ app.globalData.isLogin = true;
|
|
|
+ const users = await api_queryUserInfo({ wxAppId: app.globalData.appId })
|
|
|
+ if(users.data.code === 200) {
|
|
|
+ app.globalData.userInfo = users.data.data
|
|
|
+ } else {
|
|
|
+ wx.removeStorageSync("token");
|
|
|
+ app.globalData.isLogin = false
|
|
|
+ }
|
|
|
+ console.log(users)
|
|
|
+ } else {
|
|
|
+ app.globalData.isLogin = false;
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ console.log('error')
|
|
|
+ }
|
|
|
+ },
|
|
|
/**
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
*/
|