123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- const UPDATE_URL = 'http://192.168.1.150:8443/updateInfo/'
- /**
- * 版本号比较
- * @param {Object} v1 eg: 11.0.2
- * @param {Object} v2 eg: 11.0.1
- * @return {Number} -1 => v1 < v2
- * 1 => v1 > v2
- * 0 => v1 = v2
- */
- const compareVersion = function(v1, v2) {
- if (v1 === v2) return 0
- v1 = v1.split('.')
- v2 = v2.split('.')
- const len = Math.max(v1.length, v2.length)
- while (v1.length < len) {
- v1.push('0')
- }
- while (v2.length < len) {
- v2.push('0')
- }
- for (let i = 0; i < len; i++) {
- const num1 = parseInt(v1[i])
- const num2 = parseInt(v2[i])
- if (num1 > num2) {
- return 1
- } else if (num1 < num2) {
- return -1
- }
- }
- return 0
- }
- export default class UpdateApp {
- constructor() {
- this.force = false // 是否为强制更新
- this.restart = true // 安装完成后是否需要强制重启
- }
- check() {
- const that = this
- return new Promise((resolve, reject) => {
- plus.runtime.getProperty(
- plus.runtime.appid,
- function(widgetInfo) {
- uni.request({
- url: UPDATE_URL,
- data: {
- version: widgetInfo.version,
- appid: plus.runtime.appid
- },
- success: (result) => {
- // force: true, // 是否为强制更新
- // restart: true, // 安装完成后是否需要强制重启
- // androidWgtUrl: '****.wgt', // android wgt包地址(热更新时使用)
- // iosWgtUrl: '', // ios wgt包更新地址(热更新时使用)
- // androidPkgUrl: '', // android apk 地址(全量更新时使用)
- // iosPkgUrl: '', // ios 应用商店地址(全量更新时使用,用于直接跳转到应用商店)
- // remark: '1、用户体验优化\n2、BUG修复'
- const res = result.data.data
- // version: widgetInfo.version,
- // appid: plus.runtime.appid
- const updateFlag = compareVersion(res.version, widgetInfo.version)
- if (updateFlag === 0) {
- resolve({ downloadUrl: null })
- return
- }
- that.force = res.force
- that.restart = res.restart
- const sys = uni.getSystemInfoSync()
- let downloadUrl = ''
- if (sys.platform === 'android') {
- downloadUrl = res.androidWgtUrl || res.androidPkgUrl
- } else if (sys.platform === 'ios') {
- downloadUrl = res.iosWgtUrl || res.iosPkgUrl
- }
-
- resolve({
- ...res,
- downloadUrl: downloadUrl
- })
- }
- })
- }
- )
- })
- }
- /**
- * 下载安装更新包
- * @param {String} data
- */
- install(data) {
- const sys = uni.getSystemInfoSync()
- if (sys.platform === 'ios' && data.iosPkgUrl) {
- plus.runtime.openURL(data.iosPkgUrl)
- return
- }
-
- console.log('install data', data)
- uni.downloadFile({
- url: data.downloadUrl,
- success: function(downloadResult) {
- if (downloadResult.statusCode === 200) {
- plus.runtime.install(
- downloadResult.tempFilePath, {
- force: data.force
- },
- function() {
- console.log('install success...')
- if (data.restart) {
- plus.runtime.restart();
- }
- },
- function(e) {
- console.error('install fail...')
- plus.runtime.quit()
- }
- )
- }
- },
- error: function(e) {
- console.log('install error', e)
- uni.showToast({
- position: 'top',
- title: '更新失败',
- icon: 'none',
- duration: 3000
- })
- }
- })
- }
- }
|