app.js 8.0 KB

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