1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- // app.ts
- import { api_login, api_queryUserInfo } from './api/login';
- const config = require("./config");
- App<IAppOption>({
- globalData: {
- baseUrl: config?.baseUrl,
- appId: '',
- deviceNum: '', // 设备信息
- isLogin: false, // 是否登录
- userInfo: {} as any
- },
- onLaunch() {
- // 展示本地存储能力
- // const logs = wx.getStorageSync('logs') || []
- // logs.unshift(Date.now())
- // wx.setStorageSync('logs', logs)
- this.setAppId();
- // 登录
- wx.login({
- success: async (res) => {
- this.onLogin(res.code)
- },
- })
- },
- setAppId() {
- //获取当前小程序appId
- const accountInfo = wx.getAccountInfoSync();
- this.globalData.appId = accountInfo.miniProgram.appId;
- // wxRequest.config.appid = accountInfo.miniProgram.appId;
- //先设置appid再引入接口文件,防止appid未更新问题
- // require("./utils/request/api.js");
- // 获取设备信息
- const deviceInfo = wx.getDeviceInfo()
- // 品牌 设备型号 操作系统及版本 客户端平台
- const deviceNum = deviceInfo.brand + '-' + deviceInfo.model + '-' + deviceInfo.platform + '-' + deviceInfo.system
- this.globalData.deviceNum = deviceNum
- },
- // userInfoReadyCallback(result) {
- // console.log(result, 'result')
- // }
- /** 微信登录 */
- async onLogin(code: 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: this.globalData.deviceNum,
- extra: '',
- grant_type: 'password',
- loginType: 'WECHAT_MA',
- multiUser: false,
- username: this.globalData.appId,
- password: code
- })
- if(data.code === 200) {
- const userToken = data.data.token_type + " " + data.data.access_token;
- wx.setStorageSync("token", userToken);
- this.globalData.isLogin = true;
- const users = await api_queryUserInfo({ wxAppId: this.globalData.appId })
- if(users.data.code === 200) {
- this.globalData.userInfo = users.data.data
- } else {
- wx.removeStorageSync("token");
- this.globalData.isLogin = false
- }
- console.log(users)
- } else {
- this.globalData.isLogin = false;
- }
- } catch {}
- wx.hideLoading()
- }
- })
|