app.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // app.ts
  2. import { api_login, api_queryUserInfo } from "./api/login";
  3. import { api_getOpenId } from "./api/new";
  4. const config = require("./config");
  5. App<IAppOption>({
  6. globalData: {
  7. // 在客服页面使用
  8. top: 0, // 初始的上偏移
  9. baseUrl: config?.baseUrl,
  10. appId: "",
  11. deviceNum: "", // 设备信息
  12. isLogin: false, // 是否登录
  13. userInfo: {} as any
  14. },
  15. onLaunch() {
  16. // 展示本地存储能力
  17. // const logs = wx.getStorageSync('logs') || []
  18. // logs.unshift(Date.now())
  19. // wx.setStorageSync('logs', logs)
  20. this.setAppId();
  21. // 登录
  22. wx.login({
  23. success: async (res) => {
  24. this.onLogin(res.code);
  25. },
  26. });
  27. wx.setStorageSync("traceId", this.getRandomKey())
  28. // 获取openId
  29. wx.login({
  30. success: async (res) => {
  31. await api_getOpenId({
  32. code: res.code,
  33. appId: this.globalData.appId
  34. }).then((res: any) => {
  35. // 存储openId
  36. wx.setStorageSync("openId", res.data.data);
  37. })
  38. }
  39. })
  40. },
  41. getRandomKey() {
  42. let key = "" + new Date().getTime() + Math.floor(Math.random() * 1000000);
  43. return key;
  44. },
  45. setAppId() {
  46. //获取当前小程序appId
  47. const accountInfo = wx.getAccountInfoSync();
  48. this.globalData.appId = accountInfo.miniProgram.appId;
  49. // wxRequest.config.appid = accountInfo.miniProgram.appId;
  50. //先设置appid再引入接口文件,防止appid未更新问题
  51. // require("./utils/request/api.js");
  52. // 获取设备信息
  53. const deviceInfo = wx.getDeviceInfo();
  54. // 品牌 设备型号 操作系统及版本 客户端平台
  55. const deviceNum =
  56. deviceInfo.brand +
  57. "-" +
  58. deviceInfo.model +
  59. "-" +
  60. deviceInfo.platform +
  61. "-" +
  62. deviceInfo.system;
  63. this.globalData.deviceNum = deviceNum;
  64. // 设置客服初始位置
  65. const systemInfo = wx.getWindowInfo();
  66. this.globalData.top = systemInfo.windowHeight - 180;
  67. },
  68. // userInfoReadyCallback(result) {
  69. // console.log(result, 'result')
  70. // }
  71. /** 微信登录 */
  72. async onLogin(code: string) {
  73. wx.showLoading({
  74. mask: true,
  75. title: "加载中...",
  76. });
  77. try {
  78. // 开始登录
  79. const { data } = await api_login({
  80. autoRegister: false,
  81. client_id: "cooleshow-student-wxlite",
  82. client_secret: "cooleshow-student-wxlite",
  83. deviceNum: this.globalData.deviceNum,
  84. extra: "",
  85. grant_type: "password",
  86. loginType: "WECHAT_MA",
  87. multiUser: false,
  88. username: this.globalData.appId,
  89. password: code,
  90. });
  91. if (data.code === 200) {
  92. const userToken = data.data.token_type + " " + data.data.access_token;
  93. wx.setStorageSync("token", userToken);
  94. this.globalData.isLogin = true;
  95. const users = await api_queryUserInfo({
  96. wxAppId: this.globalData.appId,
  97. });
  98. if (users.data.code === 200) {
  99. this.globalData.userInfo = users.data.data;
  100. } else {
  101. wx.removeStorageSync("token");
  102. this.globalData.isLogin = false;
  103. }
  104. console.log(users);
  105. } else {
  106. this.globalData.isLogin = false;
  107. }
  108. } catch {}
  109. wx.hideLoading();
  110. },
  111. });