app-update.vue 11 KB

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