app.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // app.js
  2. import {
  3. loginLog,
  4. userLogin,
  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 MP
  18. wx.setStorageSync('channelCode', '3001')
  19. this.updateApplet()
  20. // #elif ANDROID
  21. wx.setStorageSync('channelCode', '3001')
  22. this.initPlugin()
  23. // #endif
  24. this.getNavbarInfo()
  25. await loginLog()
  26. },
  27. async onShow(options) {
  28. if (!this.storeBindings) {
  29. this.storeBindings = createStoreBindings(this, {
  30. store,
  31. actions: ['setUser']
  32. })
  33. }
  34. let {
  35. path,
  36. scene,
  37. query
  38. } = wx.getEnterOptionsSync()
  39. //判断是不是扫海报进入
  40. if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
  41. let params = decodeURIComponent(query.scene).split('&')
  42. if (params.length == 1) {
  43. this.login(params[0])
  44. } else {
  45. this.login()
  46. }
  47. } else {
  48. let shareUid = options.query.uid || ''
  49. let userChannelCode = '3001'
  50. // console.log("朋友圈广告进入");
  51. if ([1045, 1046, 1084].includes(scene)) {
  52. userChannelCode = '4001'
  53. }
  54. this.login(shareUid, userChannelCode)
  55. }
  56. },
  57. async login(shareUid, userChannelCode = '3001') {
  58. let uid = wx.getStorageSync('uid')
  59. if (uid) {
  60. let userInfo = await getMyInfo()
  61. this.setUser(userInfo.user)
  62. this.globalData.userInfo = userInfo.user
  63. this.deviceLogin()
  64. if (getApp().callBack) {
  65. getApp().callBack();
  66. }
  67. } else {
  68. // #if MP
  69. this.getWXCode().then(async res => {
  70. if (res.code) {
  71. // 获取openid
  72. let data = {
  73. code: res.code,
  74. userChannelCode
  75. }
  76. if (shareUid != 'undefined' && shareUid) {
  77. data.shareUid = shareUid
  78. }
  79. let userRes = await userLogin(data)
  80. this.setUser(userRes.data)
  81. wx.setStorageSync('uid', userRes.data.uid)
  82. wx.setStorageSync('user', userRes.data)
  83. this.globalData.userInfo = userRes.data
  84. this.deviceLogin()
  85. if (getApp().callBack) {
  86. getApp().callBack(userRes);
  87. }
  88. }
  89. })
  90. // #elif ANDROID
  91. this.setUser({
  92. grade: 'PRIMARY_FIRST_GRADE'
  93. })
  94. // #endif
  95. }
  96. },
  97. getWXCode() {
  98. return new Promise((reslove, reject) => {
  99. wx.login({
  100. success: async (res) => {
  101. if (res.code) {
  102. reslove(res)
  103. } else {
  104. reject(res.errMsg)
  105. }
  106. }
  107. })
  108. })
  109. },
  110. // 音箱端登录
  111. deviceLogin() {
  112. // #if MP
  113. let {
  114. path,
  115. scene,
  116. query
  117. } = wx.getEnterOptionsSync()
  118. // 1047 扫描小程序码 1048长按图片识别小程序码
  119. var gradeObj = Object.keys({
  120. "PRESCHOOL": "学前班",
  121. "PRIMARY_FIRST_GRADE": "一年级",
  122. "PRIMARY_SECOND_GRADE": "二年级",
  123. "PRIMARY_THREE_GRADE": "三年级",
  124. "PRIMARY_SENIOR_GRADE": "四年级",
  125. "PRIMARY_FIVE_GRADE": "五年级",
  126. "PRIMARY_SIX_GRADE": "六年级",
  127. })
  128. if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
  129. let params = decodeURIComponent(query.scene).split('&')
  130. if (params.length > 1) {
  131. bindDevice({
  132. deviceCode: params[0],
  133. channelCode: params[1],
  134. grade: gradeObj[params[2]],
  135. uid: wx.getStorageSync('uid')
  136. }).then(res => {
  137. console.log(res);
  138. })
  139. }
  140. }
  141. // #endif
  142. },
  143. getNavbarInfo() {
  144. // 获取系统信息
  145. const systemInfo = wx.getSystemInfoSync();
  146. // 胶囊按钮位置信息
  147. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  148. // 导航栏高度 = 状态栏高度 + 44
  149. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  150. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  151. this.globalData.menuTop = menuButtonInfo.top;
  152. this.globalData.menuHeight = menuButtonInfo.height;
  153. // 设备显示区域的宽度,单位px
  154. this.globalData.windowWidth = systemInfo.windowWidth
  155. this.globalData.windowHeight = systemInfo.windowHeight
  156. },
  157. updateApplet() {
  158. // 获取小程序更新机制兼容
  159. if (wx.canIUse('getUpdateManager')) {
  160. const updateManager = wx.getUpdateManager()
  161. updateManager.onCheckForUpdate(function (res) {
  162. // 请求完新版本信息的回调
  163. if (res.hasUpdate) {
  164. updateManager.onUpdateReady(function () {
  165. wx.showModal({
  166. title: '更新提示',
  167. content: '新版本已经准备好,是否重启应用?',
  168. success: function (res) {
  169. if (res.confirm) {
  170. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  171. updateManager.applyUpdate()
  172. }
  173. }
  174. })
  175. })
  176. updateManager.onUpdateFailed(function () {
  177. // 新的版本下载失败
  178. wx.showModal({
  179. title: '已经有新版本了哟~',
  180. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  181. })
  182. })
  183. }
  184. })
  185. }
  186. },
  187. initPlugin() {
  188. let miniAppPluginId = 'wxf1a06dafa3350688'
  189. //根据模块ID初始化模块
  190. console.log("插件ID:" + miniAppPluginId)
  191. wx.miniapp.loadNativePlugin({
  192. pluginId: miniAppPluginId,
  193. success: (plugin) => {
  194. this.globalData.plugin = plugin
  195. console.log('load plugin success', plugin)
  196. this.globalData.plugin.initPlugin(({}), (res) => {
  197. if (res === "success") {
  198. console.log("初始化plugin成功")
  199. } else {
  200. console.log("初始化plugin失败")
  201. }
  202. })
  203. },
  204. fail: (e) => {
  205. console.log('load plugin fail', e)
  206. }
  207. })
  208. },
  209. globalData: {
  210. userInfo: null,
  211. isIOS: false, // 判断设备是否为苹果
  212. navBarHeight: 0, // 导航栏高度
  213. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  214. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  215. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  216. windowWidth: 0,
  217. windowHeight: 0,
  218. plugin: {}
  219. }
  220. })