App.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <script>
  2. export default {
  3. data() {
  4. return {}
  5. },
  6. onLaunch: function () {
  7. setInterval((d) => {
  8. //定时器,定时去调取聊天未读消息
  9. console.log(uni.getStorageSync('shopToken'), '初始化')
  10. let shopToken = uni.getStorageSync('shopToken')
  11. if (shopToken) {
  12. let shopId = uni.getStorageSync('shopId')
  13. let data = {
  14. shopId: shopId
  15. }
  16. this.$Request.get('/shop/ordersChat/selectShopAllUnreadCount', data).then((res) => {
  17. if (res.code === 0) {
  18. // let chatCount = res.data
  19. let messageCount = res.data
  20. uni.setStorageSync('messageCount', messageCount)
  21. let num = messageCount
  22. if (num == 0) {
  23. uni.removeTabBarBadge({
  24. index: 2
  25. })
  26. return
  27. }
  28. uni.setTabBarBadge({
  29. index: 2,
  30. text: num + ''
  31. })
  32. }
  33. })
  34. }
  35. }, 3000)
  36. //#ifdef APP-PLUS
  37. // APP检测更新 具体打包流程可以参考:https://ask.dcloud.net.cn/article/35667
  38. plus.screen.lockOrientation('portrait-primary') //竖屏正方向锁定
  39. //获取是否热更新过
  40. const updated = uni.getStorageSync('updated') // 尝试读取storage
  41. let that = this
  42. if (updated.completed === true) {
  43. // 如果上次刚更新过
  44. // 删除安装包及安装记录
  45. console.log('安装记录被删除,更新成功')
  46. uni.removeSavedFile({
  47. filePath: updated.packgePath,
  48. success: (res) => {
  49. uni.removeStorageSync('updated')
  50. }
  51. })
  52. } else if (updated.completed === false) {
  53. uni.removeStorageSync('updated')
  54. plus.runtime.install(updated.packgePath, {
  55. force: true
  56. })
  57. uni.setStorage({
  58. key: 'updated',
  59. data: {
  60. completed: true,
  61. packgePath: updated.packgePath
  62. },
  63. success: (res) => {
  64. console.log('成功安装上次的更新,应用需要重启才能继续完成')
  65. }
  66. })
  67. uni.showModal({
  68. title: '温馨提示',
  69. content: '应用将重启以完成更新',
  70. showCancel: false,
  71. complete: () => {
  72. plus.runtime.restart()
  73. }
  74. })
  75. } else {
  76. //获取当前系统版本信息
  77. plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
  78. //请求后台接口 解析数据 对比版本
  79. // let data = {
  80. // classify: 1
  81. // }
  82. that.$Request.getT('/app/user/selectNewApp').then((res) => {
  83. res = res.data[2]
  84. if (res.wgtUrl && widgetInfo.version < res.version) {
  85. let downloadLink = ''
  86. let androidLink = res.androidWgtUrl
  87. let iosLink = res.iosWgtUrl
  88. let ready = false
  89. //校验是是不是热更新
  90. if (res.wgtUrl.match(RegExp(/.wgt/))) {
  91. // 判断系统类型
  92. if (plus.os.name.toLowerCase() === 'android') {
  93. console.log('安卓系统')
  94. if (androidLink && androidLink !== '#') {
  95. // 我这里默认#也是没有地址,请根据业务自行修改
  96. console.log('发现下载地址')
  97. // 安卓:创建下载任务
  98. if (androidLink.match(RegExp(/.wgt/))) {
  99. console.log('确认wgt热更新包')
  100. downloadLink = androidLink
  101. ready = true
  102. } else {
  103. console.log('安卓推荐.wgt强制更新,.apk的强制更新请您自行修改程序')
  104. }
  105. } else {
  106. console.log('下载地址是空的,无法继续')
  107. }
  108. } else {
  109. console.log('苹果系统')
  110. if (iosLink && iosLink !== '#') {
  111. // 我这里默认#也是没有地址,请根据业务自行修改
  112. console.log('发现下载地址')
  113. // 苹果(A):进行热更新(如果iosLink是wgt更新包的下载地址)判断文件名中是否含有.wgt
  114. if (iosLink.match(RegExp(/.wgt/))) {
  115. console.log('确认wgt热更新包')
  116. downloadLink = iosLink
  117. ready = true
  118. } else {
  119. console.log('苹果只支持.wgt强制更新')
  120. }
  121. } else {
  122. console.log('下载地址是空的,无法继续')
  123. }
  124. }
  125. if (ready) {
  126. console.log('任务开始')
  127. let downloadTask = uni.downloadFile({
  128. url: downloadLink,
  129. success: (res) => {
  130. if (res.statusCode === 200) {
  131. // 保存下载的安装包
  132. console.log('保存安装包')
  133. uni.saveFile({
  134. tempFilePath: res.tempFilePath,
  135. success: (res) => {
  136. const packgePath = res.savedFilePath
  137. // 保存更新记录到stroage,下次启动app时安装更新
  138. uni.setStorage({
  139. key: 'updated',
  140. data: {
  141. completed: false,
  142. packgePath: packgePath
  143. },
  144. success: () => {
  145. console.log('成功保存记录')
  146. }
  147. })
  148. // 任务完成,关闭下载任务
  149. console.log('任务完成,关闭下载任务,下一次启动应用时将安装更新')
  150. downloadTask.abort()
  151. downloadTask = null
  152. }
  153. })
  154. }
  155. }
  156. })
  157. } else {
  158. console.log('下载地址未准备,无法开启下载任务')
  159. }
  160. } else {
  161. //不是热更新是在线更新 校验是否强制升级
  162. if (res.method == 'true') {
  163. uni.showModal({
  164. showCancel: false,
  165. confirmText: '立即更新',
  166. title: '发现新版本',
  167. content: res.des,
  168. success: (res) => {
  169. if (res.confirm) {
  170. that.$queue.showLoading('下载中...')
  171. if (uni.getSystemInfoSync().platform == 'android') {
  172. uni.downloadFile({
  173. url: androidLink,
  174. success: (downloadResult) => {
  175. if (downloadResult.statusCode === 200) {
  176. plus.runtime.install(
  177. downloadResult.tempFilePath,
  178. {
  179. force: false
  180. },
  181. (d) => {
  182. console.log('install success...')
  183. plus.runtime.restart()
  184. },
  185. (e) => {
  186. console.error('install fail...')
  187. }
  188. )
  189. }
  190. }
  191. })
  192. }
  193. if (uni.getSystemInfoSync().platform == 'ios') {
  194. plus.runtime.openURL(iosLink, function (res) {})
  195. }
  196. } else if (res.cancel) {
  197. console.log('取消')
  198. }
  199. }
  200. })
  201. } else {
  202. uni.showModal({
  203. title: '发现新版本',
  204. confirmText: '立即更新',
  205. cancelText: '下次更新',
  206. content: res.des,
  207. success: (res) => {
  208. if (res.confirm) {
  209. that.$queue.showLoading('下载中...')
  210. if (uni.getSystemInfoSync().platform == 'android') {
  211. uni.downloadFile({
  212. url: androidLink,
  213. success: (downloadResult) => {
  214. if (downloadResult.statusCode === 200) {
  215. plus.runtime.install(
  216. downloadResult.tempFilePath,
  217. {
  218. force: false
  219. },
  220. (d) => {
  221. console.log('install success...')
  222. plus.runtime.restart()
  223. },
  224. (e) => {
  225. console.error('install fail...')
  226. }
  227. )
  228. }
  229. }
  230. })
  231. }
  232. if (uni.getSystemInfoSync().platform == 'ios') {
  233. plus.runtime.openURL(iosLink, function (res) {})
  234. }
  235. } else if (res.cancel) {
  236. console.log('取消')
  237. }
  238. }
  239. })
  240. }
  241. }
  242. }
  243. })
  244. })
  245. }
  246. //#endif
  247. },
  248. onShow: function () {
  249. console.log('App Show')
  250. let updateManager = uni.getUpdateManager()
  251. if (!updateManager) {
  252. return
  253. } else {
  254. //新版本更新
  255. if (uni.canIUse('getUpdateManager')) {
  256. //判断当前微信版本是否支持版本更新
  257. updateManager.onCheckForUpdate(function (res) {
  258. if (res.hasUpdate) {
  259. // 请求完新版本信息的回调
  260. updateManager.onUpdateReady(function () {
  261. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  262. updateManager.applyUpdate()
  263. // uni.showModal({
  264. // title: "更新提示",
  265. // content: "新版本已经准备好,是否重启应用?",
  266. // success: function (res) {
  267. // if (res.confirm) {
  268. // // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  269. // updateManager.applyUpdate();
  270. // }
  271. // },
  272. // });
  273. })
  274. // updateManager.onUpdateFailed(function () {
  275. // uni.showModal({
  276. // // 新的版本下载失败
  277. // title: "已经有新版本了哟~",
  278. // content:
  279. // "新版本已经上线啦~,请您删除当前小程序,到微信 “发现-小程序” 页,重新搜索打开哦~",
  280. // });
  281. // });
  282. } else {
  283. }
  284. })
  285. } else {
  286. uni.showModal({
  287. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  288. title: '溫馨提示',
  289. content: '当前微信版本过低,部分功能无法使用,请升级到最新微信版本后重试。'
  290. })
  291. }
  292. }
  293. this.$Request.get('/app/common/type/323').then((res) => {
  294. if (res.code == 0) {
  295. // #ifdef MP-WEIXIN
  296. this.$queue.setData('XCXIsSelect', res.data.value)
  297. // #endif
  298. // #ifndef MP-WEIXIN
  299. this.$queue.setData('XCXIsSelect', '是')
  300. // #endif
  301. }
  302. })
  303. // #ifdef APP-PLUS
  304. if (uni.getSystemInfoSync().platform == 'android') {
  305. let clientid = plus.push.getClientInfo().clientid
  306. let userId = this.$queue.getData('userId')
  307. if (userId) {
  308. this.$Request.postT('/user/updateShopClientId?clientId=' + clientid).then((res) => {})
  309. }
  310. }
  311. //#endif
  312. },
  313. methods: {},
  314. onHide: function () {
  315. console.log('App Hide')
  316. }
  317. }
  318. </script>
  319. <style lang="scss">
  320. /*每个页面公共css */
  321. @import 'uview-ui/index.scss';
  322. @import 'components/colorui/main.css';
  323. @import 'components/colorui/icon.css';
  324. </style>