uni-app框架实现自动更新并安装【完美解决】

参考: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)

 

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值