uniapp开发中遇到的plus.runtime.appid问题

本文详细介绍了uni-app的整包升级流程,包括如何通过plus.runtime.appid和plus.runtime.version向服务器请求更新,并展示了一个PHP示例。在App.vue的onLaunch中检测更新,当需要升级时,通过uni.showModal提示用户并调用plus.runtime.openURL进行更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天接手一个别人的项目,看代码看了一天,有个地方我很费解,上图
plus.runtime.appid 我寻思这是个啥?
在这里插入图片描述
打印输出的结果 如下
在这里插入图片描述
我把plus给打印了 结果如下,看不懂 很费解
在这里插入图片描述
文字版如下 这是给人看的东西吗??

[native code] }","11":"function() { [native code] }","12":"function() { [native
code] }","13":"function() { [native code] }","14":"function() { [native code]
}","15":"function() { [native code] }","16":"function() { [native code]
}","17":"function() { [native code] }","18":"function() { [native code]
}","19":"function() { [native code]
}"},"hooks":{}}},"documentElement":{"ref":"_documentElement","type":"document","attr":{},"style":{}},"__$automator__":false,"__$compiler__":"uni-app"},"requireModule":"function()
{ [native code] }","importScript":"function() { [native code]
}","isRegisteredModule":"function() { [native code]
}","isRegisteredComponent":"function() { [native code]
}"},"weexBridge":{"preloadReady":"function() { [native code]
}","postMessage":"function() { [native code] }","execSync":"function() { [native
code] }","getConfigInfo":"function() { [native code]
}","evalJSFiles":"function() { [native code] }","uniReady":"function() { [native
code] }","removeAllEventListeners":"function() { [native code]
}","sendNativeEvent":"function() { [native code] }","pushDebugData":"function()
{ [native code] }","exec":"function() { [native code] }","getValue":"function()
{ [native code] }","getDotData":"function() { [native code]
}","addEventListener":"function() { [native code]
}","getRedirectInfo":"function() { [native code] }","log":"function() { [native
code] }","setDefaultFontSize":"function() { [native code]
}"},"globalEvent":{"addEventListener":"function() { [native code]
}","removeAllEventListeners":"function() { [native code]
}","removeEventListener":"function() { [native code]
}"},"tools":{"__UUID__":3,"UNKNOWN":-1,"IOS":0,"ANDROID":1,"platform":1,"debug":true,"UUID":"function()
{ [native code] }","extend":"function() { [native code]
}","typeName":"function() { [native code] }","isDate":"function() { [native
code] }","isArray":"function() { [native code] }","isDebug":"function() {
[native code] }","stringify":"function() { [native code]
}","isNumber":"function() { [native code]
}","getElementOffsetInWebview"

遂开始面向百度编程 这个东西是跟app的升级更新有关 其实我现在还是没有整太明白,但是有时候不怕百度就怕不知道咋百度,所以把我遇到的问题分享给大家,希望对你有帮助

粘贴原文如下————

uni-app 整包升级/更新方案

w微凉半秋

2019.10.31 21:40:00

注意:plus.runtime.appid,plus.runtime.version, plus.runtime.openURL()
在真机环境下才有效

使用 uni-app 开发,可将代码编译到iOS、Android、微信小程序等多个平台,升级时也需考虑多平台同步升级。

uni-app发布为小程序的升级模式较简单,只需将开发完的代码提交小程序后台,待审核通过后用户将自动升级 iOS/Android App
的升级需开发者自己处理,本文主要简介 App 的整包升级模式。 App
的资源热更新另见文档:http://ask.dcloud.net.cn/article/35667 接口约定
如下数据接口约定仅为示例,开发者可以自定义接口参数。 请求地址:https://www.example.com/update

请求方法:GET

请求数据:

    "appid": plus.runtime.appid,  
    "version": plus.runtime.version   }   ```

响应数据:

```javascript {  
    "status":1,//升级标志,1:需要升级;0:无需升级  
    "note": "修复bug1;\n修复bug2;",//release notes  
    "url": "http://www.example.com/uniapp.apk" //更新包下载地址   }  ```

客户端实现 App启动时,向服务端上报当前版本号,服务端判断是否提示升级。

在App.vue的onLaunch中,发起升级检测请求,如下:

```javascript onLaunch: function () {  
    //#ifdef APP-PLUS  
    var server = "https://www.example.com/update"; //检查更新地址  
    var req = { //升级检测数据  
        "appid": plus.runtime.appid,  
        "version": plus.runtime.version  
    };  
    uni.request({  
        url: server,  
        data: req,  
        success: (res) => {  
            if (res.statusCode == 200 && res.data.status === 1) {  
                uni.showModal({ //提醒用户更新  
                    title: "更新提示",  
                    content: res.data.note,  
                    success: (res) => {  
                        if (res.confirm) {  
                            plus.runtime.openURL(res.data.url);  
                        }  
                    }  
                })  
            }  
        }  
    })  
    //#endif   }   ```

注意:App的升级检测代码必须使用条件编译,否则在微信环境由于不存在plus相关API,将会报错。

服务端实现 根据客户端上报的版本号,比对服务端最新版本号,决定是否需要升级,若需升级则返回升级信息(rease notes、更新包地址等)

开发者可以根据服务端开发语言,自己实现升级检测逻辑,如下是一个php示例代码:

```javascript header("Content-type:text/json");   $appid =
$_GET["appid"];   $version = $_GET["version"]; //客户端版本号   $rsp =
array("status" => 0); //默认返回值,不需要升级   if (isset($appid) &&
isset($version)) {  
    if ($appid === "__UNI__123456") { //校验appid  
        if ($version !== "1.0.1") { //这里是示例代码,真实业务上,最新版本号及relase notes可以存储在数据库或文件中  
            $rsp["status"] = 1;  
            $rsp["note"] = "修复bug1;\n修复bug2;"; //release notes  
            $rsp["url"] = "http://www.example.com/uniapp.apk"; //应用升级包下载地址  
        }  
    }   }    echo json_encode($rsp);   exit;   ```

常见问题 版本检测需要打包app,真机运行基座无法测试。因为真机运行的plus.runtime.version是固定值。

原文地址:https://ask.dcloud.net.cn/article/34972


顺便把评论也粘贴过来

Han涛_ 205.06 17:43 我这样给后端传过去,后端拿到的数据一直是null

w微凉半秋
05.06 18:51 你取的前端有取到数据吗

Han涛_
05.07 13:43 @w晚风 发现问题了,后端设置的问题,谢谢
UniApp 中实现 App 的热更新功能,通常依赖于 H5 的特性以及原生 App 的热更新机制。热更新允许在不重新发布整个应用的情况下,更新部分资源文件,从而快速修复 bug 或更新界面内容。以下是实现热更新的几种方法: ### 1. 使用 H5 热更新机制 对于基于 H5UniApp 应用,可以通过重新加载更新的资源文件来实现热更新。 - **步骤**: - 将需要更新的页面或组件打包成 `.wgt` 文件(即资源包)。 - 将该资源包上传到服务器,并提供一个更新接口。 - 在 App 中请求该接口,获取最新的版本信息。 - 如果发现新版本,则下载 `.wgt` 文件并进行替换。 - 最后重新加载页面即可生效。 示例代码片段: ```javascript // 检查热更新 checkHotUpdate() { uni.request({ url: 'https://yourdomain.com/check-update', // 请求更新接口 success: (res) => { if (res.data.hasUpdate) { uni.downloadFile({ url: res.data.updateUrl, // 下载更新包 success: (downloadRes) => { if (downloadRes.statusCode === 200) { uni.saveFile({ tempFilePath: downloadRes.tempFilePath, success: (saveRes) => { uni.loadPages({ pages: saveRes.savedFilePath // 加载更新后的页面 }); } }); } } }); } } }); } ``` ### 2. 使用原生 App 的热更新机制 对于原生 App,可以通过 UniApp 提供的 `plus` API 实现热更新。 - **步骤**: - 通过 `plus.runtime.getProperty` 获取当前应用的版本信息。 - 向服务器发起请求,检查是否有新的 `.wgt` 文件。 - 如果存在更新,则下载 `.wgt` 文件。 - 使用 `plus.runtime.install` 方法安装更新包。 - 最后提示用户重启应用以应用更新。 示例代码片段: ```javascript // 检查并安装热更新 checkNativeHotUpdate() { plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => { uni.request({ url: 'https://yourdomain.com/check-native-update', success: (res) => { if (res.data.version > widgetInfo.version) { uni.downloadFile({ url: res.data.updateUrl, success: (downloadRes) => { if (downloadRes.statusCode === 200) { plus.runtime.install(downloadRes.tempFilePath, { force: false }, () => { uni.showToast({ title: '更新成功,请重启应用' }); }, (e) => { uni.showToast({ title: '安装失败', icon: 'none' }); }); } } }); } } }); }); } ``` ### 3. 整包更新与热更新的对比 - **整包更新**:需要用户重新下载整个 `.apk` 文件(适用于 Android),更新内容较大,但可以包含完整的资源和代码更新。 - **热更新**:仅更新部分资源文件(如 `.wgt` 文件),更新速度快,适合小范围的修复和优化。 根据需求选择合适的更新方式,例如,对于需要大规模改动的更新,建议使用整包更新;而对于小范围的 bug 修复或界面优化,推荐使用热更新 [^1]。 ### 4. 更新内容的版本管理 为了确保更新的顺利进行,应在服务器端维护版本号和更新内容。客户端在请求更新时,可以比较当前版本号与服务器上的版本号,判断是否需要更新。 - **版本号**:通常使用 `versionCode` 来标识应用的版本。 - **更新内容**:可以通过接口返回更新说明,让用户了解更新的细节。 示例接口返回数据: ```json { "hasUpdate": true, "version": "1.0.1", "updateUrl": "https://yourdomain.com/update.wgt", "updateContent": "修复了已知的 bug,优化了用户体验。" } ``` ### 5. 用户提示与更新流程 在热更新过程中,需要向用户展示清晰的提示信息,例如: - 是否有新的更新。 - 更新的内容说明。 - 下载进度。 - 安装完成后的提示。 通过合理的用户交互设计,可以提升用户的更新体验,并减少更新过程中的困惑。 ### 6. 热更新的限制与注意事项 - **兼容性问题**:热更新仅适用于资源文件的更新,不能更新原生代码。 - **安全性**:确保更新包的来源安全,防止恶意更新。 - **网络依赖**:热更新需要稳定的网络连接,下载更新包时可能会受到网络影响。 通过以上方法,可以在 UniApp 中实现高效的热更新功能,从而提升应用的维护效率和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值