app.js 8.6 KB

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