app.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // app.ts
  2. import api from "./api/index";
  3. App<IAppOption>({
  4. globalData: {
  5. getUserLoad: false
  6. },
  7. onLaunch() {
  8. // 登录
  9. const nowTime = Date.now() - (wx.getStorageSync("tokenTime") || 0) >= 86400000 * 7;
  10. (!wx.getStorageSync("token") || nowTime) && this.wLogin();
  11. // 获取用户手机号
  12. this.globalData.phone = wx.getStorageSync("userphone") || "";
  13. // 获取授权内容
  14. wx.getSetting({
  15. success: res => {
  16. this.globalData.setting = res ? res.authSetting : {}
  17. }
  18. });
  19. this.globalData.SystemInfo = wx.getSystemInfoSync();
  20. this.globalData.FileSystemManager = wx.getFileSystemManager();
  21. console.log(this.globalData.SystemInfo.SDKVersion);
  22. },
  23. wLogin() {
  24. wx.login({
  25. fail: err => {
  26. console.log(err)
  27. },
  28. success: res => {
  29. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  30. this.login({
  31. data: {
  32. code: res.code
  33. },
  34. success: (res: any) => {
  35. wx.setStorageSync("token", res.token);
  36. wx.setStorageSync("tokenTime", Date.now());
  37. }
  38. })
  39. },
  40. })
  41. },
  42. ...api
  43. })