app.ts 3.8 KB

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