app.js 3.4 KB

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