app.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // app.js
  2. import {
  3. loginLog,
  4. androidLogin,
  5. getMyInfo,
  6. bindDevice
  7. } from '~/api/user'
  8. import {
  9. createStoreBindings
  10. } from 'mobx-miniprogram-bindings'
  11. import {
  12. store
  13. } from '~/store/index'
  14. let storeBindings
  15. App({
  16. async onLaunch() {
  17. // #if ANDROID
  18. wx.setStorageSync('channelCode', '3016')
  19. this.initPlugin()
  20. // #endif
  21. this.getNavbarInfo()
  22. await loginLog()
  23. },
  24. async onShow(options) {
  25. if (!this.storeBindings) {
  26. this.storeBindings = createStoreBindings(this, {
  27. store,
  28. actions: ['setUser']
  29. })
  30. }
  31. this.login()
  32. },
  33. async login() {
  34. let uid = wx.getStorageSync('uid')
  35. if (uid) {
  36. let userInfo = await getMyInfo()
  37. console.log(userInfo,'zzz');
  38. this.setUser(userInfo)
  39. this.globalData.userInfo = userInfo
  40. if (getApp().callBack) {
  41. getApp().callBack();
  42. }
  43. } else {
  44. let userRes = await androidLogin({extOpenId:'1234567',channel:'3016',grade:'PRIMARY_FIRST_GRADE'})
  45. console.log(userRes,'455');
  46. this.setUser(userRes)
  47. wx.setStorageSync('uid', userRes.uid)
  48. wx.setStorageSync('user', userRes)
  49. this.globalData.userInfo = userRes
  50. if (getApp().callBack) {
  51. getApp().callBack(userRes);
  52. }
  53. }
  54. },
  55. getNavbarInfo() {
  56. // 获取系统信息
  57. const systemInfo = wx.getSystemInfoSync();
  58. // 胶囊按钮位置信息
  59. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  60. // 导航栏高度 = 状态栏高度 + 44
  61. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  62. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  63. this.globalData.menuTop = menuButtonInfo.top;
  64. this.globalData.menuHeight = menuButtonInfo.height;
  65. // 设备显示区域的宽度,单位px
  66. this.globalData.windowWidth = systemInfo.windowWidth
  67. this.globalData.windowHeight = systemInfo.windowHeight
  68. },
  69. initPlugin() {
  70. let miniAppPluginId = 'wxf1a06dafa3350688'
  71. //根据模块ID初始化模块
  72. console.log("插件ID:" + miniAppPluginId)
  73. wx.miniapp.loadNativePlugin({
  74. pluginId: miniAppPluginId,
  75. success: (plugin) => {
  76. this.globalData.plugin = plugin
  77. console.log('load plugin success', plugin)
  78. this.globalData.plugin.initPlugin(({}), (res) => {
  79. if (res === "success") {
  80. console.log("初始化plugin成功")
  81. } else {
  82. console.log("初始化plugin失败")
  83. }
  84. })
  85. },
  86. fail: (e) => {
  87. console.log('load plugin fail', e)
  88. }
  89. })
  90. },
  91. globalData: {
  92. userInfo: null,
  93. isIOS: false, // 判断设备是否为苹果
  94. navBarHeight: 0, // 导航栏高度
  95. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  96. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  97. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  98. windowWidth: 0,
  99. windowHeight: 0,
  100. plugin: {}
  101. }
  102. })