feat: 版本更新
This commit is contained in:
@@ -76,13 +76,6 @@
|
||||
link: 'navigateTo',
|
||||
to: '/pages/home/pass',
|
||||
},
|
||||
{
|
||||
id: '12',
|
||||
title: '银行卡',
|
||||
clickable: true,
|
||||
link: 'navigateTo',
|
||||
to: '/pages/home/bankCard',
|
||||
},
|
||||
{
|
||||
id: '5',
|
||||
title: '隐私政策',
|
||||
|
||||
+152
-168
@@ -6,186 +6,170 @@
|
||||
</view>
|
||||
<view class="center"> 最新版本号: {{ newversion }} </view>
|
||||
<view class="center m-t-50"> 当前版本号: {{ version }} </view>
|
||||
<!-- #ifdef APP-PLUS -->
|
||||
<view class="flex-center m-t-100 absolute b50 w100 p-x-32">
|
||||
<view class="ai-button2 m-r-20" @click="downloadFile()">更新最新版本</view>
|
||||
<view class="ai-button2 m-r-20" @click="onClickUpdate()">更新最新版本</view>
|
||||
<view class="ai-button2" @click="copy()">复制APP下载链接</view>
|
||||
</view>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
|
||||
<view v-if="showUpdateProgress" class="update-progress-mask">
|
||||
<view class="update-progress-card">
|
||||
<view class="update-progress-title">{{ updateProgressText }}</view>
|
||||
<view class="update-progress-bar">
|
||||
<view class="update-progress-bar__fill" :style="{ width: updateProgress + '%' }"></view>
|
||||
</view>
|
||||
<view class="update-progress-percent">{{ updateProgress }}%</view>
|
||||
</view>
|
||||
</view>
|
||||
</s-pages>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted, onUnmounted, getCurrentInstance, watch, nextTick } from 'vue';
|
||||
import { gethotupdatelist, getAndroidAppUrl } from '@/sheep/api/index/index';
|
||||
import sheep from '@/sheep';
|
||||
let { proxy } = getCurrentInstance();
|
||||
let newversion = ref('');
|
||||
let version = ref('');
|
||||
let filePath = ref();
|
||||
let androidUrl = ref(); //安卓apk地址
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { createHotUpdate, downloadAndOpenApk } from '@/utils/hotUpdate'
|
||||
import { getAndroidAppUrl, gethotupdatelist } from '@/sheep/api/index/index'
|
||||
|
||||
const wgtUp = async () => {
|
||||
// console.log(plus.runtime);
|
||||
let res = await gethotupdatelist();
|
||||
if (res.code !== 0) return;
|
||||
filePath.value = res.data.filePath;
|
||||
let localVersion = uni.getSystemInfoSync().appWgtVersion;
|
||||
version.value = localVersion;
|
||||
newversion.value = res.data.versionNumber;
|
||||
if (res.data.versionNumber != localVersion) {
|
||||
if (res.data.type == 'min') {
|
||||
if (res.data.isForceUpdate) {
|
||||
// 强制更新
|
||||
downloadFile();
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '发现新的版本,是否更新?',
|
||||
confirmColor: '#FA7E31',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
downloadFile();
|
||||
} else if (res.cancel) {
|
||||
if (res.data.isForceUpdate) {
|
||||
plus.runtime.quit(); //退出应用
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
} else {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '发现新的版本,请到立即更新到最新版本',
|
||||
confirmText: '立即更新',
|
||||
cancelText: res.data.isForceUpdate ? '关闭' : '取消',
|
||||
confirmColor: '#FA7E31',
|
||||
const newversion = ref('')
|
||||
const version = ref('')
|
||||
const appVersion = ref('')
|
||||
const wgtVersion = ref('')
|
||||
const updateInfoRef = ref({})
|
||||
const apkPath = ref('')
|
||||
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
updateApp(filePath.value);
|
||||
} else if (res.cancel) {
|
||||
if (res.data.isForceUpdate) {
|
||||
plus.runtime.quit(); //退出应用
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
const updateApp = (url) => {
|
||||
uni.showLoading({
|
||||
title: '下载中',
|
||||
});
|
||||
uni.downloadFile({
|
||||
url: url, //下载地址
|
||||
success: (res) => {
|
||||
console.log(res, '下载成功');
|
||||
if (res.statusCode === 200) {
|
||||
console.log('下载成功');
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '下载成功',
|
||||
icon: 'success',
|
||||
});
|
||||
uni.saveFile({
|
||||
tempFilePath: res.tempFilePath,
|
||||
success: function (res) {
|
||||
uni.openDocument({
|
||||
filePath: res.savedFilePath,
|
||||
success: function (res) {
|
||||
console.log(res, '打开安装包');
|
||||
},
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log(err, '打开安装包-失败');
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
fail(e) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '下载失败,请检查网络',
|
||||
icon: 'none',
|
||||
});
|
||||
console.log(e);
|
||||
},
|
||||
});
|
||||
};
|
||||
const copy = () => {
|
||||
uni.setClipboardData({
|
||||
data: androidUrl.value,
|
||||
success: () => {
|
||||
uni.showToast({
|
||||
title: '链接已复制,请在浏览器打开',
|
||||
icon: 'none',
|
||||
});
|
||||
},
|
||||
});
|
||||
};
|
||||
const downloadFile = () => {
|
||||
if (newversion.value == version.value) {
|
||||
uni.showToast({ title: '已经是最新版本', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
let url = filePath.value;
|
||||
uni.downloadFile({
|
||||
url: url,
|
||||
success: (downloadResult) => {
|
||||
uni.showToast({
|
||||
title: '更新中....',
|
||||
icon: 'none',
|
||||
});
|
||||
if (downloadResult) {
|
||||
if (downloadResult.statusCode === 200) {
|
||||
plus.runtime.install(
|
||||
downloadResult.tempFilePath,
|
||||
{
|
||||
force: true,
|
||||
},
|
||||
function () {
|
||||
uni.showModal({
|
||||
title: '成功提示',
|
||||
content: '热更新成功,请重启应用',
|
||||
confirmColor: '#FA7E31',
|
||||
showCancel: false,
|
||||
success: function (res) {
|
||||
if (res.confirm) {
|
||||
plus.runtime.restart(); //自动重启软件
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
function (e) {
|
||||
uni.showModal({
|
||||
title: '失败提示',
|
||||
content: e,
|
||||
showCancel: false,
|
||||
confirmColor: '#FA7E31',
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
};
|
||||
const showUpdateProgress = ref(false)
|
||||
const updateProgress = ref(0)
|
||||
const updateProgressText = ref('下载中...')
|
||||
|
||||
//获取安卓apk
|
||||
function getAndroid() {
|
||||
getAndroidAppUrl().then((res) => {
|
||||
console.log(res.data);
|
||||
androidUrl.value = res.data || 'http://8.136.55.200:1080/app.apk';
|
||||
});
|
||||
const progress = {
|
||||
show: (text) => {
|
||||
showUpdateProgress.value = true
|
||||
updateProgress.value = 0
|
||||
updateProgressText.value = String(text || '下载中...')
|
||||
},
|
||||
set: (p, text) => {
|
||||
updateProgress.value = Math.max(0, Math.min(100, Number(p) || 0))
|
||||
if (text) updateProgressText.value = String(text)
|
||||
},
|
||||
hide: () => {
|
||||
showUpdateProgress.value = false
|
||||
},
|
||||
}
|
||||
|
||||
const hotUpdate = createHotUpdate({
|
||||
progress,
|
||||
apkMode: 'download',
|
||||
getLatest: gethotupdatelist,
|
||||
})
|
||||
|
||||
const wgtUp = async () => {
|
||||
const payload = await hotUpdate.fetchLatest()
|
||||
if (!payload) return
|
||||
const { updateInfo, localWgtVersion, localAppVersion, localCompareVersion, latestVersion } = payload
|
||||
updateInfoRef.value = updateInfo || {}
|
||||
wgtVersion.value = String(localWgtVersion || '')
|
||||
appVersion.value = String(localAppVersion || '')
|
||||
version.value = String(localCompareVersion || localAppVersion || localWgtVersion || '')
|
||||
newversion.value = String(latestVersion || '')
|
||||
apkPath.value = String(updateInfo?.apkPath || '')
|
||||
}
|
||||
|
||||
const onClickUpdate = async () => {
|
||||
await wgtUp()
|
||||
await hotUpdate.promptAndUpdate(updateInfoRef.value, { source: 'manual' })
|
||||
}
|
||||
|
||||
const copy = async () => {
|
||||
if (!apkPath.value) {
|
||||
try {
|
||||
const res = await getAndroidAppUrl({})
|
||||
apkPath.value = String(res?.data || '')
|
||||
} catch (e) {}
|
||||
}
|
||||
onMounted(() => {
|
||||
getAndroid();
|
||||
wgtUp();
|
||||
});
|
||||
if (!apkPath.value) {
|
||||
uni.showToast({ title: '暂无下载链接', icon: 'none' })
|
||||
return
|
||||
}
|
||||
const url = apkPath.value
|
||||
uni.setClipboardData({
|
||||
data: url,
|
||||
success: () => {
|
||||
uni.showToast({ title: '链接已复制', icon: 'none' })
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '是否立即下载更新?',
|
||||
confirmText: '立即下载',
|
||||
cancelText: '暂不下载',
|
||||
confirmColor: 'var(--color2)',
|
||||
success: (res) => {
|
||||
if (!res?.confirm) return
|
||||
downloadAndOpenApk(apkPath.value, { progress })
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
// #ifdef APP-PLUS
|
||||
wgtUp()
|
||||
// #endif
|
||||
})
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
.pages {
|
||||
}
|
||||
|
||||
.update-progress-mask {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0, 0, 0, 0.45);
|
||||
z-index: 9999;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 40rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.update-progress-card {
|
||||
width: 100%;
|
||||
max-width: 560rpx;
|
||||
background: #fff;
|
||||
border-radius: 16rpx;
|
||||
padding: 32rpx;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.update-progress-title {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.update-progress-bar {
|
||||
height: 18rpx;
|
||||
background: rgba(0, 0, 0, 0.08);
|
||||
border-radius: 999rpx;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.update-progress-bar__fill {
|
||||
height: 100%;
|
||||
background: var(--color2);
|
||||
border-radius: 999rpx;
|
||||
width: 0%;
|
||||
transition: width 0.2s linear;
|
||||
}
|
||||
|
||||
.update-progress-percent {
|
||||
font-size: 28rpx;
|
||||
color: #333;
|
||||
text-align: center;
|
||||
margin-top: 16rpx;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user