<template>
|
<view class="pages-version-car-program-download">
|
<view>
|
<a-button type="primary" class="max-button" :disabled="downloading"
|
@click="clickCheckUpdate">{{translate('check_update')}}</a-button>
|
|
</view>
|
|
<view class="group" v-if="newPack.version">
|
<view class="item big-text ">
|
{{translate('updatable_version')}}:{{newPack.file_time}}.{{newPack.version}}
|
</view>
|
<view class="item gray-text">
|
{{newPack.size}}
|
</view>
|
|
<view class="item">
|
{{translate('current_version')}}:{{svrVersion.date}}_{{svrVersion.ver}}
|
</view>
|
</view>
|
<view class="no-version gray-text" v-else>
|
<uni-icons type="upload-filled" size="150" color="#888"></uni-icons>
|
{{translate('current_version')}}:{{svrVersion.date}}_{{svrVersion.ver}}
|
</view>
|
|
<view class="button-group" v-if="newPack.version">
|
<a-button type="primary" class="button" :disabled="downloading"
|
@click="clickNowUpdate">{{translate('download_immediately')}}</a-button>
|
<cmdProgress v-if="downloading" :percent="percentage"
|
stroke-color="linear-gradient(to right, #ef32d9, #89fffd)">
|
</cmdProgress>
|
</view>
|
|
</view>
|
</template>
|
<script>
|
import {
|
session,
|
showToast,
|
showModal
|
} from "@/comm/utils.js"
|
import {
|
Button
|
} from 'antd-mobile-vue-next'
|
import {
|
getServerVersion,
|
downloadUpdatePackage
|
} from "@/api/version.js"
|
import TaskInit from "@/comm/extend.js"
|
import cmdProgress from "@/components/cmd-progress/index.vue"
|
export default {
|
name: "PagesVersionCarProgramDownload",
|
components: {
|
'a-button': Button,
|
cmdProgress
|
},
|
data() {
|
return {
|
ip: "",
|
svrVersion: {
|
date: "",
|
ver: ""
|
},
|
newPack: {
|
version: "",
|
fileName: "",
|
size: "",
|
info: "",
|
fileTime: "",
|
},
|
percentage: 0, //下载进度
|
downloading: false
|
|
}
|
},
|
onLoad(option) {
|
|
this.ip = option.ip || ""
|
uni.setNavigationBarTitle({
|
title: this.translate('version_update')
|
})
|
this.loadData()
|
|
},
|
computed: {
|
newVersionSize() {
|
const packSize = this.newPack?.size || 0
|
let size = 0
|
if (packSize < 1024) {
|
size = Math.round(packSize * 100)
|
return `${size/100} KB`
|
} else if (packSize < 1024 * 1024) {
|
size = Math.round(packSize * 100 / 1024)
|
return `${size/100} MB`
|
} else {
|
size = Math.round(packSize * 100 / (1024 * 1024 * 1024))
|
return `${size/100} GB`
|
}
|
}
|
|
},
|
methods: {
|
setData(obj) {
|
let that = this;
|
let keys = [];
|
let val, data;
|
|
Object.keys(obj).forEach(function(key) {
|
keys = key.split(".");
|
val = obj[key];
|
data = that.$data;
|
keys.forEach(function(key2, index) {
|
if (index + 1 == keys.length) {
|
that.$set(data, key2, val);
|
} else {
|
if (!data[key2]) {
|
that.$set(data, key2, {});
|
}
|
}
|
data = data[key2];
|
});
|
});
|
},
|
loadData() {
|
const _this = this
|
// #ifdef APP-PLUS
|
plus.runtime.getProperty(plus.runtime.appid, (info) => {
|
// console.log(info);
|
const verStr = info.version || "";
|
const arVer = verStr.split("_")
|
if (arVer.length > 1) {
|
_this.setData({
|
svrVersion: {
|
date: arVer[0],
|
ver: arVer[1]
|
}
|
})
|
} else if (arVer.length == 1) {
|
_this.setData({
|
svrVersion: {
|
date: "",
|
ver: arVer[0]
|
}
|
})
|
|
}
|
|
|
});
|
//#endif
|
},
|
updateAPP(fileName, ver) {
|
let _this = this;
|
let sys = uni.getSystemInfoSync().platform //检查系统
|
|
downloadUpdatePackage(fileName, (path) => {
|
var pathArr = fileName.split(".")
|
const fileExt = pathArr.pop()
|
const fileName2 = `EasyGO_${ver}.${fileExt}`
|
TaskInit.fileUtils.clearSavedFiles("car_version")
|
TaskInit.fileUtils.moveToLocal(path, "car_version", fileName2).then((
|
res) => {
|
console.log("保存成功:", res)
|
showToast(this.translate('download_success'));
|
}).catch((err) => {
|
console.log("保存失败:", err)
|
showToast(_this.translate("failed_save_file"))
|
})
|
_this.downloading = false
|
}, (res) => {
|
console.log('下载进度' + res.progress);
|
_this.percentage = res.progress
|
}, (err) => {
|
showToast(this.translate('update_fail'));
|
_this.downloading = false
|
})
|
},
|
|
async checkAppVersion(version) {
|
try {
|
|
const listVer = await getServerVersion() || []
|
const svrVer = listVer[0] || {}
|
console.log("downloadUrl", svrVer)
|
|
const fileName = svrVer.file_name || ""
|
if (fileName) {
|
if (!svrVer.version) return
|
const newVer = `${svrVer.version }`
|
var isver = this.compareVersion(svrVer.version || "", version);
|
if (isver) {
|
this.newPack.version = svrVer.version
|
this.newPack.fileName = fileName
|
this.newPack.fileTime = svrVer.file_time
|
this.newPack.size = svrVer.size || ""
|
this.newPack.info =appVer.info
|
} else {
|
this.newPack = {
|
version: "",
|
fileName: "",
|
size: "",
|
info: "",
|
fileTime: "",
|
}
|
showToast(this.translate("is_latest_version"))
|
console.log('当前已是最新版本')
|
}
|
}
|
} catch (ex) {
|
showError(ex)
|
this.newPack = {
|
version: "",
|
fileName: "",
|
size: "",
|
info: "",
|
fileTime: "",
|
}
|
}
|
},
|
compareVersion(curV, reqV) {
|
return curV > reqV
|
var arr1 = curV.toString().split('.');
|
var arr2 = reqV.toString().split('.');
|
//将两个版本号拆成数字
|
var minL = Math.min(arr1.length, arr2.length);
|
var pos = 0; //当前比较位
|
var diff = 0; //当前为位比较是否相等
|
var flag = false;
|
//逐个比较如果当前位相等则继续比较下一位
|
while (pos < minL) {
|
diff = parseInt(arr1[pos]) - parseInt(arr2[pos]);
|
if (diff == 0) {
|
pos++;
|
continue;
|
} else if (diff > 0) {
|
flag = true;
|
break;
|
} else {
|
flag = false;
|
break;
|
}
|
}
|
return flag;
|
},
|
|
clickCheckUpdate(e) {
|
|
this.checkAppVersion(this.svrVersion)
|
|
},
|
|
clickNowUpdate() {
|
this.updateAPP(this.newPack.fileName)
|
},
|
translate(t) {
|
if (typeof this.$t == "function") return this.$t(`page.${t}`)
|
else return t;
|
},
|
}
|
}
|
</script>
|
|
<style lang="scss">
|
.pages-version-car-program-download {
|
display: flex;
|
width: 750rpx;
|
height: 100vh;
|
flex-direction: column;
|
|
.group {
|
width: calc(100% - 40rpx);
|
// border: 2rpx solid #ccc;
|
// background-color: #fff;
|
border-radius: 20rpx;
|
margin: 20rpx;
|
padding: 0 20rpx;
|
display: flex;
|
flex-direction: column;
|
|
.item {
|
width: 100%;
|
padding: 20rpx 10rpx;
|
display: flex;
|
flex-direction: row;
|
align-items: center;
|
|
.right {
|
flex: 1;
|
text-align: right;
|
color: #888;
|
}
|
}
|
|
|
}
|
|
.tip {
|
color: #888;
|
padding: 10rpx;
|
margin-left: 50rpx;
|
}
|
|
.gray-text {
|
color: #888;
|
}
|
|
.big-text {
|
font-size: 40rpx;
|
}
|
|
.no-version {
|
margin: auto;
|
display: flex;
|
flex-direction: column;
|
}
|
|
.button-group {
|
display: flex;
|
justify-content: center;
|
align-items: center;
|
flex-direction: column;
|
font-size: 30rpx !important;
|
|
|
}
|
|
.max-button {
|
margin: auto;
|
margin-top: 20rpx;
|
margin-bottom: 20rpx;
|
border-radius: 40rpx;
|
height: 80rpx;
|
line-height: 60rpx;
|
width: 600rpx;
|
}
|
|
.button {
|
margin: auto;
|
margin-top: 20rpx;
|
border-radius: 10rpx;
|
height: 50rpx;
|
line-height: 32rpx;
|
width: 400rpx;
|
}
|
|
}
|
</style>
|