app.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. this.updateApplet()
  18. this.checkIsIos()
  19. this.getNavbarInfo()
  20. await loginLog()
  21. },
  22. async onShow(options) {
  23. if (!this.storeBindings) {
  24. this.storeBindings = createStoreBindings(this, {
  25. store,
  26. actions: ['setUser']
  27. })
  28. }
  29. let {
  30. path,
  31. scene,
  32. query
  33. } = wx.getEnterOptionsSync()
  34. //判断是不是扫海报进入
  35. if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
  36. let params = decodeURIComponent(query.scene).split('&')
  37. if (params.length == 1) {
  38. this.login(params[0])
  39. } else {
  40. this.login()
  41. }
  42. } else {
  43. let shareUid = options.query.uid || ''
  44. let userChannelCode='3001'
  45. if ([1045, 1046, 1084].includes(scene)) {
  46. console.log("朋友圈广告进入");
  47. userChannelCode='4001'
  48. }
  49. this.login(shareUid,userChannelCode)
  50. }
  51. },
  52. async login(shareUid,userChannelCode='3001') {
  53. let uid = wx.getStorageSync('uid')
  54. if (uid) {
  55. let userInfo = await getMyInfo()
  56. this.setUser(userInfo.user)
  57. this.globalData.userInfo = userInfo.user
  58. this.deviceLogin()
  59. if (getApp().callBack) {
  60. getApp().callBack();
  61. }
  62. } else {
  63. wx.login({
  64. success: async (res) => {
  65. if (res.code) {
  66. // 获取openid
  67. let data = {
  68. code: res.code,
  69. userChannelCode
  70. }
  71. if (shareUid != 'undefined' && shareUid) {
  72. data.shareUid = shareUid
  73. }
  74. let userRes = await userLogin(data)
  75. this.setUser(userRes.data)
  76. wx.setStorageSync('uid', userRes.data.uid)
  77. wx.setStorageSync('user', userRes.data)
  78. this.globalData.userInfo = userRes.data
  79. this.deviceLogin()
  80. if (getApp().callBack) {
  81. getApp().callBack(userRes);
  82. }
  83. }
  84. }
  85. })
  86. }
  87. },
  88. checkIsIos: function () {
  89. wx.getSystemInfo({
  90. success: (res) => {
  91. // 设备显示区域的宽度,单位px
  92. this.globalData.windowWidth = res.windowWidth
  93. this.globalData.windowHeight = res.windowHeight
  94. if (res.system.search('iOS') != -1) {
  95. this.globalData.isIOS = true
  96. }
  97. let {
  98. scene
  99. } = wx.getLaunchOptionsSync()
  100. // 1023 安卓系统桌面图标,1104微信聊天主界面下拉,「我的小程序」栏(基础库2.2.4-2.29.0版本废弃,2.29.1版本起生效)
  101. if (scene != 1023) {
  102. let preTime = wx.getStorageSync('preDesktopTime')
  103. let flag = !preTime ? true : (new Date() - preTime) / 43200000 > 1 ? true : false
  104. if (flag || !preTime) {
  105. this.globalData.desktopTips = true
  106. wx.setStorage({
  107. key: "preDesktopTime",
  108. data: new Date()
  109. })
  110. }
  111. }
  112. }
  113. })
  114. },
  115. // 音箱端登录
  116. deviceLogin() {
  117. let {
  118. path,
  119. scene,
  120. query
  121. } = wx.getEnterOptionsSync()
  122. // 1047 扫描小程序码 1048长按图片识别小程序码
  123. var gradeObj = Object.keys({
  124. "PRESCHOOL": "学前班",
  125. "PRIMARY_FIRST_GRADE": "一年级",
  126. "PRIMARY_SECOND_GRADE": "二年级",
  127. "PRIMARY_THREE_GRADE": "三年级",
  128. "PRIMARY_SENIOR_GRADE": "四年级",
  129. "PRIMARY_FIVE_GRADE": "五年级",
  130. "PRIMARY_SIX_GRADE": "六年级",
  131. })
  132. if (query.scene && [1047, 1048].includes(scene) && path == 'pages/index/index') {
  133. let params = decodeURIComponent(query.scene).split('&')
  134. if (params.length > 1) {
  135. bindDevice({
  136. deviceCode: params[0],
  137. channelCode: params[1],
  138. grade: gradeObj[params[2]],
  139. uid: wx.getStorageSync('uid')
  140. }).then(res => {
  141. console.log(res);
  142. })
  143. }
  144. }
  145. },
  146. getNavbarInfo() {
  147. // 获取系统信息
  148. const systemInfo = wx.getSystemInfoSync();
  149. // 胶囊按钮位置信息
  150. const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
  151. // 导航栏高度 = 状态栏高度 + 44
  152. this.globalData.navBarHeight = systemInfo.statusBarHeight + 44;
  153. this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right;
  154. this.globalData.menuTop = menuButtonInfo.top;
  155. this.globalData.menuHeight = menuButtonInfo.height;
  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. globalData: {
  188. userInfo: null,
  189. isIOS: false, // 判断设备是否为苹果
  190. desktopTips: false,
  191. navBarHeight: 0, // 导航栏高度
  192. menuRight: 0, // 胶囊距右方间距(方保持左、右间距一致)
  193. menuTop: 0, // 胶囊距底部间距(保持底部间距一致)
  194. menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
  195. windowWidth: 0,
  196. windowHeight: 0
  197. }
  198. })