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