app更新——自动更新 配置

148 阅读2分钟

app更新——自动更新 配置

返回数据

"data":
    {
        "code":1000,
        "msg":"操作成功",
        "data":{
                "id":"",
                "appName":"666", // 包名称                         
                "appVersion":"1.0.0", // 更新版本信息
                "filename":"",
                "downloadUrl":"包线上地址", // 需 先将文件上传,后返回线上地址(接口)
                "forceUpdate":0, // 是否强制更新
                "appSys":"android", //平台
                "updateTip":"123", // 描述
                "defaultVersion":1, //是否默认
                "createTime":"2022-09-15 11:36:06" 
            },
        "encrypt":0,
        "error":false,
        "ok":true
},

调用函数

    import { updateApp }from '@/api/index.js'
    import autoUpdater from '@/utils/autoUpdater.min.js'
    getNewV() {
                uni.getSystemInfo({
                 success: res => {
                  if (res.platform == "android") {
                   plus.runtime.getProperty(plus.runtime.appid,async (data) => {
                    const res = await updateApp(参数) // 调用接口
                    // console.log('版本更新',res);
                    //获取当前app版本号
                     console.log('最新版本号',res.data.data.appVersion);
                     console.log('本机版本号',data.version);
                     if (
                      Number(
                       res.data.data.appVersion
                       .split(".")
                       .filter(item => item !== ".")
                       .join("")
                      ) > Number(data.version.split(".")
                       .filter(item => item !== ".")
                       .join(""))
                     ) {
                      try {
                        // console.log('版本更新地址',res.data.data.downloadUrl);
                       autoUpdater.init({
                        packageUrl: res.data.data.downloadUrl,
                        content: `更新内容:${res.data.data.updateTip}\n更新版本号:${res.data.data.appVersion}\n`,
                           contentAlign: "left",
                           cancelColor: "#999",
                           confirmColor: "#0054d8",
                           showCancel: res.data.data.forceUpdate === 0 // 是否显示取消键
                       })
                          this.$nextTick(() => {
                              autoUpdater.show()
                          })
                      } 
                         catch (error) {
                             console.log('版本更新错误',error);
                             autoUpdater.close()
                         }
                     }
                   })
                  }
                 }
                })
},

autoUpdater.min.js 包

"use strict"
Object.defineProperty(exports, "__esModule", { value: !0 })
var _maskView,
    _contentView,
    _downloadTask,
    _loadingProgress,
    _screenHeight,
    _screenWidth,
    _config = {
        logoUrl: "/static/logo.png",
        releaseNotes: "",
        noteAglin: "left",
        loadingColor: "#ff6666",
        cancelText: "\u53D6\u6D88",
        showCancel: false,
        cancelColor: "#000000",
        confirmText: "\u5347\u7EA7",
        confirmColor: "#ff6666",
        windowHeight: 240,
        packageUrl: "",
        browser: !1
    },
    _calculatePosition = function () {
        var a = (_screenHeight - _config.windowHeight) / 2,
            b = 0.1 * _screenWidth * 2,
            c = _screenWidth - 2 * b
        return { top: a, left: b, width: c, right: b }
    },
    _createMask = function () {
        _maskView = new plus.nativeObj.View("maskView", {
            top: "0px",
            left: "0px",
            width: "100%",
            height: "100%",
            backgroundColor: "rgba(0,0,0,0.2)"
        })
    },
    _createContentView = function () {
        var a = _calculatePosition()
        ;(_contentView = new plus.nativeObj.View("contentView", {
            top: a.top + "px",
            left: a.left + "px",
            height: _config.windowHeight + "px",
            width: a.width + "px"
        })),
            _contentView.drawRect({ color: "#ffffff", radius: "10px" }, { width: "100%", height: "100%" }, "roundedRect")
        var b = (a.width - 50) / 2
        _contentView.drawBitmap(_config.logoUrl, {}, { top: "10px", width: "50px", height: "50px", left: b + "px" }, "autoUpdaterIcon"),
            _contentView.drawText(
                _config.releaseNotes,
                { height: _config.windowHeight - 100 + "px", left: "20px", right: "10px", top: "20px" },
                { size: "25px", color: "#2f2f2f", align: _config.noteAglin, verticalAlign: "top", whiteSpace: "normal", overflow: "ellipsis" },
                "autoUpdaterContent"
            ),
            _createLoading(-2)
        var c = _config.windowHeight - 50 + 15
        if (_config.showCancel) {
            _contentView.drawRichText(
                '<font style="font-size:25px;" color="' + _config.cancelColor + '">' + _config.cancelText + "</font>",
                { width: "50%", top: c + "px", left: "0px" },
                {
                    align: "center",
                    onClick: function () {
                        close()
                    }
                },
                "cancel"
            )
        }
        _contentView.drawRichText(
            '<font color="' + _config.confirmColor + '" style="font-size:23px;">' + _config.confirmText + "</font>",
            { width: "50%", right: "0px", top: c + "px"},
            {
                align: "center",
                onClick: function () {
                    _config.packageUrl
                        ? "android" === plus.os.name.toLowerCase()
                            ? _config.browser
                                ? (plus.runtime.openURL(_config.packageUrl), close())
                                : _createTask()
                            : plus.runtime.openURL(_config.packageUrl)
                        : uni.showToast({ title: "\u5B89\u88C5\u5305\u5730\u5740\u4E3A\u7A7A", icon: "none" })
                }
            },
            "submit"
        )
    },
    _createLoading = function (a) {
        var b = _calculatePosition(),
            c = _config.windowHeight - 60,
            d = (b.width - 120) / 2,
            e = 0 <= a ? ((b.width - 100) / 100) * a : 0
        e = parseInt(e)
        var f = 100 <= a ? "\u4E0B\u8F7D\u5B8C\u6210" : "\u4E0B\u8F7D\u4E2D...",
            g = ""
        ;(g = -1 == a ? "\u8D44\u6E90\u52A0\u8F7D\u4E2D..." : 0 <= a ? f + "(" + a + "%)" : ""),
            _contentView.drawRect({ color: _config.loadingColor }, { width: e + "px", height: "3px", left: "10px", top: c + 8 + "px" }, "loading"),
            _contentView.drawRichText(
                '<font color="' + _config.loadingColor + '">' + g + "</font>",
                { width: "100px", top: c + "px", left: e + "px" },
                { align: "center" },
                "loadingText"
            )
    },
    _createTask = function () {
        return _downloadTask
            ? void console.log("\u6B63\u5728\u4E0B\u8F7D\u4E2D")
            : void (_createLoading(-1),
              (_downloadTask = uni.downloadFile({
                  url: _config.packageUrl,
                  success: function (a) {
                      if (200 === a.statusCode) {
                          var b = a.tempFilePath
                          uni.saveFile({
                              tempFilePath: b,
                              success: function (a) {
                                  plus.runtime.install(a.savedFilePath, { force: !0 }), close()
                              }
                          })
                      }
                  }
              })),
              _downloadTask.onProgressUpdate(function (a) {
                  _loadingProgress != a.progress && ((_loadingProgress = a.progress), _createLoading(a.progress))
              }))
    },
    init = function (a) {
        var b = a.windowHeight,
            c = a.logo,
            d = a.content,
            e = a.contentAlign,
            f = a.loadingColor,
            g = a.cancel,
            h = a.cancelColor,
            i = a.confirm,
            j = a.confirmColor,
            k = a.packageUrl,
            l = a.browser,
            m = a.showCancel
        ;(_screenHeight = plus.screen.resolutionHeight),
            (_screenWidth = plus.screen.resolutionWidth),
            (_downloadTask = null),
            b && (_config.windowHeight = b),
            c && (_config.logoUrl = c),
            d && (_config.releaseNotes = d),
            e && (_config.noteAglin = e),
            f && (_config.loadingColor = f),
            g && (_config.cancelText = g),
            h && (_config.cancelColor = h),
            i && (_config.confirmText = i),
            j && (_config.confirmColor = j),
            k && (_config.packageUrl = k),
            l && (_config.browser = l),
            m && (_config.showCancel = m),
            _createMask(),
            _createContentView()
    },
    show = function () {
        _maskView && _maskView.show(), _contentView && _contentView.show()
    },
    close = function () {
        _downloadTask && (_downloadTask.abort(), (_downloadTask = null), _createLoading(-2)),
            _maskView && _maskView.hide(),
            _contentView && _contentView.hide()
    }
exports.default = { init: init, show: show, close: close }
​

\