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天左右的时间,我想可能主要是源于过于依赖文档,自己主动思考去解决问题的决心不够坚决。墨迹一下,过度自信和不够自信也许都是解决问题道路上的绊脚石。

相关推荐
用户6990304848753 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_4 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Geek_Vison4 天前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
CHB4 天前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos
2501_915918415 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
斯内普吖5 天前
(开源)高校素拓分管理系统小程序实战指南 基于 Java + SpringBoot + uni-app + Vue + MySQL
java·spring boot·mysql·小程序·uni-app·开源
海阔天空66885 天前
uniapp开启调试模式
uni-app·uniapp开启调试模式
anyup6 天前
分享 5 套 uni-app 实用主题,一键适配暗黑模式
前端·uni-app·视觉设计
gg159357284606 天前
Uni-app跨平台开发全解课程:从零基础到企业级多端落地实战
vue.js·uni-app
xshirleyl7 天前
uniapp小兔鲜儿day3
uni-app