appUpdate.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <template>
  2. <view class="page-height">
  3. <view class="page-content">
  4. <view class="wrap" v-if="popup_show">
  5. <view class="popup-bg">
  6. <view class="popup-content" :class="{ 'popup-content-show': popup_show }">
  7. <view class="update-wrap">
  8. <image src="/static/images/img.png" class="top-img"></image>
  9. <view class="content">
  10. <text class="title">发现新版本V{{ updateInfo.version }}</text>
  11. <!-- 升级描述 -->
  12. <view class="title-sub" v-html="updateInfo.note"></view>
  13. <!-- 操作按钮 -->
  14. <view class="operation-btn" v-if="downstatus < 1">
  15. <button class="btn cancel" @click="cancel()">暂不升级</button>
  16. <button class="btn" @click="onUpdate()">立即升级</button>
  17. </view>
  18. <!-- 下载进度 -->
  19. <view class="sche-wrap" v-else>
  20. <!-- 更新包下载中 -->
  21. <view class="sche-bg">
  22. <view class="sche-bg-jindu" :style="lengthWidth"></view>
  23. </view>
  24. <text class="down-text">下载进度:{{ (downSize / 1024 / 1024).toFixed(2) }}M/{{ (fileSize / 1024 / 1024).toFixed(2) }}M</text>
  25. </view>
  26. </view>
  27. </view>
  28. <image src="/static/images/close.png" class="close-ioc" @click="closeUpdate()" v-if="downstatus < 1 && updateInfo.force == 0"></image>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { config } from '@/common/config.js';
  37. export default {
  38. data() {
  39. return {
  40. popup_show: true,
  41. updateInfo: {
  42. note: '',
  43. version: '',
  44. downloadUrl: ''
  45. }, //上一页面传过来的升级参数
  46. note: [], //升级说明数组格式
  47. fileSize: 0, //文件大小
  48. downSize: 0, //已下载大小
  49. downing: false, //是否下载中
  50. downstatus: 0 //0未下载 1已开始 2已连接到资源 3已接收到数据 4下载完成
  51. };
  52. },
  53. onLoad(option) {
  54. this.updateInfo.version = option.version;
  55. this.updateInfo.downloadUrl = option.downloadUrl;
  56. },
  57. onBackPress(e) {
  58. if (e.from == 'backbutton') return true; // APP安卓物理返回键逻辑
  59. },
  60. computed: {
  61. // 下载进度计算
  62. lengthWidth: function () {
  63. let w = (this.downSize / this.fileSize) * 100;
  64. if (!w) {
  65. w = 0;
  66. } else {
  67. w = w.toFixed(2);
  68. }
  69. return {
  70. width: w + '%' //return 宽度半分比
  71. };
  72. },
  73. getHeight: function () {
  74. let bottom = 0;
  75. if (this.tabbar) {
  76. bottom = 50;
  77. }
  78. return {
  79. bottom: bottom + 'px',
  80. height: 'auto'
  81. };
  82. }
  83. },
  84. methods: {
  85. // 当点击更新时
  86. onUpdate() {
  87. clearInterval(getApp().timer);
  88. //判断是否为wifi模式
  89. uni.getNetworkType({
  90. success: (res) => {
  91. if (res.networkType == 'wifi') {
  92. this.startUpdate(); //开始更新
  93. } else {
  94. uni.showModal({
  95. title: '提示',
  96. content: '当前网络非WIFI,继续更新可能会产生流量,确认要更新吗?',
  97. success: (modal_res) => {
  98. if (modal_res.confirm) {
  99. this.startUpdate(); //开始更新
  100. }
  101. }
  102. });
  103. }
  104. }
  105. });
  106. },
  107. /**
  108. * 取消升级
  109. */
  110. cancel() {
  111. clearInterval(getApp().timer);
  112. uni.$u.route({
  113. type: 'back',
  114. delta: 1
  115. });
  116. },
  117. //开始更新
  118. startUpdate() {
  119. if (this.downing) return false; //如果正在下载就停止操作
  120. this.downing = true; //状态改变 正在下载中
  121. if (/\.wgt$/.test(this.updateInfo.downloadUrl)) {
  122. // 如果是更新包
  123. this.download_wgt(); // 安装包/升级包更新
  124. } else {
  125. plus.runtime.openURL(this.updateInfo.downloadUrl, function () {
  126. //调用外部浏览器打开更新地址
  127. plus.nativeUI.toast('打开错误');
  128. });
  129. }
  130. },
  131. // 下载升级资源包
  132. download_wgt() {
  133. plus.nativeUI.showWaiting('下载更新文件...'); //下载更新文件...
  134. let options = {
  135. method: 'get'
  136. };
  137. let dtask = plus.downloader.createDownload(this.updateInfo.downloadUrl, options);
  138. dtask.addEventListener('statechanged', (task, status) => {
  139. if (status === null) {
  140. } else if (status == 200) {
  141. //在这里打印会不停的执行,请注意,正式上线切记不要在这里打印东西!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  142. this.downstatus = task.state;
  143. switch (task.state) {
  144. case 3: // 已接收到数据
  145. plus.nativeUI.closeWaiting();
  146. this.downSize = task.downloadedSize;
  147. if (task.totalSize) {
  148. this.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  149. }
  150. break;
  151. case 4:
  152. this.installWgt(task.filename); // 安装
  153. break;
  154. }
  155. } else {
  156. plus.nativeUI.closeWaiting();
  157. plus.nativeUI.toast('下载出错');
  158. this.downing = false;
  159. this.downstatus = 0;
  160. }
  161. });
  162. dtask.start();
  163. },
  164. // 安装文件
  165. installWgt(path) {
  166. plus.nativeUI.showWaiting('安装更新文件...'); //安装更新文件...
  167. plus.runtime.install(
  168. path,
  169. {
  170. force: true
  171. },
  172. function () {
  173. plus.nativeUI.closeWaiting();
  174. // 应用资源下载完成!
  175. plus.nativeUI.alert('更新完成,请重启APP!', function () {
  176. plus.runtime.restart(); //重启APP
  177. });
  178. },
  179. function (e) {
  180. plus.nativeUI.closeWaiting();
  181. // 安装更新文件失败
  182. plus.nativeUI.alert('安装更新文件失败[' + e.code + ']:' + e.message);
  183. }
  184. );
  185. }
  186. }
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .page-height {
  191. height: 100vh;
  192. display: flex;
  193. flex-direction: column;
  194. justify-content: center;
  195. align-items: center;
  196. background-color: rgba($color: #000000, $alpha: 0.7);
  197. }
  198. .popup-bg {
  199. display: flex;
  200. flex-direction: column;
  201. align-items: center;
  202. justify-content: center;
  203. position: fixed;
  204. top: 0;
  205. left: 0;
  206. right: 0;
  207. bottom: 0;
  208. width: 750rpx;
  209. }
  210. .popup-content {
  211. display: flex;
  212. flex-direction: column;
  213. align-items: center;
  214. }
  215. .popup-content-show {
  216. animation: mymove 300ms;
  217. transform: scale(1);
  218. }
  219. @keyframes mymove {
  220. 0% {
  221. transform: scale(0);
  222. /*开始为原始大小*/
  223. }
  224. 100% {
  225. transform: scale(1);
  226. }
  227. }
  228. .update-wrap {
  229. width: 580rpx;
  230. border-radius: 18rpx;
  231. position: relative;
  232. display: flex;
  233. flex-direction: column;
  234. background-color: #ffffff;
  235. padding: 170rpx 30rpx 0;
  236. .top-img {
  237. position: absolute;
  238. left: 0;
  239. width: 100%;
  240. height: 256rpx;
  241. top: -128rpx;
  242. }
  243. .content {
  244. display: flex;
  245. flex-direction: column;
  246. align-items: center;
  247. padding-bottom: 40rpx;
  248. .title {
  249. font-size: 32rpx;
  250. font-weight: bold;
  251. color: #6526f3;
  252. }
  253. .title-sub {
  254. text-align: center;
  255. font-size: 24rpx;
  256. color: #666666;
  257. padding: 30rpx 0;
  258. }
  259. .operation-btn {
  260. width: 100%;
  261. display: flex;
  262. justify-content: space-between;
  263. align-items: center;
  264. }
  265. .btn {
  266. width: 200rpx;
  267. display: flex;
  268. align-items: center;
  269. justify-content: center;
  270. color: #ffffff;
  271. font-size: 30rpx;
  272. height: 80rpx;
  273. line-height: 80rpx;
  274. border-radius: 100px;
  275. background-color: #6526f3;
  276. margin-top: 20rpx;
  277. }
  278. .cancel {
  279. background-color: #cccccc;
  280. color: #6526f3;
  281. }
  282. }
  283. }
  284. .close-ioc {
  285. width: 70rpx;
  286. height: 70rpx;
  287. margin-top: 30rpx;
  288. }
  289. .sche-wrap {
  290. display: flex;
  291. flex-direction: column;
  292. align-items: center;
  293. justify-content: flex-end;
  294. padding: 10rpx 50rpx 0;
  295. .sche-wrap-text {
  296. font-size: 24rpx;
  297. color: #666;
  298. margin-bottom: 20rpx;
  299. }
  300. .sche-bg {
  301. position: relative;
  302. background-color: #cccccc;
  303. height: 30rpx;
  304. border-radius: 100px;
  305. width: 480rpx;
  306. display: flex;
  307. align-items: center;
  308. .sche-bg-jindu {
  309. position: absolute;
  310. left: 0;
  311. top: 0;
  312. height: 30rpx;
  313. min-width: 40rpx;
  314. border-radius: 100px;
  315. background: url(/static/images/round.png) #6526f3 center right 4rpx no-repeat;
  316. background-size: 26rpx 26rpx;
  317. }
  318. }
  319. .down-text {
  320. font-size: 24rpx;
  321. color: #6526f3;
  322. margin-top: 16rpx;
  323. }
  324. }
  325. </style>