// app.ts import { api_login, api_queryUserInfo } from "./api/login"; import { api_getOpenId } from "./api/new"; const config = require("./config"); App({ globalData: { // 在客服页面使用 top: 0, // 初始的上偏移 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); }, }); wx.setStorageSync("traceId", this.getRandomKey()) // 获取openId wx.login({ success: async (res) => { await api_getOpenId({ code: res.code, appId: this.globalData.appId }).then((res: any) => { // 存储openId wx.setStorageSync("openId", res.data.data); }) } }) }, getRandomKey() { let key = "" + new Date().getTime() + Math.floor(Math.random() * 1000000); return key; }, 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; // 设置客服初始位置 const systemInfo = wx.getWindowInfo(); this.globalData.top = systemInfo.windowHeight - 180; }, // 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(); }, });