check-update.js 3.6 KB
Newer Older
LukeLiou's avatar
LukeLiou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
// 推荐再App.vue中使用
const PACKAGE_INFO_KEY = '__package_info__'

export default function() {
	// #ifdef APP-PLUS
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
		uniCloud.callFunction({
			name: 'check-version',
			data: {
				appid: plus.runtime.appid,
				appVersion: plus.runtime.version,
				wgtVersion: widgetInfo.version
			},
			success: async (e) => {
				if (!e.result) return;
				const {
					code,
					message,
					is_silently, // 是否静默更新
					url, // 安装包下载地址
					platform, // 安装包平台
					type // 安装包类型
				} = e.result;

				// 此处逻辑仅为实例,可自行编写
				if (code > 0) {
					// 腾讯云和阿里云下载链接不同,需要处理一下,阿里云会原样返回
					const {
						fileList
					} = await uniCloud.getTempFileURL({
						fileList: [url]
					});
					e.result.url = fileList[0].tempFileURL

					// 静默更新,只有wgt有
					if (is_silently) {
						uni.downloadFile({
							url: e.result.url,
							success: res => {
								if (res.statusCode == 200) {
									// 下载好直接安装,下次启动生效
									plus.runtime.install(res.tempFilePath, {
										force: false
									});
								}
							}
						});
						return;
					}

					/**
					 * 提示升级一
					 * 使用 uni.showModal
					 */
					// return updateUseModal(e.result)

					/**
					 * 提示升级二
					 * 官方适配的升级弹窗,可自行替换资源适配UI风格
					 */
					uni.setStorageSync(PACKAGE_INFO_KEY, e.result)
					uni.navigateTo({
						url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
						fail: (err) => {
							console.error('更新弹框跳转失败', err)
							uni.removeStorageSync(PACKAGE_INFO_KEY)
						}
					})
				} else if (code < 0) {
					// TODO 云函数报错处理
					console.error(message)
				}
			},
			fail: (err) => {
				// TODO 云函数报错处理
				console.error(err.message)
			}
		})
	});
	// #endif
}

/**
 * 使用 uni.showModal 升级
 */
function updateUseModal(packageInfo) {
	const {
		title, // 标题
		contents, // 升级内容
		is_mandatory, // 是否强制更新
		url, // 安装包下载地址
		platform, // 安装包平台
		type // 安装包类型
	} = packageInfo;

	let isWGT = type === 'wgt'
	let isiOS = !isWGT ? platform.includes('iOS') : false;
	let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'

	return uni.showModal({
		title,
		content: contents,
		showCancel: !is_mandatory,
		confirmText,
		success: res => {
			if (res.cancel) return;

			// 安装包下载
			if (isiOS) {
				plus.runtime.openURL(url);
				return;
			}

			uni.showToast({
				title: '后台下载中……',
				duration: 1000
			});

			// wgt 和 安卓下载更新
			downloadTask = uni.downloadFile({
				url,
				success: res => {
					if (res.statusCode !== 200) {
						console.error('下载安装包失败', err);
						return;
					}
					// 下载好直接安装,下次启动生效
					plus.runtime.install(res.tempFilePath, {
						force: false
					}, () => {
						if (is_mandatory) {
							//更新完重启app
							plus.runtime.restart();
							return;
						}
						uni.showModal({
							title: '安装成功是否重启?',
							success: res => {
								if (res.confirm) {
									//更新完重启app
									plus.runtime.restart();
								}
							}
						});
					}, err => {
						uni.showModal({
							title: '更新失败',
							content: err
								.message,
							showCancel: false
						});
					});
				}
			});
		}
	});
}