update.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <view>
  3. <navbar ref="hxnb" :config="navConfig" backColor="#666"></navbar>
  4. <view class="flex-column">
  5. <view class="content">
  6. <image class="logo" src="@/static/logo-icon.png" />
  7. <view class="mainInfo">
  8. <text class="title">{{ info }} | {{ Minfo }}</text>
  9. <text class="tip">\n{{ Mtip }}</text>
  10. </view>
  11. <text class="line" />
  12. </view>
  13. <view>
  14. <view class="infoContentTitle">
  15. <!-- <image class="infoPic" src="../../static/info-circle.png" /> -->
  16. <text class="infoTitle">更新摘要</text>
  17. </view>
  18. <view class="infoContent">
  19. <text class="updateInfo">{{ updateInfo }}</text>
  20. </view>
  21. </view>
  22. <view v-if="!currentIsLatest">
  23. <view class="infoContentTitle">
  24. <!-- <image class="infoPic" src="../../static/sync.png" /> -->
  25. <text class="infoTitle">更新大小</text>
  26. </view>
  27. <view class="infoContent">
  28. <text class="updateInfo">{{ packgeSize }}</text>
  29. </view>
  30. </view>
  31. <view class="minorContent bottom_aera">
  32. <view v-if="startProgress && !currentIsLatest" class="smallTitle">
  33. <text>下载进度:{{ downloadProgress }}%</text>
  34. <progress :percent="downloadProgress" stroke-width="4" />
  35. </view>
  36. <button class="btn" v-if="!startProgress && !currentIsLatest" type="primary" @click="handleUpdate()">立即更新</button>
  37. <button class="btn" v-if="currentIsLatest" :loading="buttonLoading" type="primary" @click="getLatest()">检查更新</button>
  38. </view>
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. export default {
  44. components: {},
  45. data() {
  46. return {
  47. navConfig: {
  48. back:true,
  49. title: '检查更新',
  50. color: '#1a1a1a',
  51. statusBarFontColor: '#000000',
  52. //背景颜色;参数一:透明度(0-1);参数二:背景颜色(array则为线性渐变,string为单色背景)
  53. backgroundColor: [1, '#FFFFFF']
  54. },
  55. info: '正在加载', // 主标题显示版本号
  56. Minfo: 'release', // 副标题显示版本类型
  57. Mtip: '', // 小提示标语
  58. updateInfo: '无', // 更新摘要
  59. verInfo: null, // 版本信息
  60. packgeSize: null, // 更新包大小
  61. packgePath: null, // 更新包的文件地址
  62. downloadTask: null, // 下载任务
  63. downloadProgress: 0, // 下载进度
  64. buttonLoading: false, // 加载 - 标记
  65. installed: false, // 是否执行了安装 - 标记
  66. startProgress: false, // 下载进行 - 标记
  67. currentIsLatest: true, // 当前版本就是最新版本 - 标记
  68. version: null
  69. };
  70. },
  71. onLoad() {
  72. const updated = uni.getStorageSync('updated');
  73. if (updated.packgePath) {
  74. this.packgePath = updated.packgePath;
  75. }
  76. plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
  77. this.version = wgtinfo.versionCode;
  78. this.getLatest();
  79. });
  80. },
  81. // 如果用户下载后没有完成安装,却回到app,则执行这里
  82. onShow() {
  83. if (this.installed === true && this.packgePath) {
  84. this.installed = false;
  85. this.haveDownloaded();
  86. }
  87. },
  88. // 用户关闭页面时检查是否存在下载任务
  89. onUnload() {
  90. if (this.downloadTask) {
  91. this.closeTask();
  92. this.showToast('更新被取消');
  93. }
  94. },
  95. // 下拉刷新更新
  96. onPullDownRefresh() {
  97. this.getLatest();
  98. uni.stopPullDownRefresh();
  99. },
  100. methods: {
  101. // 封装个Toast方便用
  102. showToast(text) {
  103. uni.showToast({
  104. title: text,
  105. duration: 3000,
  106. icon: 'none'
  107. });
  108. },
  109. installPackge() {
  110. // 安装更新
  111. plus.runtime.install(this.packgePath, {
  112. force: true
  113. });
  114. this.installed = true;
  115. // 保存更新记录到stroage,方便下次启动app时删除安装包
  116. uni.setStorage({
  117. key: 'updated',
  118. data: {
  119. completed: true,
  120. packgePath: this.packgePath
  121. },
  122. success: res => {
  123. console.log('成功保存更新记录');
  124. // console.log(res.data)
  125. }
  126. });
  127. // 判断是否为热更新(判断文件名中是否含有.wgt)
  128. console.log(8888888888888888888888 + this.packgePath);
  129. if (this.packgePath.match(RegExp(/.wgt/))) {
  130. console.log("9999999999999")
  131. this.installed = false;
  132. uni.showModal({
  133. title: '提示',
  134. content: '应用将重启以完成更新',
  135. showCancel: false,
  136. complete: () => {
  137. plus.runtime.restart();
  138. }
  139. });
  140. }
  141. },
  142. // 已经下载了更新包但是没有安装
  143. haveDownloaded() {
  144. uni.showModal({
  145. title: '更新尚未完成',
  146. content: '您已下载更新包,但是还没有完成安装,请问是否要继续安装更新包呢?',
  147. success: res => {
  148. if (res.confirm) {
  149. // 安装
  150. this.installPackge();
  151. } else if (res.cancel) {
  152. this.showToast('更新被取消');
  153. }
  154. }
  155. });
  156. },
  157. // 取得最新版本及其所有信息
  158. async getLatest() {
  159. console.log("gengxinyemian")
  160. this.info = '正在加载'; // 主标题显示版本号
  161. this.Minfo = '未知'; // 副标题显示版本类型
  162. this.updateInfo = '无'; // 更新摘要
  163. this.buttonLoading = true;
  164. this.verInfo = null;
  165. let res = await this.$http.get('/app/config/info');
  166. if (res.code == 200) {
  167. this.buttonLoading = false;
  168. console.log("gengxinxixi"+JSON.stringify(res.data))
  169. this.info = res.data.name;
  170. this.Minfo = res.data.ver;
  171. this.updateInfo = res.data.info;
  172. this.packgeSize = res.data.packge_size + 'MB';
  173. this.verInfo = res.data;
  174. if (this.verInfo) {
  175. this.checkLatest(this.verInfo);
  176. }
  177. }
  178. },
  179. // 检查版本
  180. checkLatest(verInfo) {
  181. console.log(222222222222222222222222222);
  182. console.log(JSON.stringify(verInfo.ver) + this.version);
  183. if (!verInfo.ver) {
  184. this.Mtip = '未找到发行版本';
  185. } else if (this.version < verInfo.number) {
  186. // 当前版本与新版本不符($current在main.js里)
  187. this.currentIsLatest = false;
  188. this.Mtip = '发现新版本';
  189. } else {
  190. this.showToast('当前是最新版了');
  191. this.currentIsLatest = true;
  192. this.Mtip = '当前是最新版';
  193. }
  194. },
  195. // 关闭下载任务
  196. closeTask() {
  197. this.downloadTask.abort();
  198. this.downloadTask = null;
  199. this.startProgress = false;
  200. },
  201. // 开始下载任务
  202. createTask(downloadLink) {
  203. console.log("下载链接"+downloadLink)
  204. this.downloadProgress = 0;
  205. this.startProgress = true;
  206. // 创建下载任务对象
  207. this.downloadTask = uni.downloadFile({
  208. url: downloadLink,
  209. success: res => {
  210. if (res.statusCode === 200) {
  211. // 保存下载的安装包
  212. uni.saveFile({
  213. tempFilePath: res.tempFilePath,
  214. success: res => {
  215. this.packgePath = res.savedFilePath;
  216. // 进行安装
  217. this.installPackge();
  218. // 任务完成,关闭下载任务
  219. this.closeTask();
  220. }
  221. });
  222. }
  223. }
  224. });
  225. this.downloadTask.onProgressUpdate(res => {
  226. this.downloadProgress = res.progress;
  227. console.log(res.progress)
  228. });
  229. },
  230. handleUpdate() {
  231. // 判断系统类型
  232. if (plus.os.name.toLowerCase() === 'android') {
  233. if (this.verInfo.android_link && this.verInfo.android_link !== '#') {
  234. this.createTask(this.verInfo.android_link);
  235. } else {
  236. this.showToast('未找到下载地址');
  237. }
  238. } else {
  239. if (this.verInfo.ios_link && this.verInfo.ios_link !== '#') {
  240. // 我这里默认#也是没有地址,请根据业务自行修改
  241. // 苹果(A):进行热更新(如果iosUrl是wgt更新包的下载地址)判断文件名中是否含有.wgt
  242. if (this.verInfo.ios_link.match(RegExp(/.wgt/))) {
  243. this.createTask(this.verInfo.ios_link);
  244. } else {
  245. // 苹果(B):打开商店链接(如果iosUrl是苹果商店的地址)
  246. plus.runtime.openURL(this.verInfo.ios_link);
  247. }
  248. } else {
  249. this.showToast('未找到ios商店地址');
  250. }
  251. }
  252. }
  253. }
  254. };
  255. </script>
  256. <style lang="scss">
  257. .btn {
  258. background:#053520 !important;
  259. }
  260. .flex-column {
  261. display: flex;
  262. flex-direction: column;
  263. }
  264. .content {
  265. text-align: center;
  266. padding: 20upx;
  267. background-color: #efeff4;
  268. }
  269. .minorContent {
  270. width: 650upx;
  271. padding: 0 50upx;
  272. }
  273. .process {
  274. margin-top: 200upx;
  275. margin-left: 30%;
  276. }
  277. .logo {
  278. margin: 0 auto;
  279. height: 216upx;
  280. width: 216upx;
  281. }
  282. .title {
  283. font-size: 36upx;
  284. color: #373737;
  285. font-weight: bold;
  286. }
  287. .infoTitle {
  288. font-size: 32upx;
  289. color: #373737;
  290. padding-left: 15upx;
  291. font-weight: bold;
  292. }
  293. .tip {
  294. font-size: 28upx;
  295. color: #7e7e83;
  296. vertical-align: text-top;
  297. }
  298. .bottom_aera {
  299. position: absolute;
  300. bottom: 0;
  301. height: 12%;
  302. }
  303. .line {
  304. /* padding: 0 600upx; */
  305. display: inline-block;
  306. border-bottom: 2upx solid #d8d8d8;
  307. width: 100%;
  308. }
  309. .infoPic {
  310. height: 50upx;
  311. width: 50upx;
  312. }
  313. .infoContentTitle {
  314. display: flex;
  315. align-items: center;
  316. padding: 40upx 40upx;
  317. }
  318. .infoContent {
  319. display: flex;
  320. align-items: center;
  321. }
  322. .updateInfo {
  323. font-size: 28upx;
  324. color: #7e7e83;
  325. padding: 0 80upx;
  326. }
  327. .smallTitle {
  328. font-size: 26upx;
  329. font-weight: 500;
  330. padding: 20upx 0;
  331. line-height: 1.5;
  332. color: #888;
  333. }
  334. </style>