appUpdate.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. //判断是否为wifi模式
  88. uni.getNetworkType({
  89. success: (res) => {
  90. if (res.networkType == 'wifi') {
  91. this.startUpdate(); //开始更新
  92. } else {
  93. uni.showModal({
  94. title: '提示',
  95. content: '当前网络非WIFI,继续更新可能会产生流量,确认要更新吗?',
  96. success: (modal_res) => {
  97. if (modal_res.confirm) {
  98. this.startUpdate(); //开始更新
  99. }
  100. }
  101. });
  102. }
  103. }
  104. });
  105. },
  106. /**
  107. * 取消升级
  108. */
  109. cancel() {
  110. uni.$u.route({
  111. type: 'back',
  112. delta: 1
  113. });
  114. },
  115. //开始更新
  116. startUpdate() {
  117. if (this.downing) return false; //如果正在下载就停止操作
  118. this.downing = true; //状态改变 正在下载中
  119. if (/\.wgt$/.test(this.updateInfo.downloadUrl)) {
  120. // 如果是更新包
  121. this.download_wgt(); // 安装包/升级包更新
  122. } else {
  123. plus.runtime.openURL(this.updateInfo.downloadUrl, function () {
  124. //调用外部浏览器打开更新地址
  125. plus.nativeUI.toast('打开错误');
  126. });
  127. }
  128. },
  129. // 下载升级资源包
  130. download_wgt() {
  131. plus.nativeUI.showWaiting('下载更新文件...'); //下载更新文件...
  132. let options = {
  133. method: 'get'
  134. };
  135. let dtask = plus.downloader.createDownload(this.updateInfo.downloadUrl, options);
  136. dtask.addEventListener('statechanged', (task, status) => {
  137. if (status === null) {
  138. } else if (status == 200) {
  139. //在这里打印会不停的执行,请注意,正式上线切记不要在这里打印东西!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  140. this.downstatus = task.state;
  141. switch (task.state) {
  142. case 3: // 已接收到数据
  143. plus.nativeUI.closeWaiting();
  144. this.downSize = task.downloadedSize;
  145. if (task.totalSize) {
  146. this.fileSize = task.totalSize; //服务器须返回正确的content-length才会有长度
  147. }
  148. break;
  149. case 4:
  150. this.installWgt(task.filename); // 安装
  151. break;
  152. }
  153. } else {
  154. plus.nativeUI.closeWaiting();
  155. plus.nativeUI.toast('下载出错');
  156. this.downing = false;
  157. this.downstatus = 0;
  158. }
  159. });
  160. dtask.start();
  161. },
  162. // 安装文件
  163. installWgt(path) {
  164. plus.nativeUI.showWaiting('安装更新文件...'); //安装更新文件...
  165. plus.runtime.install(
  166. path,
  167. {
  168. force: true
  169. },
  170. function () {
  171. plus.nativeUI.closeWaiting();
  172. // 应用资源下载完成!
  173. plus.nativeUI.alert('更新完成,请重启APP!', function () {
  174. plus.runtime.restart(); //重启APP
  175. });
  176. },
  177. function (e) {
  178. plus.nativeUI.closeWaiting();
  179. // 安装更新文件失败
  180. plus.nativeUI.alert('安装更新文件失败[' + e.code + ']:' + e.message);
  181. }
  182. );
  183. }
  184. }
  185. };
  186. </script>
  187. <style lang="scss" scoped>
  188. .page-height {
  189. height: 100vh;
  190. display: flex;
  191. flex-direction: column;
  192. justify-content: center;
  193. align-items: center;
  194. background-color: rgba($color: #000000, $alpha: 0.7);
  195. }
  196. .popup-bg {
  197. display: flex;
  198. flex-direction: column;
  199. align-items: center;
  200. justify-content: center;
  201. position: fixed;
  202. top: 0;
  203. left: 0;
  204. right: 0;
  205. bottom: 0;
  206. width: 750rpx;
  207. }
  208. .popup-content {
  209. display: flex;
  210. flex-direction: column;
  211. align-items: center;
  212. }
  213. .popup-content-show {
  214. animation: mymove 300ms;
  215. transform: scale(1);
  216. }
  217. @keyframes mymove {
  218. 0% {
  219. transform: scale(0);
  220. /*开始为原始大小*/
  221. }
  222. 100% {
  223. transform: scale(1);
  224. }
  225. }
  226. .update-wrap {
  227. width: 580rpx;
  228. border-radius: 18rpx;
  229. position: relative;
  230. display: flex;
  231. flex-direction: column;
  232. background-color: #ffffff;
  233. padding: 170rpx 30rpx 0;
  234. .top-img {
  235. position: absolute;
  236. left: 0;
  237. width: 100%;
  238. height: 256rpx;
  239. top: -128rpx;
  240. }
  241. .content {
  242. display: flex;
  243. flex-direction: column;
  244. align-items: center;
  245. padding-bottom: 40rpx;
  246. .title {
  247. font-size: 32rpx;
  248. font-weight: bold;
  249. color: #6526f3;
  250. }
  251. .title-sub {
  252. text-align: center;
  253. font-size: 24rpx;
  254. color: #666666;
  255. padding: 30rpx 0;
  256. }
  257. .operation-btn {
  258. width: 100%;
  259. display: flex;
  260. justify-content: space-between;
  261. align-items: center;
  262. }
  263. .btn {
  264. width: 200rpx;
  265. display: flex;
  266. align-items: center;
  267. justify-content: center;
  268. color: #ffffff;
  269. font-size: 30rpx;
  270. height: 80rpx;
  271. line-height: 80rpx;
  272. border-radius: 100px;
  273. background-color: #6526f3;
  274. margin-top: 20rpx;
  275. }
  276. .cancel {
  277. background-color: #cccccc;
  278. color: #6526f3;
  279. }
  280. }
  281. }
  282. .close-ioc {
  283. width: 70rpx;
  284. height: 70rpx;
  285. margin-top: 30rpx;
  286. }
  287. .sche-wrap {
  288. display: flex;
  289. flex-direction: column;
  290. align-items: center;
  291. justify-content: flex-end;
  292. padding: 10rpx 50rpx 0;
  293. .sche-wrap-text {
  294. font-size: 24rpx;
  295. color: #666;
  296. margin-bottom: 20rpx;
  297. }
  298. .sche-bg {
  299. position: relative;
  300. background-color: #cccccc;
  301. height: 30rpx;
  302. border-radius: 100px;
  303. width: 480rpx;
  304. display: flex;
  305. align-items: center;
  306. .sche-bg-jindu {
  307. position: absolute;
  308. left: 0;
  309. top: 0;
  310. height: 30rpx;
  311. min-width: 40rpx;
  312. border-radius: 100px;
  313. background: url(/static/images/round.png) #6526f3 center right 4rpx no-repeat;
  314. background-size: 26rpx 26rpx;
  315. }
  316. }
  317. .down-text {
  318. font-size: 24rpx;
  319. color: #6526f3;
  320. margin-top: 16rpx;
  321. }
  322. }
  323. </style>