app.js 8.0 KB

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