app.js 8.7 KB

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