uni-app X APP在线升级 解决【uni-upgrade-center-app】未配置uni-upgrade-center 问题

着急解决问题的同学可以直接查看第7项和7.3项。

1、按照官网的指导文档进行升级中心配置。App升级中心 uni-upgrade-center | uniCloud

2、升级中心分为两个部分:uni-upgrade-center Admin管理后台uni-upgrade-center-app前台检测更新

3、后台管理部分按照上面的连接操作即可。

4、当执行到前台检测更新时遇到了这个问题"Possible Unhandled Promise Rejection: 【uni-upgrade-center-app】未配置uni-upgrade-center,无法升级。参考: https://uniapp.dcloud.net.cn/uniCloud/upgrade-center.html"

5、当我使用的插件是官网提供的,配置安装官网的配置做的,但是依然报错的时候,我开始怀疑的是我的操作有问题,一顿操作猛如虎......一看伤害负的0.5,完全没有作用。这时AI也没有用了......

6、有时候过度的自信和过度的相信别人都是一种错误。在外部没有解决方法时开始踏踏实实的自己寻找解决方案。通过逐步console.log打印日志最终确定了问题。

7、问题是下面这句话引起的。路径为\uni_modules\uni-upgrade-center-app\utils\call-check-version.ts

TypeScript 复制代码
const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult

7.1、UniUpgradeCenterResult这个类型的定义为:

TypeScript 复制代码
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 // 是否强制更新
	is_silently : boolean | null	// 是否静默更新
	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 版本
}

7.2、管理后台返回的数据缺少了create_env和uni_platform这两个字段。

7.3、既然问题找到了,那么只要将类型匹配上了就可以了呗。

TypeScript 复制代码
							res.result.create_env = "upgrade-center";//补上create_env  字段
							res.result.uni_platform = "Android" //补上uni_platform 字段
							const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult
							resolve(result)

7.4、call-check-version.ts修改后的整体代码如下。

TypeScript 复制代码
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 // 是否强制更新
	is_silently : boolean | null	// 是否静默更新
	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 版本
}

export default function () : Promise<UniUpgradeCenterResult> {
	// #ifdef APP
	return new Promise<UniUpgradeCenterResult>((resolve, reject) => {
		const systemInfo = uni.getSystemInfoSync()
		const appId = systemInfo.appId
		const appVersion = systemInfo.appVersion //systemInfo.appVersion
		// #ifndef UNI-APP-X
		if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
			plus.runtime.getProperty(appId, function (widgetInfo) {
				if (widgetInfo.version) {
					let data = {
						action: 'checkVersion',
						appid: appId,
						appVersion: appVersion,
						wgtVersion: widgetInfo.version
					}
					uniCloud.callFunction({
						name: 'uni-upgrade-center',
						data,
						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
		// #ifdef UNI-APP-X
		if (typeof appId === 'string' && typeof appVersion === 'string' && appId.length > 0 && appVersion.length > 0) {
			let data = {
				action: 'checkVersion',
				appid: appId,
				appVersion: appVersion,
				is_uniapp_x: true,
				wgtVersion: '0.0.0.0.0.1'
			}
			try {

				uniCloud.callFunction({
					name: 'uni-upgrade-center',
					data: data
				}).then(res => {
					const code = res.result['code']
					const codeIsNumber = ['Int', 'Long', 'number'].includes(typeof code)
					if (codeIsNumber) {
						if ((code as number) == 0) {
							reject({
								code: res.result['code'],
								message: res.result['message']
							})
						} else if ((code as number) < 0) {
							reject({
								code: res.result['code'],
								message: res.result['message']
							})
						} else {
							res.result.create_env = "upgrade-center";
							res.result.uni_platform = "Android"
							// console.log("res.result:"+JSON.stringify(res.result))
							const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult
							resolve(result)
						}
					}
				}).catch<void>((err : any | null) => {
					const error = err as UniCloudError
					if (error.errMsg == '未匹配到云函数[uni-upgrade-center]')
						error.errMsg = '【uni-upgrade-center-app】未配置uni-upgrade-center,无法升级。参考: https://uniapp.dcloud.net.cn/uniCloud/upgrade-center.html'
					reject(error.errMsg)
				})
			} catch (e) {
				reject(e.message)
			}
		} else {
			reject('invalid appid or appVersion')
		}
		// #endif
	})
	// #endif
	// #ifndef APP
	return new Promise((resolve, reject) => {
		reject({
			message: '请在App中使用'
		})
	})
	// #endif
}

8、自己反思了一下问什么会被这样的问题困住2天左右的时间,我想可能主要是源于过于依赖文档,自己主动思考去解决问题的决心不够坚决。墨迹一下,过度自信和不够自信也许都是解决问题道路上的绊脚石。

相关推荐
today喝咖啡了吗4 小时前
uniapp,Anroid10+版本如何保存图片并删除
uni-app
雪碧聊技术10 小时前
uniapp如何创建并使用组件?组件通过Props如何进行数据传递?
uni-app·创建组件·使用组件·props数据传递
@Dream_Chaser10 小时前
uniapp页面间通信
uni-app
@Dream_Chaser15 小时前
uniapp ruoyi-app 中使用checkbox 无法选中问题
前端·javascript·uni-app
鱼是一只鱼啊15 小时前
uniapp移动端地图提示鉴权失败请传入正确的key问题处理
uni-app
毛毛三由15 小时前
基于svga+uniapp的微信小程序动画组件开发指南
微信小程序·uni-app·notepad++
雪碧聊技术17 小时前
uniapp简介
uni-app·hbuilder
一只一只妖17 小时前
uniapp小程序无感刷新token
前端·小程序·uni-app
y东施效颦20 小时前
uni-app uni-push 2.0推送图标不展示问题
uni-app·github