app-update.vue 10 KB

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