app.js 8.0 KB

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