app-update.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <template>
  2. <view class="wrap">
  3. <view class="popup-bg" :class="{'popup-show' : popup_show}" :style="getHeight">
  4. <view class="update-wrap">
  5. <view class="update-content">
  6. <view class="update-con-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="downstatus < 1">更新内容:<br>{{update_info.note}}</text>
  12. <text class="current-version" v-if="downstatus < 1">当前版本:V{{version}}</text>
  13. <view class="update-btn" v-if="downstatus < 1">
  14. <view class="btn-item" @click="closeUpdate" v-if="update_info.minimum_Version === version">
  15. <!-- 残忍拒绝 -->
  16. <text class="item-text ">残忍拒绝</text>
  17. </view>
  18. <view class="btn-item" @click="nowUpdate">
  19. <!-- 立即升级 -->
  20. <text class="item-text text-bule">立即升级</text>
  21. </view>
  22. </view>
  23. <!-- 下载进度 -->
  24. <view class="sche-wrap" v-else>
  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. <view class="updata-bg-img"></view>
  38. <view class="updata-bg-color"></view>
  39. </view>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. let vm;
  45. export default {
  46. name: "appUpdate",
  47. //@是否强制更新
  48. props: {
  49. force: {
  50. type: Boolean,
  51. default: true, //true强制更新 false非强制更新
  52. },
  53. tabbar: {
  54. type: Boolean,
  55. default: false, //是否有原生tabbar组件
  56. }
  57. },
  58. data() {
  59. return {
  60. popup_show: false, //弹窗是否显示
  61. platform: "", //ios or android
  62. version: "1.0.0", //当前软件版本
  63. need_update: false, // 是否更新
  64. downing: false, //是否下载中
  65. downstatus: 0, //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  66. update_info: {
  67. os: '', //设备系统
  68. version: '', //最新版本
  69. note: '', //升级说明
  70. },
  71. schedule: 0, //下载进度宽度
  72. downloadedSize: 0, //下载大小
  73. viewObj: null, //原生遮罩view
  74. };
  75. },
  76. created() {
  77. vm = this;
  78. },
  79. computed: {
  80. // 下载进度计算
  81. lengthWidth: function() {
  82. return {
  83. width: vm.schedule * 480 / 100 + "rpx"
  84. }
  85. },
  86. getHeight: function() {
  87. let bottom = 0;
  88. if (vm.tabbar) {
  89. bottom = 50;
  90. }
  91. return {
  92. "bottom": bottom + 'px'
  93. }
  94. }
  95. },
  96. methods: {
  97. // 检查更新
  98. update() {
  99. if (vm.viewObj) vm.viewObj.hide() //隐藏原生遮罩
  100. // console.log('父组件触发方法');
  101. // #ifdef APP-PLUS
  102. // 获取手机系统信息
  103. uni.getSystemInfo({
  104. success: function(res) {
  105. vm.platform = res.platform //ios or android
  106. // console.log('手机系统信息', vm.platform);
  107. }
  108. });
  109. // 获取版本号
  110. plus.runtime.getProperty(plus.runtime.appid, function(inf) {
  111. vm.version = inf.version
  112. });
  113. // console.log('当前版本', vm.version);
  114. vm.getUpdateInfo(); //获取更新信息
  115. // #endif
  116. },
  117. // 获取线上版本信息
  118. getUpdateInfo() {
  119. //向后台发起请求,获取最新版本号
  120. let integer = ''
  121. switch (uni.getSystemInfoSync().platform) {
  122. case 'android':
  123. integer = '1'
  124. break;
  125. case 'ios':
  126. integer = '2'
  127. break;
  128. default:
  129. console.log('运行在其他设备上')
  130. break;
  131. }
  132. vm.$u.get('/appVersion/version/getAppVersion', {
  133. integer: integer
  134. }).then(res => {
  135. // console.log(res)
  136. // 这里的返回的数据跟后台约定
  137. vm.update_info = {
  138. os: uni.getSystemInfoSync().platform,
  139. version: res.data.version, //最新版本
  140. note: res.data.plus_msg, //升级提示
  141. download_url: res.data.url, //下载地址
  142. minimum_Version: res.data.minimum_Version //最低版本
  143. };
  144. // 循环获取当前设备对应的更新数据
  145. // for (let i = 0; i < data.length; i++) {
  146. // if (vm.platform == data[i]['os']) {
  147. // vm.update_info = data[i];
  148. // console.log(data[i])
  149. // break;
  150. // }
  151. // }
  152. if (!vm.update_info.os) {
  153. // 后台未配置当前系统的升级数据
  154. } else {
  155. // console.log('后台返回的当前系统的升级参数', vm.update_info);
  156. vm.checkUpdate(); ///检查是否更新
  157. }
  158. })
  159. },
  160. // 检查是否更新
  161. checkUpdate() {
  162. vm.need_update = vm.compareVersion(vm.version, vm.update_info.version); // 检查是否需要升级
  163. // console.log(vm.need_update)
  164. if (vm.need_update) {
  165. vm.popup_show = true; //线上版本号大于当前安装的版本号 显示升级框
  166. if (vm.tabbar) {
  167. //页面是否有原生tabbar组件
  168. // 创建原生view用来遮罩tabbar的点击事件 (如果是没有用原生的tabbar这一步可以取消)
  169. vm.viewObj = new plus.nativeObj.View('viewObj', {
  170. bottom: '0px',
  171. left: '0px',
  172. height: '50px',
  173. width: '100%',
  174. backgroundColor: "rgba(0,0,0,.6)"
  175. });
  176. vm.viewObj.show() //显示原生遮罩
  177. }
  178. }
  179. },
  180. // 取消更新
  181. closeUpdate() {
  182. if (vm.force) {
  183. // 强制更新,取消退出app
  184. plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication()
  185. .performSelector(
  186. "exit");
  187. } else {
  188. vm.popup_show = false; //关闭升级弹窗
  189. if (vm.tabbar) vm.viewObj.hide() //隐藏原生遮罩
  190. }
  191. },
  192. // 立即更新
  193. nowUpdate() {
  194. if (vm.downing) {
  195. return false //如果正在下载就停止操作
  196. } else {
  197. vm.downing = true; //状态改变 正在下载中
  198. }
  199. if (/\.apk$/.test(vm.update_info.download_url)) {
  200. // 如果是apk地址
  201. vm.download_wgt() // 安装包/升级包更新
  202. } else if (/\.wgt$/.test(vm.update_info.download_url)) {
  203. // 如果是更新包
  204. vm.download_wgt() // 安装包/升级包更新
  205. } else {
  206. plus.runtime.openURL(vm.update_info.download_url, function() { //调用外部浏览器打开更新地址
  207. plus.nativeUI.toast("升级地址有误,请联系管理员修改");
  208. });
  209. }
  210. },
  211. // 下载升级资源包
  212. download_wgt() {
  213. // plus.nativeUI.showWaiting("下载更新文件..."); //下载更新文件...
  214. let options = {
  215. method: "get"
  216. };
  217. let dtask = plus.downloader.createDownload(vm.update_info.download_url, options, function(d, status) {
  218. });
  219. dtask.addEventListener("statechanged", function(task, status) {
  220. if (status === null) {} else if (status == 200) {
  221. //在这里打印会不停的执行,请注意,正式上线切记不要在这里打印东西///////////////////////////////////////////////////
  222. vm.downstatus = task.state;
  223. switch (task.state) {
  224. case 3: // 已接收到数据
  225. vm.downloadedSize = task.downloadedSize;
  226. let totalSize = 0;
  227. if (task.totalSize) {
  228. totalSize = task.totalSize //服务器须返回正确的content-length才会有长度
  229. }
  230. vm.schedule = parseInt(100 * task.downloadedSize / totalSize);
  231. break;
  232. case 4:
  233. vm.installWgt(task.filename); // 安装wgt包
  234. break;
  235. }
  236. } else {
  237. plus.nativeUI.closeWaiting();
  238. plus.nativeUI.toast("下载出错");
  239. vm.downing = false;
  240. vm.downstatus = 0;
  241. }
  242. });
  243. dtask.start();
  244. },
  245. // 安装文件
  246. installWgt(path) {
  247. plus.nativeUI.showWaiting("安装更新文件..."); //安装更新文件...
  248. plus.runtime.install(path, {}, function() {
  249. plus.nativeUI.closeWaiting();
  250. // 应用资源下载完成!
  251. plus.nativeUI.alert("应用资源下载完成!", function() {
  252. plus.runtime.restart();
  253. });
  254. }, function(e) {
  255. plus.nativeUI.closeWaiting();
  256. // 安装更新文件失败
  257. plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message);
  258. });
  259. },
  260. // 对比版本号
  261. compareVersion(ov, nv) {
  262. if (!ov || !nv || ov == "" || nv == "") {
  263. return false;
  264. }
  265. let b = false,
  266. ova = ov.split(".", 4),
  267. nva = nv.split(".", 4);
  268. for (let i = 0; i < ova.length && i < nva.length; i++) {
  269. let so = ova[i],
  270. no = parseInt(so),
  271. sn = nva[i],
  272. nn = parseInt(sn);
  273. if (nn > no || sn.length > so.length) {
  274. return true;
  275. } else if (nn < no) {
  276. return false;
  277. }
  278. }
  279. if (nva.length > ova.length && 0 == nv.indexOf(ov)) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. },
  285. }
  286. }
  287. </script>
  288. <style lang="scss" scoped>
  289. .popup-bg {
  290. display: flex;
  291. align-items: center;
  292. justify-content: center;
  293. position: fixed;
  294. z-index: -9999;
  295. opacity: 0;
  296. top: 0;
  297. left: 0;
  298. right: 0;
  299. bottom: 0;
  300. height: 100%;
  301. width: 100%;
  302. background-color: rgba(0, 0, 0, .6);
  303. transition: opacity 300ms;
  304. }
  305. .popup-show {
  306. z-index: 9999;
  307. opacity: 1;
  308. }
  309. .update-wrap {
  310. width: 580rpx;
  311. border-radius: 10rpx;
  312. position: relative;
  313. display: flex;
  314. flex-direction: column;
  315. overflow: hidden;
  316. .updata-bg-img {
  317. position: absolute;
  318. top: 0vw;
  319. left: 0px;
  320. width: 580rpx;
  321. height: 440rpx;
  322. background: url(images/update-img.png) no-repeat;
  323. background-size: 100% 100%;
  324. z-index: 1;
  325. }
  326. .updata-bg-color {
  327. position: absolute;
  328. width: 580rpx;
  329. top: 10vw;
  330. height: calc(100% - 10vw);
  331. background-color: #ffffff;
  332. z-index: 0;
  333. }
  334. .update-content {
  335. position: relative;
  336. z-index: 2;
  337. display: flex;
  338. flex-direction: column;
  339. }
  340. .update-con-top {
  341. padding: 70rpx 50rpx;
  342. }
  343. .update-btn {
  344. height: 80rpx;
  345. display: flex;
  346. align-items: center;
  347. border-top: 1px solid #e7e7e7;
  348. margin-top: 20rpx;
  349. .btn-item {
  350. display: flex;
  351. align-items: center;
  352. justify-content: center;
  353. flex: 1;
  354. .item-text {
  355. text-align: center;
  356. font-size: 28rpx;
  357. color: #666;
  358. }
  359. .text-bule {
  360. color: #045FCF;
  361. }
  362. }
  363. .btn-item:first-child {
  364. border-right: 1px solid #e7e7e7
  365. }
  366. }
  367. .update-top-title {
  368. color: #fff;
  369. font-size: 34rpx;
  370. font-weight: bold;
  371. }
  372. .update-top-version {
  373. color: #fff;
  374. font-size: 28rpx;
  375. margin-top: 10rpx;
  376. }
  377. .uodate-content {
  378. color: #333;
  379. font-size: 30rpx;
  380. padding: 0px 50rpx;
  381. margin-top: 120rpx;
  382. }
  383. .current-version {
  384. text-align: center;
  385. margin-top: 20rpx;
  386. font-size: 24rpx;
  387. color: #666;
  388. margin-bottom: 16rpx;
  389. }
  390. }
  391. .sche-wrap {
  392. display: flex;
  393. flex-direction: column;
  394. align-items: center;
  395. justify-content: flex-end;
  396. padding: 160rpx 50rpx 40rpx;
  397. .sche-wrap-text {
  398. font-size: 24rpx;
  399. color: #666;
  400. margin-bottom: 20rpx;
  401. }
  402. .sche-bg {
  403. position: relative;
  404. background-color: #ccc;
  405. height: 20rpx;
  406. border-radius: 100px;
  407. width: 480rpx;
  408. display: flex;
  409. align-items: center;
  410. margin-bottom: 30rpx;
  411. }
  412. .sche-bg-jindu {
  413. position: absolute;
  414. left: 0;
  415. top: 0;
  416. height: 20rpx;
  417. background-color: #5775e7;
  418. border-radius: 100px;
  419. .sche-bg-round {
  420. position: absolute;
  421. left: 100%;
  422. height: 24rpx;
  423. width: 24rpx;
  424. background-color: #fff;
  425. border-color: #fff;
  426. border-style: solid;
  427. border-width: 10rpx;
  428. border-radius: 100px;
  429. transform: translateX(-20rpx) translateY(-10rpx);
  430. box-shadow: 0 0 4px #eaeaea;
  431. .sche-bg-round-text {
  432. position: relative;
  433. top: 24rpx;
  434. font-size: 24rpx;
  435. text-align: center;
  436. color: #5775e7;
  437. }
  438. }
  439. }
  440. }
  441. </style>