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
const result = JSON.parse<UniUpgradeCenterResult>(JSON.stringify(res.result)) as UniUpgradeCenterResult
7.1、UniUpgradeCenterResult这个类型的定义为:
export type UniUpgradeCenterResult = {_id : stringappid : stringname : stringtitle : stringcontents : stringurl : string // 安装包下载地址platform : Array<string> // Array<'Android' | 'iOS'>version : string // 版本号 1.0.0uni_platform : string // "android" | "ios" // 版本号 1.0.0stable_publish : boolean // 是否是稳定版is_mandatory : boolean // 是否强制更新is_silently : boolean | null // 是否静默更新create_env : string // "upgrade-center"create_date : numbermessage : stringcode : numbertype : string // "native_app" | "wgt"store_list : StoreListItem[] | nullmin_uni_version : string | null // 升级 wgt 的最低 uni-app 版本
}
7.2、管理后台返回的数据缺少了create_env和uni_platform这两个字段。
7.3、既然问题找到了,那么只要将类型匹配上了就可以了呗。
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 UniUpgradeCenterResultresolve(result)
7.4、call-check-version.ts修改后的整体代码如下。
export type StoreListItem = {enable : booleanid : stringname : stringscheme : stringpriority : number // 优先级
}export type UniUpgradeCenterResult = {_id : stringappid : stringname : stringtitle : stringcontents : stringurl : string // 安装包下载地址platform : Array<string> // Array<'Android' | 'iOS'>version : string // 版本号 1.0.0uni_platform : string // "android" | "ios" // 版本号 1.0.0stable_publish : boolean // 是否是稳定版is_mandatory : boolean // 是否强制更新is_silently : boolean | null // 是否静默更新create_env : string // "upgrade-center"create_date : numbermessage : stringcode : numbertype : string // "native_app" | "wgt"store_list : StoreListItem[] | nullmin_uni_version : string | null // 升级 wgt 的最低 uni-app 版本
}export default function () : Promise<UniUpgradeCenterResult> {// #ifdef APPreturn new Promise<UniUpgradeCenterResult>((resolve, reject) => {const systemInfo = uni.getSystemInfoSync()const appId = systemInfo.appIdconst appVersion = systemInfo.appVersion //systemInfo.appVersion// #ifndef UNI-APP-Xif (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-Xif (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 UniUpgradeCenterResultresolve(result)}}}).catch<void>((err : any | null) => {const error = err as UniCloudErrorif (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 APPreturn new Promise((resolve, reject) => {reject({message: '请在App中使用'})})// #endif
}
8、自己反思了一下问什么会被这样的问题困住2天左右的时间,我想可能主要是源于过于依赖文档,自己主动思考去解决问题的决心不够坚决。墨迹一下,过度自信和不够自信也许都是解决问题道路上的绊脚石。