app-updatebak.vue 11 KB

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