# HarmonyOS 授权管理(License SDK) **模块**:`LicenseSDK` · **包名**:`@xuqm/harmony-sdk` --- ## 1. 安装 `LicenseSDK` 已包含在 `@xuqm/harmony-sdk` 中,无需额外安装。确保 `oh-package.json5` 中已依赖: ```json5 { "dependencies": { "@xuqm/harmony-sdk": "^0.1.0" } } ``` 执行: ```bash ohpm install ``` --- ## 2. 放置 License 文件 从租户平台下载 `.xuqmlicense` 加密文件,放入项目 `resources/rawfile/` 目录: ``` entry/ src/main/ resources/ rawfile/ license.xuqm ``` --- ## 3. 初始化并检查授权 在 `EntryAbility.onCreate` 或应用启动入口中初始化 License: ```typescript import { LicenseSDK } from '@xuqm/harmony-sdk' import common from '@ohos.app.ability.common' import rawfileManager from '@ohos.rawfileManager' export default class EntryAbility extends UIAbility { async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { // 读取 License 文件内容 const context = getContext(this) as common.UIAbilityContext const content = await readRawFile(context, 'license.xuqm') // 从加密文件初始化 await LicenseSDK.initializeFromFile(content) // 检查授权 const result = await LicenseSDK.checkLicense() if (result.type === 'success') { console.log('授权通过:', result.reason) } else { console.warn('授权失败:', result.message) } } } async function readRawFile(context: common.UIAbilityContext, filename: string): Promise { const rm = context.resourceManager const data = await rm.getRawFileContent(filename) return new TextDecoder().decode(data) } ``` --- ## 4. 携带用户信息 ```typescript import { LicenseSDK, LicenseUserInfo } from '@xuqm/harmony-sdk' const userInfo: LicenseUserInfo = { userId: 'user_001', name: '张三', email: 'zhangsan@company.com', } const result = await LicenseSDK.checkLicense(userInfo) ``` --- ## 5. API 说明 ### initializeFromFile ```typescript LicenseSDK.initializeFromFile(encryptedContent: string): Promise ``` 从加密 License 文件内容自动解析 `appKey` 并初始化。 ### checkLicense ```typescript LicenseSDK.checkLicense(userInfo?: LicenseUserInfo): Promise ``` 返回 `{ type: 'success', reason: string }` 或 `{ type: 'error', message: string }`。 **缓存策略**:10 分钟有效期,有效期内不发起网络请求。 ### getStatus ```typescript LicenseSDK.getStatus(): Promise<'ok' | 'denied' | 'unknown'> ``` ### getDeviceId ```typescript LicenseSDK.getDeviceId(): Promise ``` ### clear ```typescript LicenseSDK.clear(): Promise ``` --- ## 6. 数据存储 | 数据 | 存储方式 | |------|---------| | deviceId | `preferences`(应用持久化存储)| | token | `preferences` | | 授权状态 | `preferences` | | statusTime | `preferences` | --- ## 7. 离线模式 - 首次激活需要网络连接 - 激活后 10 分钟缓存内可离线使用 - 网络异常时,若历史缓存成功,继续返回授权通过 --- ## 8. 权限配置 使用 License SDK 需要网络权限,确保 `module.json5` 中已声明: ```json5 { "module": { "requestPermissions": [ { "name": "ohos.permission.INTERNET" }, { "name": "ohos.permission.GET_NETWORK_INFO" } ] } } ```