app.js 7.8 KB

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