app.js 7.7 KB

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