app.js 8.6 KB

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