XuqmGroup-Web/docs-site/docs/harmony/license.md
XuqmGroup e912a5e65e docs: remove WebSocket doc, strip Update from server SDKs, add Harmony license
- Remove docs/server/websocket.md and sidebar entry
- Server API: remove Update 服务 section (only IM + Push)
- Go/Python/Java SDK docs: remove Update from intro and capability tables
- RN license: remove manual initialize(baseUrl) section
- Flutter license: remove manual initialize(baseUrl) section
- Flutter/Harmony: fix git URLs to xuqmGroup org
- Harmony: add LicenseSDK to modules table and create harmony/license.md

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-16 11:56:40 +08:00

3.3 KiB

HarmonyOS 授权管理License SDK

模块LicenseSDK · 包名@xuqm/harmony-sdk


1. 安装

LicenseSDK 已包含在 @xuqm/harmony-sdk 中,无需额外安装。确保 oh-package.json5 中已依赖:

{
  "dependencies": {
    "@xuqm/harmony-sdk": "^0.1.0"
  }
}

执行:

ohpm install

2. 放置 License 文件

从租户平台下载 .xuqmlicense 加密文件,放入项目 resources/rawfile/ 目录:

entry/
  src/main/
    resources/
      rawfile/
        license.xuqm

3. 初始化并检查授权

EntryAbility.onCreate 或应用启动入口中初始化 License

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<string> {
  const rm = context.resourceManager
  const data = await rm.getRawFileContent(filename)
  return new TextDecoder().decode(data)
}

4. 携带用户信息

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

LicenseSDK.initializeFromFile(encryptedContent: string): Promise<void>

从加密 License 文件内容自动解析 appKey 并初始化。

checkLicense

LicenseSDK.checkLicense(userInfo?: LicenseUserInfo): Promise<LicenseResult>

返回 { type: 'success', reason: string }{ type: 'error', message: string }

缓存策略10 分钟有效期,有效期内不发起网络请求。

getStatus

LicenseSDK.getStatus(): Promise<'ok' | 'denied' | 'unknown'>

getDeviceId

LicenseSDK.getDeviceId(): Promise<string>

clear

LicenseSDK.clear(): Promise<void>

6. 数据存储

数据 存储方式
deviceId preferences(应用持久化存储)
token preferences
授权状态 preferences
statusTime preferences

7. 离线模式

  • 首次激活需要网络连接
  • 激活后 10 分钟缓存内可离线使用
  • 网络异常时,若历史缓存成功,继续返回授权通过

8. 权限配置

使用 License SDK 需要网络权限,确保 module.json5 中已声明:

{
  "module": {
    "requestPermissions": [
      { "name": "ohos.permission.INTERNET" },
      { "name": "ohos.permission.GET_NETWORK_INFO" }
    ]
  }
}