uni-app框架实现自动更新并安装【完美解决】
参考:http://ask.dcloud.net.cn/article/35096uni-app客户端代码:<script>//App.vue文件export default {data() {return {version: "1.0"}}...
·
参考:http://ask.dcloud.net.cn/article/35096
uni-app客户端代码:
<script>
// App.vue文件
export default {
data() {
return {
version: "1.0"
}
},
onLaunch: function() {
console.log("App Launch");
this.checkVersionUpdate({
version: this.version,
})
this.getStorageAttributeToVueprototype();
},
methods: {
// 如果客户端的版本不是最新的,提示更新且自动下载最新的app
async checkVersionUpdate (formData) {
// 这里最好使用原生的uniRequest请求方法【因为执行这段代码之前可能还没创建active_wallet对象】
// #ifdef APP-PLUS
let resData = await this.$uniRequest(this.$serverConfig.common.version_update, "POST", formData);
console.log(JSON.stringify(resData));
if (resData && resData.isUpdate) {
let openUrl = plus.os.name === "iOS" ? resData.downloadIOSUrl : resData.downloadAndroidUrl;
// 提醒用户更新
this.$uniShowModal(
resData.note ? resData.note : "是否选择更新", () => {
// 最核心的地方,自动打开下载安装【且覆盖之前的安装】
plus.runtime.openURL(openUrl);
}, "true", "更新提示");
}
// #endif
},
},
};
</script>
服务器端代码
@api.route("/version_update", methods=["POST"])
# 必传参数【version】,更新客户端版本到最新
def version_update():
form_data = request.get_json()
GlobalUtils.check_field_exit_dict(form_data, "version")
if not GlobalValidator.is_price_type(form_data["version"]) and GlobalValidator.is_price_type(
current_app.config["VERSION"]):
raise Fail("version版本必须是价格类型【如:1.01小数或小数字符串类型】")
version_dict = {
"isUpdate": True,
"downloadAndroidUrl": current_app.config["DOWNLOAD_ANDROID_URL"],
"downloadIOSUrl": current_app.config["DOWNLOAD_IOS_URL"],
"note": "版本已更新,请下载最新版本",
} if form_data["version"] < current_app.config["VERSION"] else {
"isUpdate": False,
"note": "当前已经是最新版本",
}
return GlobalUtils.jsonify_support_none_and_decimal(version_dict)
更多推荐
已为社区贡献42条内容
所有评论(0)