app-update.vue 10 KB

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