App.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <script>
  2. export default {
  3. onLaunch: function () {
  4. console.log('检测',wx.canIUse('getUpdateManager'))
  5. // 检测小程序更新
  6. if (wx.canIUse('getUpdateManager')) {
  7. const updateManager = wx.getUpdateManager();
  8. // 仅保留监听,无需手动调用checkForUpdate
  9. updateManager.onCheckForUpdate((res) => {
  10. console.log('检查更新结果:', res.hasUpdate); // 关键日志
  11. if (res.hasUpdate) {
  12. // 2. 监听新版本下载完成
  13. updateManager.onUpdateReady(() => {
  14. wx.showModal({
  15. title: '更新提示',
  16. content: '新版本已准备好,是否重启?',
  17. success: (res) => {
  18. if (res.confirm) updateManager.applyUpdate();
  19. }
  20. });
  21. });
  22. // 3. 监听下载失败
  23. updateManager.onUpdateFailed(() => {
  24. wx.showToast({ title: '新版本下载失败', icon: 'none' });
  25. });
  26. }
  27. });
  28. // 主动触发检查(部分场景需手动调用)
  29. updateManager.checkForUpdate(); // 关键:手动触发检查
  30. }
  31. // setInterval((d) => {
  32. // //定时器,定时去调取聊天未读消息
  33. // let userId = uni.getStorageSync('userId')
  34. // if (userId) {
  35. // this.$Request.getMsg('/app/ordersChat/selectUserAllUnreadCount').then((res) => {
  36. // if (res.code === 0) {
  37. // let messageCount = res.data
  38. // uni.setStorageSync('messageCount', messageCount)
  39. // let num = messageCount
  40. // // console.log(num, '未读消息数')
  41. // if (num == 0) {
  42. // uni.removeTabBarBadge({
  43. // index: 2
  44. // })
  45. // return
  46. // }
  47. // uni.setTabBarBadge({
  48. // index: 2,
  49. // text: num + ''
  50. // })
  51. // }
  52. // })
  53. // }
  54. // }, 3000)
  55. // 是否开启总送跑腿新用户红包 256
  56. this.$Request.get('/app/common/type/256').then((res) => {
  57. if (res.code == 0) {
  58. this.$queue.setData('paotuiHB', res.data.value)
  59. }
  60. })
  61. // 是否开启赠送外卖新用户红包 324
  62. this.$Request.get('/app/common/type/324').then((res) => {
  63. if (res.code == 0) {
  64. this.$queue.setData('waimaiHB', res.data.value)
  65. }
  66. })
  67. //小程序上线开关
  68. console.log('App Launch')
  69. // this.$Request.get('/app/common/type/261').then(res => {
  70. // if (res.code == 0) {
  71. // // #ifdef MP-WEIXIN
  72. // this.$queue.setData('XCXIsSelect', res.data.value);
  73. // this.$queue.setData('shangxianSelect', res.data.value);
  74. // // #endif
  75. // // #ifndef MP-WEIXIN
  76. // this.$queue.setData('XCXIsSelect', '是');
  77. // this.$queue.setData('shangxianSelect', '是');
  78. // // #endif
  79. // }
  80. // });
  81. this.$Request.get('/app/common/type/264').then((res) => {
  82. if (res.code == 0) {
  83. // #ifdef MP-WEIXIN
  84. this.$queue.setData('XCXIsSelect', res.data.value)
  85. this.$queue.setData('shangxianSelect', res.data.value)
  86. // #endif
  87. // #ifndef MP-WEIXIN
  88. this.$queue.setData('XCXIsSelect', '是')
  89. this.$queue.setData('shangxianSelect', '是')
  90. // #endif
  91. }
  92. })
  93. //#ifdef APP-PLUS
  94. // APP检测更新 具体打包流程可以参考:https://ask.dcloud.net.cn/article/35667
  95. plus.screen.lockOrientation('portrait-primary') //竖屏正方向锁定
  96. //获取是否热更新过
  97. const updated = uni.getStorageSync('updated') // 尝试读取storage
  98. let that = this
  99. if (updated.completed === true) {
  100. // 如果上次刚更新过
  101. // 删除安装包及安装记录
  102. console.log('安装记录被删除,更新成功')
  103. uni.removeSavedFile({
  104. filePath: updated.packgePath,
  105. success: (res) => {
  106. uni.removeStorageSync('updated')
  107. }
  108. })
  109. } else if (updated.completed === false) {
  110. uni.removeStorageSync('updated')
  111. plus.runtime.install(updated.packgePath, {
  112. force: true
  113. })
  114. uni.setStorage({
  115. key: 'updated',
  116. data: {
  117. completed: true,
  118. packgePath: updated.packgePath
  119. },
  120. success: (res) => {
  121. console.log('成功安装上次的更新,应用需要重启才能继续完成')
  122. }
  123. })
  124. uni.showModal({
  125. title: '温馨提示',
  126. content: '应用将重启以完成更新',
  127. showCancel: false,
  128. complete: () => {
  129. plus.runtime.restart()
  130. }
  131. })
  132. } else {
  133. //获取当前系统版本信息
  134. plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
  135. //请求后台接口 解析数据 对比版本
  136. that.$Request.getT('/app/user/selectNewApp').then((res) => {
  137. res = res.data[0]
  138. if (res.wgtUrl && widgetInfo.version < res.version) {
  139. let downloadLink = ''
  140. let androidLink = res.androidWgtUrl
  141. let iosLink = res.iosWgtUrl
  142. let ready = false
  143. //校验是是不是热更新
  144. if (res.wgtUrl.match(RegExp(/.wgt/))) {
  145. // 判断系统类型
  146. if (plus.os.name.toLowerCase() === 'android') {
  147. console.log('安卓系统')
  148. if (androidLink && androidLink !== '#') {
  149. // 我这里默认#也是没有地址,请根据业务自行修改
  150. console.log('发现下载地址')
  151. // 安卓:创建下载任务
  152. if (androidLink.match(RegExp(/.wgt/))) {
  153. console.log('确认wgt热更新包')
  154. downloadLink = androidLink
  155. ready = true
  156. } else {
  157. console.log('安卓推荐.wgt强制更新,.apk的强制更新请您自行修改程序')
  158. }
  159. } else {
  160. console.log('下载地址是空的,无法继续')
  161. }
  162. } else {
  163. console.log('苹果系统')
  164. if (iosLink && iosLink !== '#') {
  165. // 我这里默认#也是没有地址,请根据业务自行修改
  166. console.log('发现下载地址')
  167. // 苹果(A):进行热更新(如果iosLink是wgt更新包的下载地址)判断文件名中是否含有.wgt
  168. if (iosLink.match(RegExp(/.wgt/))) {
  169. console.log('确认wgt热更新包')
  170. downloadLink = iosLink
  171. ready = true
  172. } else {
  173. console.log('苹果只支持.wgt强制更新')
  174. }
  175. } else {
  176. console.log('下载地址是空的,无法继续')
  177. }
  178. }
  179. if (ready) {
  180. console.log('任务开始')
  181. let downloadTask = uni.downloadFile({
  182. url: downloadLink,
  183. success: (res) => {
  184. if (res.statusCode === 200) {
  185. // 保存下载的安装包
  186. console.log('保存安装包')
  187. uni.saveFile({
  188. tempFilePath: res.tempFilePath,
  189. success: (res) => {
  190. const packgePath = res.savedFilePath
  191. // 保存更新记录到stroage,下次启动app时安装更新
  192. uni.setStorage({
  193. key: 'updated',
  194. data: {
  195. completed: false,
  196. packgePath: packgePath
  197. },
  198. success: () => {
  199. console.log('成功保存记录')
  200. }
  201. })
  202. // 任务完成,关闭下载任务
  203. console.log('任务完成,关闭下载任务,下一次启动应用时将安装更新')
  204. downloadTask.abort()
  205. downloadTask = null
  206. }
  207. })
  208. }
  209. }
  210. })
  211. } else {
  212. console.log('下载地址未准备,无法开启下载任务')
  213. }
  214. } else {
  215. //不是热更新是在线更新 校验是否强制升级
  216. if (res.method == 'true') {
  217. uni.showModal({
  218. showCancel: false,
  219. confirmText: '立即更新',
  220. title: '发现新版本',
  221. content: res.des,
  222. success: (res) => {
  223. if (res.confirm) {
  224. that.$queue.showLoading('下载中...')
  225. if (uni.getSystemInfoSync().platform == 'android') {
  226. uni.downloadFile({
  227. url: androidLink,
  228. success: (downloadResult) => {
  229. if (downloadResult.statusCode === 200) {
  230. plus.runtime.install(
  231. downloadResult.tempFilePath,
  232. {
  233. force: false
  234. },
  235. (d) => {
  236. console.log('install success...')
  237. plus.runtime.restart()
  238. },
  239. (e) => {
  240. console.error('install fail...')
  241. }
  242. )
  243. }
  244. }
  245. })
  246. }
  247. if (uni.getSystemInfoSync().platform == 'ios') {
  248. plus.runtime.openURL(iosLink, function (res) {})
  249. }
  250. } else if (res.cancel) {
  251. console.log('取消')
  252. }
  253. }
  254. })
  255. } else {
  256. uni.showModal({
  257. title: '发现新版本',
  258. confirmText: '立即更新',
  259. cancelText: '下次更新',
  260. content: res.des,
  261. success: (res) => {
  262. if (res.confirm) {
  263. that.$queue.showLoading('下载中...')
  264. if (uni.getSystemInfoSync().platform == 'android') {
  265. uni.downloadFile({
  266. url: androidLink,
  267. success: (downloadResult) => {
  268. if (downloadResult.statusCode === 200) {
  269. plus.runtime.install(
  270. downloadResult.tempFilePath,
  271. {
  272. force: false
  273. },
  274. (d) => {
  275. console.log('install success...')
  276. plus.runtime.restart()
  277. },
  278. (e) => {
  279. console.error('install fail...')
  280. }
  281. )
  282. }
  283. }
  284. })
  285. }
  286. if (uni.getSystemInfoSync().platform == 'ios') {
  287. plus.runtime.openURL(iosLink, function (res) {})
  288. }
  289. } else if (res.cancel) {
  290. console.log('取消')
  291. }
  292. }
  293. })
  294. }
  295. }
  296. }
  297. })
  298. })
  299. }
  300. //#endif
  301. //#ifdef H5
  302. let ua = navigator.userAgent.toLowerCase()
  303. if (ua.indexOf('micromessenger') !== -1) {
  304. let openid = uni.getStorageSync('openid')
  305. let userId = uni.getStorageSync('userId')
  306. let that = this
  307. console.log(openid)
  308. if (!openid) {
  309. if (window.location.href.indexOf('?code=') !== -1 || window.location.href.indexOf('&code=') !== -1) {
  310. let code
  311. if (window.location.href.indexOf('?code=') !== -1) {
  312. code = window.location.href.split('?code=')[1].split('&')[0]
  313. } else {
  314. code = window.location.href.split('&code=')[1].split('&')[0]
  315. }
  316. let data = {
  317. code: code
  318. }
  319. this.$Request.get('/app/Login/getOpenId', data).then((ret) => {
  320. this.$queue.setData('openid', ret.data)
  321. let data2 = {
  322. openId: ret.data,
  323. userType: 1
  324. }
  325. this.$Request.get('/app/Login/wxOpenIdLogin', data2).then((res) => {
  326. console.error(res)
  327. if (res.code == 0) {
  328. this.$queue.setData('token', res.token)
  329. this.$queue.setData('userId', res.user.userId)
  330. this.$queue.setData('phone', res.user.phone)
  331. uni.setStorageSync('userName', res.user.userName)
  332. uni.setStorageSync('avatar', res.user.avatar)
  333. uni.setStorageSync('phone', res.user.phone)
  334. uni.setStorageSync('sex', res.user.sex)
  335. uni.setStorageSync('userId', res.user.userId)
  336. uni.setStorageSync('wxCode', res.user.wxCode)
  337. uni.setStorageSync('wxQrCode', res.user.wxQrCode)
  338. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  339. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  340. }
  341. })
  342. })
  343. } else {
  344. window.location.href =
  345. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
  346. that.$queue.getWxAppid() +
  347. '&redirect_uri=' +
  348. window.location.href.split('#')[0] +
  349. '&response_type=code&scope=snsapi_base#wechat_redirect'
  350. }
  351. } else {
  352. this.$Request.get('/app/Login/wxOpenIdLogin?openId=' + openid + '&userType=1').then((res) => {
  353. if (res.code == 0) {
  354. this.$queue.setData('token', res.token)
  355. this.$queue.setData('userId', res.user.userId)
  356. this.$queue.setData('phone', res.user.phone)
  357. uni.setStorageSync('userName', res.user.userName)
  358. uni.setStorageSync('avatar', res.user.avatar)
  359. uni.setStorageSync('phone', res.user.phone)
  360. uni.setStorageSync('sex', res.user.sex)
  361. uni.setStorageSync('userId', res.user.userId)
  362. uni.setStorageSync('wxCode', res.user.wxCode)
  363. uni.setStorageSync('wxQrCode', res.user.wxQrCode)
  364. uni.setStorageSync('zhiFuBao', res.user.zhiFuBao)
  365. uni.setStorageSync('zhiFuBaoName', res.user.zhiFuBaoName)
  366. }
  367. })
  368. }
  369. }
  370. //#endif
  371. },
  372. onShow: function () {
  373. console.log('App Show')
  374. // #ifdef APP-PLUS
  375. if (uni.getSystemInfoSync().platform == 'android') {
  376. let clientid = plus.push.getClientInfo().clientid
  377. let userId = this.$queue.getData('userId')
  378. if (userId) {
  379. console.log(clientid, '----', userId)
  380. this.$Request.postT('/app/user/updateClientId?clientId=' + clientid).then((res) => {})
  381. }
  382. }
  383. //#endif
  384. },
  385. onHide: function () {
  386. console.log('App Hide')
  387. }
  388. }
  389. </script>
  390. <style lang="scss">
  391. /*每个页面公共css */
  392. @import 'uview-ui/index.scss';
  393. @import 'components/colorui/main.css';
  394. @import 'components/colorui/icon.css';
  395. </style>