app-update.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="wrap">
  3. <!-- <view class="popup-bg" :style="getHeight">
  4. <view class="popup-content" :class="{'popup-content-show' : popup_show}">
  5. <view class="update-wrap">
  6. <image src="./images/img.png" class="top-img"></image>
  7. <view class="content">
  8. <text class="title">发现新版本V{{update_info.versionno}}</text>
  9. <view class="title-sub" v-html="update_info.title|| '版本更新'" style="font-weight: bold;"></view>
  10. <view class="title-sub" v-html="update_info.content || '版本更新'" style="padding-top: 8rpx;">
  11. </view>
  12. <button class="btn" v-if="downstatus < 1" @click="nowUpdate()">立即升级</button>
  13. <view class="sche-wrap" v-else>
  14. <view class="dis a-c j-s">
  15. <text style="color: #666;margin-right: 10rpx;font-size: 30rpx;">正在下载</text>
  16. <view class="sche-bg">
  17. <view class="sche-bg-jindu" :style="lengthWidth"></view>
  18. </view>
  19. <text style="color: #666;margin-left: 10rpx;font-size: 30rpx;">{{width}}%</text>
  20. </view>
  21. <text
  22. class="down-text">下载进度:{{(downSize/1024/1024 ).toFixed(2)}}M/{{(fileSize/1024/1024).toFixed(2)}}M</text>
  23. </view>
  24. </view>
  25. </view>
  26. <image src="./images/close.png" class="close-ioc" @click="closeUpdate()"></image>
  27. </view>
  28. </view> -->
  29. <uni-Popup :show="popup_show" @close='popup_show=false' :headerTitle="`晋掌柜 V${update_info.version}版本已准备好`"
  30. :closeable="false" :closeOnClickOverlay="false">
  31. <template #content>
  32. <view class="updateVersion">
  33. <text class="content" v-if="downstatus < 1">
  34. {{update_info.content || '版本更新'}}
  35. </text>
  36. <view class="sche-wrap" v-else>
  37. <view class="dis a-c j-s">
  38. <text style="color: #666;margin-right: 10rpx;font-size: 30rpx;">正在下载</text>
  39. <view class="sche-bg">
  40. <view class="sche-bg-jindu" :style="lengthWidth"></view>
  41. </view>
  42. <text style="color: #666;margin-left: 10rpx;font-size: 30rpx;">{{width}}%</text>
  43. </view>
  44. </view>
  45. <view class="footer dis a-c j-s ">
  46. <template v-if="downstatus < 1">
  47. <view class="dis a-c j-c" v-if="update_info.appType!=1" @tap="closeUpdate()">稍后再说</view>
  48. <view class="dis a-c j-c" style="margin-left: 28rpx;" @click="nowUpdate()">立即升级</view>
  49. </template>
  50. <view v-else class="dis a-c j-c">下载中</view>
  51. </view>
  52. </view>
  53. </template>
  54. </uni-Popup>
  55. </view>
  56. </template>
  57. <script>
  58. var vm;
  59. export default {
  60. name: "appUpdate",
  61. //@是否强制更新
  62. props: {
  63. },
  64. data() {
  65. return {
  66. popup_show: false, //弹窗是否显示
  67. platform: "", //ios or android
  68. version: null, //当前软件版本
  69. wgtLatestVersion: "", //最新资源
  70. apkLatestVersion: "", //最新整包
  71. need_update: false, // 是否更新
  72. downing: false, //是否下载中
  73. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  74. update_info: {},
  75. fileSize: 0, //文件大小
  76. downSize: 0, //已下载大小
  77. viewObj: null, //原生遮罩view
  78. force: true,
  79. width: "",
  80. wgtInfo: {}, //wgt最新安装包信息
  81. apkInfo: {}, //apk最新安装包更新信息
  82. };
  83. },
  84. created() {
  85. vm = this;
  86. },
  87. computed: {
  88. // 下载进度计算
  89. lengthWidth: function() {
  90. let w = this.downSize / this.fileSize * 100;
  91. let result = w;
  92. vm.width = Math.floor(result)
  93. if (!w) {
  94. w = 0
  95. } else {
  96. w = w.toFixed(2)
  97. }
  98. return {
  99. width: w + "%" //return 宽度百分比
  100. }
  101. },
  102. getHeight: function() {
  103. let bottom = 0;
  104. return {
  105. "bottom": bottom + 'px',
  106. "height": "auto"
  107. }
  108. }
  109. },
  110. methods: {
  111. // 检查更新
  112. update() {
  113. // #ifdef APP-PLUS
  114. // 获取手机系统信息
  115. uni.getSystemInfo({
  116. success: function(res) {
  117. vm.platform = res.platform //ios or android
  118. }
  119. });
  120. // 获取手机应用当前版本号
  121. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  122. vm.version = inf.version
  123. });
  124. vm.getUpdateInfo(); //获取最新版本信息
  125. // #endif
  126. vm.version = '1.0.6'
  127. vm.getUpdateInfo(); //获取最新版本信息
  128. },
  129. // 获取线上版本信息
  130. async getUpdateInfo() {
  131. //向后台发起请求,获取最新版本号
  132. let apkquote = await this.$http.get('/version/getMaxVersion', {
  133. appid: "__UNI__05D695B",
  134. packageType: "apk",
  135. });
  136. if (apkquote.code == 200 && apkquote.data) {
  137. vm.apkInfo = apkquote.data;
  138. vm.apkLatestVersion = apkquote.data.version;
  139. }
  140. let wgtquote = await this.$http.get('/version/getMaxVersion', {
  141. packageType: "wgt",
  142. appid: "__UNI__05D695B",
  143. });
  144. if (wgtquote.code == 200 && wgtquote.data) {
  145. vm.wgtInfo = wgtquote.data;
  146. vm.wgtLatestVersion = wgtquote.data.version;
  147. }
  148. vm.checkUpdate(); //检查是否更新
  149. },
  150. // 检查是否更新
  151. checkUpdate() {
  152. if (vm.version) {
  153. let apkneed = vm.compareVersion(vm.version, vm.apkLatestVersion); // 检查包版本
  154. let wgtneed = vm.compareVersion(vm.version, vm.wgtLatestVersion); // 检查资源版本
  155. if (apkneed) {
  156. vm.update_info = vm.apkInfo;
  157. if (vm.update_info.appType != 0) {
  158. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  159. this.$emit('status', vm.popup_show)
  160. }
  161. } else {
  162. if (wgtneed) {
  163. vm.update_info = vm.wgtInfo;
  164. if (vm.update_info.appType != 0) {
  165. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  166. this.$emit('status', vm.popup_show)
  167. }
  168. }
  169. }
  170. }
  171. },
  172. // 取消更新
  173. closeUpdate() {
  174. vm.popup_show = false; //关闭升级弹窗
  175. },
  176. // 立即更新
  177. nowUpdate() {
  178. if (vm.downing) return false; //如果正在下载就停止操作
  179. vm.downing = true; //状态改变 正在下载中
  180. // if (/\.apk$/.test(vm.update_info.path)) {
  181. // // 如果是apk地址
  182. // vm.download_wgt() // 安装包/升级包更新
  183. // } else if (/\.wgt$/.test(vm.update_info.path)) {
  184. // // 如果是更新包
  185. // vm.download_wgt() // 安装包/升级包更新
  186. // } else {
  187. // plus.runtime.openURL(vm.update_info.path, function() { //调用外部浏览器打开更新地址
  188. // plus.nativeUI.toast("打开错误");
  189. // });
  190. // }
  191. if (vm.update_info.path) {
  192. vm.download_wgt()
  193. }
  194. },
  195. // 下载升级资源包
  196. download_wgt() {
  197. plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  198. let options = {
  199. method: "get"
  200. };
  201. let dtask = plus.downloader.createDownload(vm.update_info.path, options, function(d, status) {
  202. });
  203. dtask.addEventListener("statechanged", function(task, status) {
  204. if (status === null) {} else if (status == 200) {
  205. vm.downstatus = task.state;
  206. switch (task.state) {
  207. case 3: // 已接收到数据
  208. vm.downSize = task.downloadedSize;
  209. if (task.totalSize) {
  210. vm.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  211. }
  212. break;
  213. case 4:
  214. vm.installWgt(task.filename); // 安装wgt包
  215. break;
  216. }
  217. } else {
  218. plus.nativeUI.closeWaiting();
  219. plus.nativeUI.toast("下载出错");
  220. vm.downing = false;
  221. vm.downstatus = 0;
  222. }
  223. });
  224. dtask.start();
  225. },
  226. // 安装文件
  227. installWgt(path) {
  228. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  229. plus.runtime.install(path, {}, function() {
  230. plus.nativeUI.closeWaiting();
  231. // 应用资源下载完成!
  232. plus.nativeUI.alert("应用资源下载完成!", function() {
  233. plus.runtime.restart();
  234. });
  235. }, function(e) {
  236. plus.nativeUI.closeWaiting();
  237. // 安装更新文件失败
  238. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  239. });
  240. },
  241. // 对比版本号
  242. compareVersion(ov, nv) {
  243. if (!ov || !nv || ov == "" || nv == "") {
  244. return false;
  245. }
  246. let b = false,
  247. ova = ov.split(".", 4),
  248. nva = nv.split(".", 4);
  249. for (let i = 0; i < ova.length && i < nva.length; i++) {
  250. let so = ova[i],
  251. no = parseInt(so),
  252. sn = nva[i],
  253. nn = parseInt(sn);
  254. if (nn > no || sn.length > so.length) {
  255. return true;
  256. } else if (nn < no) {
  257. return false;
  258. }
  259. }
  260. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. },
  266. }
  267. }
  268. </script>
  269. <style lang="scss" scoped>
  270. .popup-bg {
  271. display: flex;
  272. flex-direction: column;
  273. align-items: center;
  274. justify-content: center;
  275. position: fixed;
  276. top: 0;
  277. left: 0;
  278. right: 0;
  279. bottom: var(--window-bottom);
  280. width: 750rpx;
  281. background-color: rgba(0, 0, 0, 0.6);
  282. z-index: 99999999;
  283. }
  284. .popup-content {
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. }
  289. .popup-content-show {
  290. animation: mymove 500ms;
  291. transform: scale(1);
  292. }
  293. @keyframes mymove {
  294. 0% {
  295. transform: scale(0);
  296. /*开始为原始大小*/
  297. }
  298. 100% {
  299. transform: scale(1);
  300. }
  301. }
  302. .update-wrap {
  303. width: 580rpx;
  304. border-radius: 18rpx;
  305. position: relative;
  306. display: flex;
  307. flex-direction: column;
  308. background-color: #ffffff;
  309. padding: 170rpx 30rpx 0;
  310. .top-img {
  311. position: absolute;
  312. left: 0;
  313. width: 100%;
  314. height: 256rpx;
  315. top: -128rpx;
  316. }
  317. .content {
  318. display: flex;
  319. flex-direction: column;
  320. align-items: center;
  321. padding-bottom: 40rpx;
  322. .title {
  323. font-size: 32rpx;
  324. font-weight: bold;
  325. color: #3a63f6;
  326. }
  327. .title-sub {
  328. text-align: center;
  329. font-size: 24rpx;
  330. color: #666666;
  331. padding-top: 20rpx;
  332. }
  333. .btn {
  334. width: 460rpx;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. color: #ffffff;
  339. font-size: 30rpx;
  340. height: 80rpx;
  341. line-height: 80rpx;
  342. border-radius: 100px;
  343. background-color: #3a63f6;
  344. margin-top: 20rpx;
  345. }
  346. }
  347. }
  348. .close-ioc {
  349. width: 70rpx;
  350. height: 70rpx;
  351. margin-top: 30rpx;
  352. }
  353. .sche-wrap {
  354. width: 100%;
  355. padding: 10rpx 0 0;
  356. box-sizing: border-box;
  357. .sche-wrap-text {
  358. font-size: 24rpx;
  359. color: #666;
  360. margin-bottom: 20rpx;
  361. }
  362. .sche-bg {
  363. position: relative;
  364. background-color: #F8F8F8;
  365. height: 13rpx;
  366. border-radius: 100px;
  367. flex: 1;
  368. display: flex;
  369. align-items: center;
  370. .sche-bg-jindu {
  371. position: absolute;
  372. left: 0;
  373. top: 0;
  374. height: 13rpx;
  375. min-width: 40rpx;
  376. border-radius: 100px;
  377. // background: url(images/round.png) #5775e7 center right 4rpx no-repeat;
  378. background: #FDE935;
  379. // background-size: 26rpx 26rpx;
  380. }
  381. }
  382. .down-text {
  383. font-size: 24rpx;
  384. color: #5674e5;
  385. margin-top: 16rpx;
  386. }
  387. }
  388. .updateVersion {
  389. padding: 40rpx 40rpx 20rpx;
  390. //内容区
  391. .content {
  392. font-size: 30rpx;
  393. color: #666;
  394. }
  395. //底部
  396. .footer {
  397. margin-top: 58rpx;
  398. view {
  399. font-size: 30rpx;
  400. padding: 20rpx;
  401. flex: 1;
  402. }
  403. view:first-child {
  404. color: #333;
  405. background: transparent;
  406. border-radius: 50rpx;
  407. border: 1rpx solid #FDE935;
  408. }
  409. view:last-child {
  410. background: #FDE935;
  411. border-radius: 50rpx;
  412. color: #1F1F1F;
  413. }
  414. }
  415. }
  416. </style>