1,582
社区成员




爱智应用开发基础共分为三个模块,依次是应用构建、Web-SDK集成、使用JSRE开发。学习后可完成爱智应用的基础开发工作。
本次讲解模块二:Web-SDK集成
本章节介绍 Web-SDK 的常用功能和集成方法。
Web-SDK 是 EdgerOS 提供的前端开发工具包,为开发者提供一系列与爱智应用交互的接口,支持 JavaScript 和 TypeScript 语言,更多信息请参考 Web-SDK API 文档。
在开发环境中执行以下命令,安装 Web-SDK。
npm install @edgeros/web-sdk
Web-SDK 在 JavaScript 和 TypeScript 中的集成方法如下:
import { edger } from '@edgeros/web-sdk';
<script src="\\PATH/sdk.min.js"></script>
<script>
const edger = window.edger
......
</script>
注意:
若使用 script 标签引入 Web-SDK,edger 对象将会挂载在 window 对象上,调用时需使用 window.edger 参数。
Web-SDK 为开发者提供便捷的开发接口,以下列举部分常用功能:
通过 edger.user()
接口,获取当前登录者的翼辉 ID、昵称和头像信息。
edger.user().then(data => {
var { acoid, nickname, profile } = data;
console.log('User:', acoid, nickname, profile);
}).catch(error => {
console.error(error);
});
2. 添加扫码功能
调用 API 为爱智应用添加扫码功能。
edger.mobile.qrscan().then((data) => {
var { format, text } = data;
}).catch(error => {
// Maybe the user cancelled this scan.
console.error(error);
});
3. 权限功能
通过 edger.permission.fetch()
接口,查询当前用户权限,具体请参考 权限详情。
edger.permission.fetch().then((data) => {
// data contains a complete permission table
if (data.share) {
console.log('We have share permission');
}
if (data.mediacenter.readable) {
console.log('We have mediacenter.readable permission');
}
if (data.devices.includes('xxxx')) {
console.log('We have device xxx permission');
}
}).catch(error => {
console.error(error);
});
以下代码将在应用界面弹出权限申请对话框,要求使用者赋予对应权限。
edger.permission.request({
code: ['network'],
type: 'permissions'
}).then((data) => {
// data.success is 'true' means pops up successfully
}).catch(error => {
console.error(error);
});
监听当前权限,在权限发生变化时触发此事件,以下两种代码方式均可实现:
edger.onAction('permission', (data) => {
// This data is same with edger.permission.fetch()
if (data.share) {
console.log('We have share permission');
}
});
edger.addEventListener('permission', (data) => {
// This data is same with edger.permission.fetch()
if (data.share) {
console.log('We have share permission');
}
});