app-update.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. console.log(task.downloadedSize);
  187. let totalSize = 0;
  188. if (task.totalSize) {
  189. totalSize = task.totalSize //服务器须返回正确的content-length才会有长度
  190. }
  191. vm.schedule = parseInt(100 * task.downloadedSize / totalSize);
  192. break;
  193. case 4:
  194. vm.installWgt(task.filename); // 安装wgt包
  195. break;
  196. }
  197. });
  198. dtask.start();
  199. },
  200. // 安装文件
  201. installWgt(path) {
  202. let vm = this;
  203. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  204. plus.runtime.install(path, {}, function() {
  205. plus.nativeUI.closeWaiting();
  206. // 应用资源更新完成!
  207. plus.nativeUI.alert("应用资源更新完成!", function() {
  208. plus.runtime.restart();
  209. });
  210. }, function(e) {
  211. plus.nativeUI.closeWaiting();
  212. // 安装更新文件失败
  213. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  214. });
  215. },
  216. // 对比版本号
  217. compareVersion(ov, nv) {
  218. if (!ov || !nv || ov == "" || nv == "") {
  219. return false;
  220. }
  221. let b = false,
  222. ova = ov.split(".", 4),
  223. nva = nv.split(".", 4);
  224. for (let i = 0; i < ova.length && i < nva.length; i++) {
  225. let so = ova[i],
  226. no = parseInt(so),
  227. sn = nva[i],
  228. nn = parseInt(sn);
  229. if (nn > no || sn.length > so.length) {
  230. return true;
  231. } else if (nn < no) {
  232. return false;
  233. }
  234. }
  235. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. },
  241. }
  242. }
  243. </script>
  244. <style scoped>
  245. /*#ifndef APP-NVUE*/
  246. view {
  247. display: flex;
  248. flex-direction: column;
  249. box-sizing: border-box;
  250. }
  251. /*#endif*/
  252. .updateBox {
  253. background-color: rgba(0, 0, 0, 0.6);
  254. z-index: 1000;
  255. position: fixed;
  256. right: 0px;
  257. top: 0;
  258. left: 0;
  259. bottom: 0;
  260. align-items: center;
  261. justify-content: center;
  262. }
  263. .update-wrap {
  264. width: 580rpx;
  265. height: 500rpx;
  266. border-radius: 10px;
  267. padding-bottom: 0px;
  268. position: relative;
  269. }
  270. .white-bg {
  271. position: absolute;
  272. top: 60rpx;
  273. left: 0px;
  274. width: 580rpx;
  275. height: 440rpx;
  276. background-color: #ffffff;
  277. border-bottom-left-radius: 30rpx;
  278. border-bottom-right-radius: 30rpx;
  279. }
  280. .updateCon-img {
  281. position: absolute;
  282. top: 0rpx;
  283. left: 0px;
  284. width: 580rpx;
  285. height: 440rpx;
  286. }
  287. .updateCon-top {
  288. padding: 70rpx 50rpx;
  289. /*#ifndef APP-NVUE*/
  290. z-index: 1;
  291. /*#endif*/
  292. }
  293. .update-top-title {
  294. color: #fff;
  295. font-size: 36rpx;
  296. font-weight: bold;
  297. }
  298. .update-top-version {
  299. color: #fff;
  300. font-size: 28rpx;
  301. margin-top: 10rpx;
  302. }
  303. .uodate-content {
  304. color: #333;
  305. font-size: 22rpx;
  306. padding: 0px 50rpx;
  307. margin-top: 80rpx;
  308. /*#ifndef APP-NVUE*/
  309. z-index: 1;
  310. /*#endif*/
  311. }
  312. .current-version {
  313. text-align: center;
  314. margin-top: 10rpx;
  315. font-size: 20rpx;
  316. color: #666;
  317. /*#ifndef APP-NVUE*/
  318. z-index: 1;
  319. /*#endif*/
  320. }
  321. .update-btn {
  322. position: absolute;
  323. left: 0px;
  324. bottom: 0px;
  325. width: 580rpx;
  326. height: 80rpx;
  327. padding: 0px 50rpx;
  328. align-items: center;
  329. flex-direction: row;
  330. border-top-color: #e7e7e7;
  331. border-top-style: solid;
  332. border-top-width: 1px;
  333. background-color: #fff;
  334. border-bottom-left-radius: 30rpx;
  335. border-bottom-right-radius: 30rpx;
  336. }
  337. .update-btn-item {
  338. width: 240rpx;
  339. height: 80rpx;
  340. justify-content: center;
  341. align-items: center;
  342. }
  343. .update-btn-left {
  344. border-right-color: #e7e7e7;
  345. border-right-style: solid;
  346. border-right-width: 1px;
  347. }
  348. .update-btn-item-text {
  349. text-align: center;
  350. font-size: 28rpx;
  351. color: #666;
  352. }
  353. .text-bule {
  354. color: #045FCF;
  355. }
  356. .sche-wrap {
  357. padding: 0px 50rpx 0rpx;
  358. height: 100rpx;
  359. align-items: center;
  360. border-bottom-left-radius: 30rpx;
  361. border-bottom-right-radius: 30rpx;
  362. flex: 1;
  363. justify-content: flex-end;
  364. }
  365. .sche-wrap-text {
  366. font-size: 24rpx;
  367. color: #666;
  368. margin-bottom: 20rpx;
  369. }
  370. .sche-bg {
  371. position: relative;
  372. background-color: #ccc;
  373. height: 20rpx;
  374. border-radius: 100px;
  375. width: 480rpx;
  376. margin-bottom: 30rpx;
  377. }
  378. .sche-bg-jindu {
  379. position: absolute;
  380. left: 0px;
  381. top: 0px;
  382. height: 20rpx;
  383. background-color: #5775e7;
  384. border-radius: 100px;
  385. }
  386. .sche-bg-round {
  387. position: absolute;
  388. left: 100%;
  389. height: 24rpx;
  390. width: 24rpx;
  391. background-color: #fff;
  392. border-color: #fff;
  393. border-style: solid;
  394. border-width: 10px;
  395. border-radius: 100px;
  396. transform: translateX(-20rpx) translateY(-10rpx);
  397. }
  398. .sche-bg-round-text {
  399. font-size: 24rpx;
  400. width: 120rpx;
  401. text-align: center;
  402. transform: translateX(-50rpx) translateY(-40rpx);
  403. color: #5775e7;
  404. }
  405. .uodate-close {
  406. /* display: flex; */
  407. flex-direction: row;
  408. justify-content: center;
  409. padding-top: 50rpx;
  410. }
  411. .uodate-close-img {
  412. width: 80rpx;
  413. height: 80rpx;
  414. }
  415. </style>