feat: 简化登录模型,移除 nickname/avatar/expiresAt
这个提交包含在:
父节点
3d1873b6a3
当前提交
7e8fc55e41
11
README.md
11
README.md
@ -82,17 +82,18 @@ export default class EntryAbility extends UIAbility {
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 设置 Token
|
||||
### 3. 登录
|
||||
|
||||
```typescript
|
||||
// 登录后
|
||||
await XuqmSDK.setToken('eyJ...')
|
||||
// 登录
|
||||
const session = await XuqmSDK.login('user_001', 'eyJ...')
|
||||
console.log('登录成功', session.userId, session.userSig)
|
||||
|
||||
// 登出
|
||||
await XuqmSDK.setToken(null)
|
||||
await XuqmSDK.logout()
|
||||
```
|
||||
|
||||
Token 通过 `@ohos.data.preferences` 持久化,App 重启后自动恢复。
|
||||
登录会话通过 `@ohos.data.preferences` 持久化,App 重启后自动恢复。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@ -6,6 +6,7 @@ export { SDKContext } from './src/main/ets/core/SDKContext'
|
||||
export { HttpClient } from './src/main/ets/core/HttpClient'
|
||||
export type {
|
||||
SDKConfig,
|
||||
LoginSession,
|
||||
ImMessage,
|
||||
SendMessageParams,
|
||||
AppVersionInfo,
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import common from '@ohos.app.ability.common'
|
||||
import type { SDKConfig } from './core/Types'
|
||||
import type { LoginSession, SDKConfig } from './core/Types'
|
||||
import { SDKContext } from './core/SDKContext'
|
||||
import { ImClient } from './im/ImClient'
|
||||
import { UpdateSDK } from './update/UpdateSDK'
|
||||
@ -12,20 +12,17 @@ export class XuqmSDK {
|
||||
await SDKContext.initPreferences(context)
|
||||
}
|
||||
|
||||
static async setToken(token: string | null): Promise<void> {
|
||||
await SDKContext.setToken(token)
|
||||
static async login(userId: string, userSig: string): Promise<LoginSession> {
|
||||
await SDKContext.login(userId, userSig)
|
||||
return SDKContext.getLoginSession()!
|
||||
}
|
||||
|
||||
static getToken(): string | null {
|
||||
return SDKContext.getToken()
|
||||
static async logout(): Promise<void> {
|
||||
await SDKContext.logout()
|
||||
}
|
||||
|
||||
static setUserId(userId: string | null): void {
|
||||
SDKContext.setUserId(userId)
|
||||
}
|
||||
|
||||
static getUserId(): string | null {
|
||||
return SDKContext.getUserId()
|
||||
static get currentLoginSession(): LoginSession | null {
|
||||
return SDKContext.getLoginSession()
|
||||
}
|
||||
|
||||
static get im(): ImClient {
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import preferences from '@ohos.data.preferences'
|
||||
import type { InstalledRnBundleInfo, SDKConfig } from './Types'
|
||||
import type { InstalledRnBundleInfo, LoginSession, SDKConfig } from './Types'
|
||||
|
||||
const TOKEN_KEY = 'xuqm_token'
|
||||
const USER_ID_KEY = 'xuqm_user_id'
|
||||
const RN_BUNDLE_PREFIX = 'xuqm_rn_bundle_'
|
||||
const PREF_NAME = 'xuqm_sdk_prefs'
|
||||
|
||||
@ -27,18 +28,28 @@ export class SDKContext {
|
||||
|
||||
static async initPreferences(context: Context): Promise<void> {
|
||||
SDKContext._pref = await preferences.getPreferences(context, PREF_NAME)
|
||||
const saved = await SDKContext._pref.get(TOKEN_KEY, '') as string
|
||||
if (saved) SDKContext._token = saved
|
||||
const savedToken = await SDKContext._pref.get(TOKEN_KEY, '') as string
|
||||
if (savedToken) SDKContext._token = savedToken
|
||||
const savedUserId = await SDKContext._pref.get(USER_ID_KEY, '') as string
|
||||
if (savedUserId) SDKContext._userId = savedUserId
|
||||
}
|
||||
|
||||
static async setToken(token: string | null): Promise<void> {
|
||||
SDKContext._token = token
|
||||
static async login(userId: string, userSig: string): Promise<void> {
|
||||
SDKContext._token = userSig
|
||||
SDKContext._userId = userId
|
||||
if (SDKContext._pref) {
|
||||
if (token) {
|
||||
await SDKContext._pref.put(TOKEN_KEY, token)
|
||||
} else {
|
||||
await SDKContext._pref.delete(TOKEN_KEY)
|
||||
}
|
||||
await SDKContext._pref.put(TOKEN_KEY, userSig)
|
||||
await SDKContext._pref.put(USER_ID_KEY, userId)
|
||||
await SDKContext._pref.flush()
|
||||
}
|
||||
}
|
||||
|
||||
static async logout(): Promise<void> {
|
||||
SDKContext._token = null
|
||||
SDKContext._userId = null
|
||||
if (SDKContext._pref) {
|
||||
await SDKContext._pref.delete(TOKEN_KEY)
|
||||
await SDKContext._pref.delete(USER_ID_KEY)
|
||||
await SDKContext._pref.flush()
|
||||
}
|
||||
}
|
||||
@ -47,14 +58,21 @@ export class SDKContext {
|
||||
return SDKContext._token
|
||||
}
|
||||
|
||||
static setUserId(userId: string | null): void {
|
||||
SDKContext._userId = userId
|
||||
}
|
||||
|
||||
static getUserId(): string | null {
|
||||
return SDKContext._userId
|
||||
}
|
||||
|
||||
static getLoginSession(): LoginSession | null {
|
||||
if (!SDKContext._token || !SDKContext._userId || !SDKContext._config) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
appKey: SDKContext._config.appKey,
|
||||
userId: SDKContext._userId,
|
||||
userSig: SDKContext._token,
|
||||
}
|
||||
}
|
||||
|
||||
private static rnBundleKey(bundleName: string): string {
|
||||
return RN_BUNDLE_PREFIX + bundleName
|
||||
}
|
||||
|
||||
@ -166,6 +166,12 @@ export interface InstalledRnBundleInfo {
|
||||
installedAt: number
|
||||
}
|
||||
|
||||
export interface LoginSession {
|
||||
appKey: string
|
||||
userId: string
|
||||
userSig: string
|
||||
}
|
||||
|
||||
export interface PushTokenInfo {
|
||||
vendor: string
|
||||
token: string
|
||||
|
||||
正在加载...
在新工单中引用
屏蔽一个用户