app-update.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <uni-popup ref="popup" type="center" :maskClick="false">
  3. <view class="update-wrap">
  4. <view class="white-bg"></view>
  5. <image src="@/static/images/top_2.png" class="updateCon-img"></image>
  6. <view class="updateCon-top">
  7. <!-- 发现新版本 -->
  8. <text class="update-top-title">发现新版本</text>
  9. <text class="update-top-version">V{{update_info.version}}</text>
  10. </view>
  11. <text class="uodate-content" v-if="!update_ing">更新内容:{{update_info.version_note}}</text>
  12. <text class="current-version" v-if="!update_ing">当前版本:V{{version}}</text>
  13. <view class="update-btn" v-if="!update_ing">
  14. <view class="update-btn-item update-btn-left" @click="up_close">
  15. <!-- 残忍拒绝 -->
  16. <text class="update-btn-item-text ">残忍拒绝</text>
  17. </view>
  18. <view class="update-btn-item" @click="nowUpdate">
  19. <!-- 立即升级 -->
  20. <text class="update-btn-item-text text-bule">立即升级</text>
  21. </view>
  22. </view>
  23. <!-- 下载进度 -->
  24. <view class="sche-wrap" v-if="update_ing">
  25. <!-- 更新包下载中 -->
  26. <text class="sche-wrap-text">更新包下载中...</text>
  27. <view class="sche-bg">
  28. <view class="sche-bg-jindu" :style="lengthWidth">
  29. <view class="sche-bg-round">
  30. <text class="sche-bg-round-text" v-if="schedule">{{schedule}}%</text>
  31. <text class="sche-bg-round-text" v-else>{{(downloadedSize/1024/1024 ).toFixed(2)}}M</text>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. </uni-popup>
  38. </template>
  39. <script>
  40. import uniPopup from '@/components/uni-popup/uni-popup.vue';
  41. import { creatRequest } from '../../utils/request.js';
  42. export default {
  43. // props:['url'],
  44. data() {
  45. return {
  46. platform: "", //ios or android
  47. version: "0.0.0", //当前软件版本
  48. is_update: false, // 是否更新
  49. is_reques: false, //是否请求中
  50. update_info: {
  51. "type": "2", //1分发平台更新 2安装包/升级包更新
  52. "version_note": "", //ios更新备注
  53. "version": "0.0.0", //ios版本号
  54. "download_url": "", //ios更新新链接
  55. },
  56. ifCheckAppUpdate:false,
  57. is_update: false,
  58. schedule: 0,
  59. update_ing: false, //点击升级
  60. is_down: false,
  61. downloadedSize: 0,
  62. };
  63. },
  64. components:{uniPopup},
  65. mounted() {
  66. const updateRejectStart = uni.getStorageSync('updateRejectStart');
  67. const updateRejectCurrent = new Date().getTime();
  68. const timeLeft = updateRejectCurrent-(updateRejectStart?updateRejectStart:0);
  69. console.log({updateRejectStart,updateRejectCurrent,timeLeft});
  70. if(timeLeft>=600000){
  71. this.getSystemInfo() //获取系统新
  72. }
  73. },
  74. watch: {
  75. ifCheckAppUpdate: function(newVal, oldVal) {
  76. //其他业务代码
  77. console.log({newVal, oldVal});
  78. }
  79. },
  80. computed: {
  81. // 下载进度计算
  82. lengthWidth: function() {
  83. return {
  84. width: this.schedule * 480 / 100 + "rpx"
  85. }
  86. }
  87. },
  88. methods: {
  89. // 获取系统信息
  90. getSystemInfo() {
  91. let vm = this;
  92. // 获取手机系统信息
  93. uni.getSystemInfo({
  94. success: function(res) {
  95. vm.platform = res.platform //ios or android
  96. }
  97. });
  98. // 获取版本号
  99. // #ifdef APP-PLUS
  100. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  101. console.log({inf});
  102. vm.version = inf.version
  103. vm.getUpdateInfo(); //获取更新信息
  104. });
  105. // #endif
  106. },
  107. // 获取线上版本信息
  108. getUpdateInfo() {
  109. let vm = this;
  110. const obj = {
  111. method: 'GET',
  112. url: 'versionInfo/getVersionData'
  113. }
  114. console.log('当前版本',vm.version);
  115. creatRequest(obj,{versionNo:vm.version}).then(res=>{
  116. console.log({res});
  117. if(res.isNew){
  118. // vm.update_info.type = "1";
  119. vm.update_info.version_note = res.versionContent;
  120. vm.update_info.version = res.versionNo;
  121. vm.update_info.download_url = res.versionUrl;
  122. }else {
  123. }
  124. vm.checkUpdate(); ///检查是否更新
  125. })
  126. },
  127. // 检查是否更新
  128. checkUpdate() {
  129. let vm = this;
  130. console.log(vm.version,vm.update_info.version);
  131. vm.is_update = vm.compareVersion(vm.version, vm.update_info.version); // 检查是否升级
  132. if (vm.is_update) {
  133. vm.$refs.popup.open() //显示升级弹窗
  134. } else {
  135. // 不更新,根据需要做处理
  136. }
  137. },
  138. // 取消更新
  139. up_close() {
  140. console.log('点击取消');
  141. // this.$store.commit('setAppUpdateStatus',false);
  142. uni.setStorageSync('updateRejectStart',new Date().getTime());
  143. this.$refs.popup.close();
  144. // plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication().performSelector("exit");
  145. },
  146. // 立即更新
  147. nowUpdate() {
  148. let vm = this;
  149. if(vm.is_reques){
  150. return false //如果正在下载就停止操作
  151. }else{
  152. vm.is_reques = true
  153. }
  154. console.log(111111);
  155. if (vm.update_info.type == 1) {
  156. // 分发平台更新 //调用浏览器打开链接或者分发平台
  157. plus.runtime.openURL(vm.update_info.download_url, function() {
  158. plus.nativeUI.toast("打开错误");
  159. });
  160. setTimeout(function() {
  161. plus.runtime.quit(); //五秒后关闭app
  162. }, 5000)
  163. } else if (vm.update_info.type == 2) {
  164. // 安装包/升级包更新
  165. vm.download_wgt()
  166. }
  167. },
  168. // 下载升级资源包
  169. download_wgt() {
  170. let vm = this;
  171. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  172. // const token = uni.getStorageSync('token');
  173. let options = {
  174. method: "GET",
  175. };
  176. let dtask = plus.downloader.createDownload(vm.update_info.download_url, options);
  177. dtask.addEventListener("statechanged", function(task, status) {
  178. switch (task.state) {
  179. case 1: // 开始
  180. break;
  181. case 2: //已连接到服务器
  182. vm.update_ing = true;
  183. break;
  184. case 3: // 已接收到数据
  185. vm.downloadedSize = task.downloadedSize;
  186. let totalSize = 0;
  187. if (task.totalSize) {
  188. totalSize = task.totalSize //服务器须返回正确的content-length才会有长度
  189. }
  190. vm.schedule = parseInt(100 * task.downloadedSize / totalSize);
  191. break;
  192. case 4:
  193. vm.installWgt(task.filename); // 安装wgt包
  194. break;
  195. }
  196. });
  197. dtask.start();
  198. },
  199. // 安装文件
  200. installWgt(path) {
  201. let vm = this;
  202. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  203. plus.runtime.install(path, {}, function() {
  204. plus.nativeUI.closeWaiting();
  205. // 应用资源更新完成!
  206. plus.nativeUI.alert("应用资源更新完成!", function() {
  207. plus.runtime.restart();
  208. });
  209. }, function(e) {
  210. plus.nativeUI.closeWaiting();
  211. // 安装更新文件失败
  212. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  213. });
  214. },
  215. // 对比版本号
  216. compareVersion(ov, nv) {
  217. if (!ov || !nv || ov == "" || nv == "") {
  218. return false;
  219. }
  220. let b = false,
  221. ova = ov.split(".", 4),
  222. nva = nv.split(".", 4);
  223. for (let i = 0; i < ova.length && i < nva.length; i++) {
  224. let so = ova[i],
  225. no = parseInt(so),
  226. sn = nva[i],
  227. nn = parseInt(sn);
  228. if (nn > no || sn.length > so.length) {
  229. return true;
  230. } else if (nn < no) {
  231. return false;
  232. }
  233. }
  234. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  235. return true;
  236. } else {
  237. return false;
  238. }
  239. },
  240. }
  241. }
  242. </script>
  243. <style scoped>
  244. /*#ifndef APP-NVUE*/
  245. view {
  246. display: flex;
  247. flex-direction: column;
  248. box-sizing: border-box;
  249. }
  250. /*#endif*/
  251. .updateBox {
  252. background-color: rgba(0, 0, 0, 0.6);
  253. z-index: 1000;
  254. position: fixed;
  255. right: 0px;
  256. top: 0;
  257. left: 0;
  258. bottom: 0;
  259. align-items: center;
  260. justify-content: center;
  261. }
  262. .update-wrap {
  263. width: 580rpx;
  264. height: 500rpx;
  265. border-radius: 10px;
  266. padding-bottom: 0px;
  267. position: relative;
  268. }
  269. .white-bg {
  270. position: absolute;
  271. top: 60rpx;
  272. left: 0px;
  273. width: 580rpx;
  274. height: 440rpx;
  275. background-color: #ffffff;
  276. border-bottom-left-radius: 30rpx;
  277. border-bottom-right-radius: 30rpx;
  278. }
  279. .updateCon-img {
  280. position: absolute;
  281. top: 0rpx;
  282. left: 0px;
  283. width: 580rpx;
  284. height: 440rpx;
  285. }
  286. .updateCon-top {
  287. padding: 70rpx 50rpx;
  288. /*#ifndef APP-NVUE*/
  289. z-index: 1;
  290. /*#endif*/
  291. }
  292. .update-top-title {
  293. color: #fff;
  294. font-size: 36rpx;
  295. font-weight: bold;
  296. }
  297. .update-top-version {
  298. color: #fff;
  299. font-size: 28rpx;
  300. margin-top: 10rpx;
  301. }
  302. .uodate-content {
  303. color: #333;
  304. font-size: 18px;
  305. padding: 0px 50rpx;
  306. margin-top: 80rpx;
  307. /*#ifndef APP-NVUE*/
  308. z-index: 1;
  309. /*#endif*/
  310. }
  311. .current-version {
  312. text-align: center;
  313. margin-top: 10rpx;
  314. font-size: 24rpx;
  315. color: #666;
  316. /*#ifndef APP-NVUE*/
  317. z-index: 1;
  318. /*#endif*/
  319. }
  320. .update-btn {
  321. position: absolute;
  322. left: 0px;
  323. bottom: 0px;
  324. width: 580rpx;
  325. height: 80rpx;
  326. padding: 0px 50rpx;
  327. align-items: center;
  328. flex-direction: row;
  329. border-top-color: #e7e7e7;
  330. border-top-style: solid;
  331. border-top-width: 1px;
  332. background-color: #fff;
  333. border-bottom-left-radius: 30rpx;
  334. border-bottom-right-radius: 30rpx;
  335. }
  336. .update-btn-item {
  337. width: 240rpx;
  338. height: 80rpx;
  339. justify-content: center;
  340. align-items: center;
  341. }
  342. .update-btn-left {
  343. border-right-color: #e7e7e7;
  344. border-right-style: solid;
  345. border-right-width: 1px;
  346. }
  347. .update-btn-item-text {
  348. text-align: center;
  349. font-size: 28rpx;
  350. color: #666;
  351. }
  352. .text-bule {
  353. color: #045FCF;
  354. }
  355. .sche-wrap {
  356. padding: 0px 50rpx 0rpx;
  357. height: 100rpx;
  358. align-items: center;
  359. border-bottom-left-radius: 30rpx;
  360. border-bottom-right-radius: 30rpx;
  361. flex: 1;
  362. justify-content: flex-end;
  363. }
  364. .sche-wrap-text {
  365. font-size: 24rpx;
  366. color: #666;
  367. margin-bottom: 20rpx;
  368. }
  369. .sche-bg {
  370. position: relative;
  371. background-color: #ccc;
  372. height: 20rpx;
  373. border-radius: 100px;
  374. width: 480rpx;
  375. margin-bottom: 30rpx;
  376. }
  377. .sche-bg-jindu {
  378. position: absolute;
  379. left: 0px;
  380. top: 0px;
  381. height: 20rpx;
  382. background-color: #5775e7;
  383. border-radius: 100px;
  384. }
  385. .sche-bg-round {
  386. position: absolute;
  387. left: 100%;
  388. height: 24rpx;
  389. width: 24rpx;
  390. background-color: #fff;
  391. border-color: #fff;
  392. border-style: solid;
  393. border-width: 10px;
  394. border-radius: 100px;
  395. transform: translateX(-20rpx) translateY(-10rpx);
  396. }
  397. .sche-bg-round-text {
  398. font-size: 24rpx;
  399. width: 120rpx;
  400. text-align: center;
  401. transform: translateX(-50rpx) translateY(-40rpx);
  402. color: #5775e7;
  403. }
  404. .uodate-close {
  405. /* display: flex; */
  406. flex-direction: row;
  407. justify-content: center;
  408. padding-top: 50rpx;
  409. }
  410. .uodate-close-img {
  411. width: 80rpx;
  412. height: 80rpx;
  413. }
  414. </style>