app.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // app.ts
  2. import { api_login, api_queryUserInfo } from './api/login';
  3. const config = require("./config");
  4. App<IAppOption>({
  5. globalData: {
  6. baseUrl: config?.baseUrl,
  7. appId: '',
  8. deviceNum: '', // 设备信息
  9. isLogin: false, // 是否登录
  10. userInfo: {} as any,
  11. isTablet: false, // 是否为平板
  12. },
  13. onLaunch() {
  14. // 展示本地存储能力
  15. // const logs = wx.getStorageSync('logs') || []
  16. // logs.unshift(Date.now())
  17. // wx.setStorageSync('logs', logs)
  18. this.setAppId();
  19. // 登录
  20. wx.login({
  21. success: async (res) => {
  22. this.onLogin(res.code)
  23. },
  24. })
  25. console.log("是否为平板设备:", this.isTablet());
  26. this.globalData.isTablet = this.isTablet() as any;
  27. },
  28. setAppId() {
  29. //获取当前小程序appId
  30. const accountInfo = wx.getAccountInfoSync();
  31. this.globalData.appId = accountInfo.miniProgram.appId;
  32. // wxRequest.config.appid = accountInfo.miniProgram.appId;
  33. //先设置appid再引入接口文件,防止appid未更新问题
  34. // require("./utils/request/api.js");
  35. // 获取设备信息
  36. const deviceInfo = wx.getDeviceInfo()
  37. // 品牌 设备型号 操作系统及版本 客户端平台
  38. const deviceNum = deviceInfo.brand + '-' + deviceInfo.model + '-' + deviceInfo.platform + '-' + deviceInfo.system
  39. this.globalData.deviceNum = deviceNum
  40. },
  41. // userInfoReadyCallback(result) {
  42. // console.log(result, 'result')
  43. // }
  44. /** 微信登录 */
  45. async onLogin(code: string) {
  46. wx.showLoading({
  47. mask: true,
  48. title: "加载中...",
  49. });
  50. try {
  51. // 开始登录
  52. const { data } = await api_login({
  53. autoRegister: false,
  54. client_id: 'cooleshow-student-wxlite',
  55. client_secret: 'cooleshow-student-wxlite',
  56. deviceNum: this.globalData.deviceNum,
  57. extra: '',
  58. grant_type: 'password',
  59. loginType: 'WECHAT_MA',
  60. multiUser: false,
  61. username: this.globalData.appId,
  62. password: code
  63. })
  64. if (data.code === 200) {
  65. const userToken = data.data.token_type + " " + data.data.access_token;
  66. wx.setStorageSync("token", userToken);
  67. this.globalData.isLogin = true;
  68. const users = await api_queryUserInfo({ wxAppId: this.globalData.appId })
  69. if (users.data.code === 200) {
  70. this.globalData.userInfo = users.data.data
  71. } else {
  72. wx.removeStorageSync("token");
  73. this.globalData.isLogin = false
  74. }
  75. // console.log(users)
  76. } else {
  77. this.globalData.isLogin = false;
  78. }
  79. } catch { }
  80. wx.hideLoading()
  81. },
  82. // 判断逻辑
  83. isTablet() {
  84. const systemInfo = wx.getDeviceInfo();
  85. const { model, platform } = systemInfo;
  86. // console.log(systemInfo, 'systemInfo')
  87. const wxWindowInfo = wx.getWindowInfo()
  88. const { screenHeight, screenWidth } = wxWindowInfo
  89. // console.log({
  90. // model,
  91. // platform,
  92. // screenHeight,
  93. // screenWidth
  94. // }, wxWindowInfo)
  95. // 1. 通过设备型号判断(iPad 或 Android 平板)
  96. if (
  97. model.includes("iPad") || // iPad
  98. model.toLowerCase().includes("pad") || // 华为平板、小米平板等
  99. (platform === "android" && Math.max(screenWidth, screenHeight) >= 900) // 大屏 Android 设备
  100. ) {
  101. return true;
  102. }
  103. const isIPad = /^iPad/.test(model) && platform === 'ios';
  104. const isAndroidPad = screenWidth >= 768 && /pad|tablet/i.test(model);
  105. const isPad = isIPad || isAndroidPad;
  106. // 2. 通过屏幕宽高比判断(平板通常宽高比接近 4:3 或 3:4)
  107. // const aspectRatio = screenWidth / screenHeight;
  108. // const isTabletRatio = aspectRatio <= 0.75 || aspectRatio >= 1.33;
  109. return isPad;
  110. }
  111. })