app.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.user)
  39. this.globalData.userInfo = userInfo.user
  40. if (getApp().callBack) {
  41. getApp().callBack(userInfo.users);
  42. }
  43. } else {
  44. let userRes = await androidLogin({extOpenId:'1234567',channel:'3016',grade:'PRIMARY_FIRST_GRADE'})
  45. this.setUser(userRes.user)
  46. wx.setStorageSync('uid', userRes.user.uid)
  47. wx.setStorageSync('user', userRes.user)
  48. this.globalData.userInfo = userRes.user
  49. if (getApp().callBack) {
  50. getApp().callBack(userRes.user);
  51. }
  52. }
  53. },
  54. getNavbarInfo() {
  55. // 获取系统信息
  56. const systemInfo = wx.getSystemInfoSync();
  57. // 胶囊按钮位置信息
  58. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  59. // 导航栏高度 = 状态栏高度 + 44
  60. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  61. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  62. this.globalData.menuTop = menuButtonInfo.top;
  63. this.globalData.menuHeight = menuButtonInfo.height;
  64. // 设备显示区域的宽度,单位px
  65. this.globalData.windowWidth = systemInfo.windowWidth
  66. this.globalData.windowHeight = systemInfo.windowHeight
  67. },
  68. initPlugin() {
  69. let miniAppPluginId = 'wxf1a06dafa3350688'
  70. //根据模块ID初始化模块
  71. console.log("插件ID:" + miniAppPluginId)
  72. wx.miniapp.loadNativePlugin({
  73. pluginId: miniAppPluginId,
  74. success: (plugin) => {
  75. this.globalData.plugin = plugin
  76. console.log('load plugin success', plugin)
  77. this.globalData.plugin.initPlugin(({}), (res) => {
  78. if (res === "success") {
  79. console.log("初始化plugin成功")
  80. } else {
  81. console.log("初始化plugin失败")
  82. }
  83. })
  84. },
  85. fail: (e) => {
  86. console.log('load plugin fail', e)
  87. }
  88. })
  89. },
  90. globalData: {
  91. userInfo: null,
  92. isIOS: false, // 判断设备是否为苹果
  93. navBarHeight: 0, // 导航栏高度
  94. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  95. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  96. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  97. windowWidth: 0,
  98. windowHeight: 0,
  99. plugin: {}
  100. }
  101. })