call-check-version.ts 3.4 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
export type StoreListItem = {
	enable : boolean
	id : string
	name : string
	scheme : string
	priority : number // 优先级
}

export type UniUpgradeCenterResult = {
	_id : string
	appid : string
	name : string
	title : string
	contents : string
	url : string // 安装包下载地址
	platform : Array<string> // Array<'Android' | 'iOS'>
	version : string // 版本号 1.0.0
	uni_platform : string // "android" | "ios" // 版本号 1.0.0
	stable_publish : boolean // 是否是稳定版
	is_mandatory : boolean // 是否强制更新
D
DCloud_LXH 已提交
21
	is_silently : boolean | null	// 是否静默更新
D
DCloud_LXH 已提交
22 23 24 25 26 27 28 29 30 31
	create_env : string // "upgrade-center"
	create_date : number
	message : string
	code : number

	type : string // "native_app" | "wgt"
	store_list : StoreListItem[] | null
	min_uni_version : string | null  // 升级 wgt 的最低 uni-app 版本
}

D
DCloud_LXH 已提交
32
export default function () : Promise<UniUpgradeCenterResult> {
W
wanganxp 已提交
33
	// #ifdef APP
D
DCloud_LXH 已提交
34 35
	return new Promise<UniUpgradeCenterResult>((resolve, reject) => {
		const systemInfo = uni.getSystemInfoSync()
D
DCloud_LXH 已提交
36 37
		const appId = '__UNI__3584C99'
		const appVersion = '1.0.17' //systemInfo.appVersion
D
DCloud_LXH 已提交
38
		// #ifndef UNI-APP-X
D
DCloud_LXH 已提交
39 40
		if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
			plus.runtime.getProperty(appId, function (widgetInfo) {
D
DCloud_LXH 已提交
41 42 43
				if (widgetInfo.version) {
					let data = {
						action: 'checkVersion',
D
DCloud_LXH 已提交
44 45
						appid: appId,
						appVersion: appVersion,
D
DCloud_LXH 已提交
46 47 48 49
						wgtVersion: widgetInfo.version
					}
					uniCloud.callFunction({
						name: 'uni-upgrade-center',
D
DCloud_LXH 已提交
50
						data,
D
DCloud_LXH 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
						success: (e) => {
							resolve(e.result as UniUpgradeCenterResult)
						},
						fail: (error) => {
							reject(error)
						}
					})
				} else {
					reject('widgetInfo.version is EMPTY')
				}
			})
		} else {
			reject('plus.runtime.appid is EMPTY')
		}
		// #endif
D
DCloud_LXH 已提交
66
		// #ifdef UNI-APP-X
D
DCloud_LXH 已提交
67 68 69 70 71
		if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
			let data = {
				action: 'checkVersion',
				appid: appId,
				appVersion: appVersion,
D
DCloud_LXH 已提交
72
				is_uniapp_x: true,
D
DCloud_LXH 已提交
73 74 75 76 77 78 79
				wgtVersion: '0.0.0.0.0.1'
			}
			try {
				uniCloud.callFunction({
					name: 'uni-upgrade-center',
					data: data
				}).then(res => {
D
DCloud_LXH 已提交
80
          console.log('res: ',res);
D
DCloud_LXH 已提交
81
					const code = res.result['code']
D
DCloud_LXH 已提交
82 83 84 85 86 87 88
					const codeIsNumber = ['Int', 'Long', 'number'].includes(typeof code)
					if (codeIsNumber) {
					  if ((code as number) == 0) {
					    reject({
					      code: res.result['code'],
					      message: res.result['message']
					    })
D
DCloud_LXH 已提交
89
					  } else if ((code as number) < 0) {
D
DCloud_LXH 已提交
90 91 92 93
					    reject({
					      code: res.result['code'],
					      message: res.result['message']
					    })
D
DCloud_LXH 已提交
94 95 96 97
					  } else {
              const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult
              resolve(result)
            }
D
DCloud_LXH 已提交
98 99
					}
				}).catch<void>((err : any | null) => {
D
DCloud_LXH 已提交
100
          console.log('err: ',err);
D
DCloud_LXH 已提交
101
					const error = err as UniCloudError
D
DCloud_LXH 已提交
102 103
					if (error.errMsg == '未匹配到云函数[uni-upgrade-center]')
						error.errMsg = '【uni-upgrade-center-app】未配置uni-upgrade-center,无法升级。参考: https://uniapp.dcloud.net.cn/uniCloud/upgrade-center.html'
D
DCloud_LXH 已提交
104 105 106 107 108 109
					reject(error.errMsg)
				})
			} catch (e) {
				reject(e.message)
			}
		} else {
W
wanganxp 已提交
110
			reject('invalid appid or appVersion')
D
DCloud_LXH 已提交
111 112 113 114 115
		}
		// #endif
	})
	// #endif
	// #ifndef APP-PLUS
D
DCloud_LXH 已提交
116
	return new Promise((resolve, reject) => {
D
DCloud_LXH 已提交
117 118 119
		reject({
			message: '请在App中使用'
		})
D
DCloud_LXH 已提交
120
	})
D
DCloud_LXH 已提交
121 122
	// #endif
}