123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- // 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,
- isTablet: false, // 是否为平板
- },
- 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)
- },
- })
- console.log("是否为平板设备:", this.isTablet());
- this.globalData.isTablet = this.isTablet() as any;
- },
- 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()
- },
- // 判断逻辑
- isTablet() {
- const systemInfo = wx.getDeviceInfo();
- const { model, platform } = systemInfo;
- // console.log(systemInfo, 'systemInfo')
- const wxWindowInfo = wx.getWindowInfo()
- const { screenHeight, screenWidth } = wxWindowInfo
- // console.log({
- // model,
- // platform,
- // screenHeight,
- // screenWidth
- // }, wxWindowInfo)
- // 1. 通过设备型号判断(iPad 或 Android 平板)
- if (
- model.includes("iPad") || // iPad
- model.toLowerCase().includes("pad") || // 华为平板、小米平板等
- (platform === "android" && Math.max(screenWidth, screenHeight) >= 900) // 大屏 Android 设备
- ) {
- return true;
- }
- const isIPad = /^iPad/.test(model) && platform === 'ios';
- const isAndroidPad = screenWidth >= 768 && /pad|tablet/i.test(model);
- const isPad = isIPad || isAndroidPad;
- // 2. 通过屏幕宽高比判断(平板通常宽高比接近 4:3 或 3:4)
- // const aspectRatio = screenWidth / screenHeight;
- // const isTabletRatio = aspectRatio <= 0.75 || aspectRatio >= 1.33;
- return isPad;
- }
- })
|