@cvoc/core 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/esm/config.d.ts +11 -1
- package/dist/esm/http.d.ts +128 -63
- package/dist/esm/index.js +80 -57
- package/dist/esm/index.js.map +1 -1
- package/dist/iife/index.global.js +80 -57
- package/dist/iife/index.global.js.map +1 -1
- package/dist/iife/index.global.min.js +1 -1
- package/dist/iife/index.global.min.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# @cvoc/core
|
|
2
|
+
|
|
3
|
+
Cognvoc SDK 核心模块,提供各业务模块共享的全局配置、HTTP 请求、事件总线、日志等基础设施。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @cvoc/core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 快速开始
|
|
12
|
+
|
|
13
|
+
`Config` 是整个 SDK 的统一配置入口,所有业务模块(如 `@cvoc/phone`、`@cvoc/data`)共享同一份配置实例。**在创建任何模块客户端之前,必须先调用 `Config.init()` 完成初始化。**
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Config, LogLevel } from '@cvoc/core';
|
|
17
|
+
|
|
18
|
+
Config.init({
|
|
19
|
+
apiUrl: 'https://api.example.com/admin-api/',
|
|
20
|
+
accessToken: 'YOUR_ACCESS_TOKEN',
|
|
21
|
+
refreshToken: 'YOUR_REFRESH_TOKEN',
|
|
22
|
+
tenantId: 1,
|
|
23
|
+
logLevel: LogLevel.Info,
|
|
24
|
+
phone: {
|
|
25
|
+
mqttUrl: 'wss://mqtt.example.com:8084/mqtt',
|
|
26
|
+
ringtoneUrl: '/ring.wav',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
初始化完成后,即可创建并使用各业务模块客户端:
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import Phone from '@cvoc/phone';
|
|
35
|
+
|
|
36
|
+
const client = await Phone.createClient();
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## 方法
|
|
40
|
+
|
|
41
|
+
### `Config.init(options)`
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
static init(options: ConfigInitOptions): void;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
初始化全局配置。可在任何时机多次调用,后传入的字段会覆盖之前的值,未传入的字段保持原值。
|
|
48
|
+
|
|
49
|
+
#### `options` 参数说明
|
|
50
|
+
|
|
51
|
+
| 字段 | 类型 | 必填 | 说明 |
|
|
52
|
+
| :--- | :--- | :--- | :--- |
|
|
53
|
+
| `apiUrl` | `string` | ✅ | API 服务基础地址,如 `https://api.example.com/admin-api/` |
|
|
54
|
+
| `accessToken` | `string` | ✅ | 访问令牌,HTTP 请求会携带 `Authorization: Bearer <token>` |
|
|
55
|
+
| `refreshToken` | `string` | ❌ | 刷新令牌,访问令牌过期时用于换取新的令牌;不传或为空时不刷新 |
|
|
56
|
+
| `tenantId` | `string \| number` | ✅ | 租户 ID |
|
|
57
|
+
| `logLevel` | `LogLevel` | ❌ | 全局日志级别。可选值:`'debug'`、`'info'`、`'warn'`、`'error'`、`'silent'` |
|
|
58
|
+
| `phone` | `PhoneConfig` | ❌ | `@cvoc/phone` 模块配置 |
|
|
59
|
+
| `phone.mqttUrl` | `string` | ✅ | MQTT 服务地址,如 `wss://mqtt.example.com:8084/mqtt` |
|
|
60
|
+
| `phone.ringtoneUrl` | `string` | ❌ | 来电铃声文件地址,如 `/ring.wav` |
|
|
61
|
+
| `phone.iceServers` | `IceServer[]` | ❌ | WebRTC ICE 服务器列表。每个条目包含 `urls`(必填)、`username`、`credential` |
|
|
62
|
+
|
|
63
|
+
### ⚠️ 注意事项
|
|
64
|
+
|
|
65
|
+
> - **调用时机**:必须在创建任何模块客户端之前调用 `Config.init()`,否则模块会抛出 `Config is not initialized` 异常。
|
|
66
|
+
> - **全局唯一**:`Config` 类无法实例化(私有构造函数),所有模块通过静态方法访问同一份配置数据。
|
|
67
|
+
> - **二次配置**:更新配置时直接再次调用 `Config.init({ ... })` 传入新值即可,无需清空。
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
MIT
|
package/dist/esm/config.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export interface GlobalConfig {
|
|
|
5
5
|
apiUrl: string;
|
|
6
6
|
/** 访问令牌 */
|
|
7
7
|
accessToken: string;
|
|
8
|
+
/** 刷新令牌,用于访问令牌过期后换取新的令牌;不传或为空时不刷新令牌 */
|
|
9
|
+
refreshToken?: string;
|
|
8
10
|
/** 租户 ID */
|
|
9
11
|
tenantId: string | number;
|
|
10
12
|
/** 全局日志级别,设置后对所有模块日志生效 */
|
|
@@ -33,7 +35,7 @@ export declare class Config {
|
|
|
33
35
|
private constructor();
|
|
34
36
|
/**
|
|
35
37
|
* 初始化全局配置,必须在创建任何模块客户端之前显式调用。
|
|
36
|
-
* apiUrl/accessToken/tenantId/logLevel 为公共字段,phone 为 phone 模块私有配置。
|
|
38
|
+
* apiUrl/accessToken/refreshToken/tenantId/logLevel 为公共字段,phone 为 phone 模块私有配置。
|
|
37
39
|
*/
|
|
38
40
|
static init(options: ConfigInitOptions): void;
|
|
39
41
|
/** 是否已调用过 init */
|
|
@@ -42,6 +44,14 @@ export declare class Config {
|
|
|
42
44
|
static ensureInitialized(): void;
|
|
43
45
|
/** 获取全局公共配置 */
|
|
44
46
|
static getGlobalConfig(): Readonly<GlobalConfig>;
|
|
47
|
+
/**
|
|
48
|
+
* 令牌刷新成功后回写全局配置,使其他模块与后续新建的客户端实例能读取到最新令牌。
|
|
49
|
+
* refreshToken 为 undefined 时不覆盖(兼容后端不轮换 refreshToken 的情况)。
|
|
50
|
+
*/
|
|
51
|
+
static updateTokens(tokens: {
|
|
52
|
+
accessToken: string;
|
|
53
|
+
refreshToken?: string;
|
|
54
|
+
}): void;
|
|
45
55
|
/** 获取 phone 模块配置 */
|
|
46
56
|
static getPhoneConfig(): Readonly<PhoneConfig>;
|
|
47
57
|
}
|
package/dist/esm/http.d.ts
CHANGED
|
@@ -3,39 +3,118 @@ export interface HttpClientOptions {
|
|
|
3
3
|
baseUrl: string;
|
|
4
4
|
/** 认证令牌,请求头携带 `Authorization: Bearer <token>` */
|
|
5
5
|
token: string;
|
|
6
|
+
/** 刷新令牌,访问令牌过期时用于换取新令牌;不传或为空时不刷新令牌 */
|
|
7
|
+
refreshToken?: string;
|
|
6
8
|
/** 租户 ID */
|
|
7
9
|
tenantId: string | number;
|
|
8
10
|
}
|
|
11
|
+
export interface RequestOptions {
|
|
12
|
+
method: "GET" | "POST" | "PUT" | "DELETE";
|
|
13
|
+
/** json: application/json; form: application/x-www-form-urlencoded */
|
|
14
|
+
contentType: "json" | "form";
|
|
15
|
+
body?: Record<string, unknown>;
|
|
16
|
+
}
|
|
9
17
|
export interface ApiResult<T = unknown> {
|
|
10
18
|
code: number;
|
|
11
19
|
msg: string;
|
|
12
20
|
data: T;
|
|
13
21
|
}
|
|
14
|
-
/**
|
|
22
|
+
/** 系统用户基础信息 */
|
|
23
|
+
export interface AgentUserInfo {
|
|
24
|
+
/** 用户 ID */
|
|
25
|
+
id: number;
|
|
26
|
+
/** 用户昵称 */
|
|
27
|
+
nickname: string;
|
|
28
|
+
/** 帐号状态 */
|
|
29
|
+
status: number;
|
|
30
|
+
/** 部门编号 */
|
|
31
|
+
deptId: number;
|
|
32
|
+
/** 岗位编号数组(JSON 字符串) */
|
|
33
|
+
postIds: string;
|
|
34
|
+
/** 手机号码 */
|
|
35
|
+
mobile: string;
|
|
36
|
+
/** 用户邮箱 */
|
|
37
|
+
email: string;
|
|
38
|
+
/** 用户头像 */
|
|
39
|
+
avatar: string;
|
|
40
|
+
transMap: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
/** 可呼叫号码 */
|
|
43
|
+
export interface CallNumber {
|
|
44
|
+
/** 号码(拨打用原值) */
|
|
45
|
+
number: string;
|
|
46
|
+
/** 外显号码(未配置时为空) */
|
|
47
|
+
displayNumber?: string;
|
|
48
|
+
}
|
|
49
|
+
/** 注册服务器信息 */
|
|
50
|
+
export interface RegisterServer {
|
|
51
|
+
/** 坐席注册地址(外网) */
|
|
52
|
+
registerExternalAddr: string;
|
|
53
|
+
/** 坐席注册地址(内网) */
|
|
54
|
+
registerInternalAddr: string;
|
|
55
|
+
}
|
|
56
|
+
/** 坐席信息(坐席档案 + 系统用户信息) */
|
|
15
57
|
export interface AgentInfo {
|
|
16
|
-
/**
|
|
58
|
+
/** 编号 */
|
|
17
59
|
id: number;
|
|
18
|
-
/**
|
|
60
|
+
/** 关联用户编号 */
|
|
61
|
+
userId: number;
|
|
62
|
+
/** 用户昵称 */
|
|
63
|
+
userNickname: string;
|
|
64
|
+
/** 关联用户信息(系统用户基础信息) */
|
|
65
|
+
userInfo: AgentUserInfo;
|
|
66
|
+
/** 坐席工号 */
|
|
19
67
|
agentNo: string;
|
|
20
|
-
/**
|
|
21
|
-
im: string;
|
|
22
|
-
/** SIP 账号 */
|
|
23
|
-
sip: string;
|
|
24
|
-
/** 登录模式, 1:软电话,2:话机,3:手机 */
|
|
25
|
-
loginMode: number;
|
|
26
|
-
/** 坐席分组 ID */
|
|
68
|
+
/** 所属坐席组编号 */
|
|
27
69
|
groupId: number;
|
|
28
|
-
/**
|
|
70
|
+
/** 所属坐席组名称 */
|
|
29
71
|
groupName: string;
|
|
30
|
-
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
72
|
+
/** 并行服务客户数上限 */
|
|
73
|
+
parallelism: number;
|
|
74
|
+
/** 坐席级别:1-普通 2-班长 */
|
|
75
|
+
agentLevel: number;
|
|
76
|
+
/** 技能等级:1-初级 2-中级 3-高级 4-专家 */
|
|
77
|
+
skillLevel: number;
|
|
78
|
+
/** 技能标签(JSON 数组字符串) */
|
|
79
|
+
skills: string;
|
|
80
|
+
/** 分机号 */
|
|
81
|
+
extension: string;
|
|
82
|
+
/** 直线号码 */
|
|
83
|
+
directNumber: string;
|
|
84
|
+
/** 外显号码 */
|
|
85
|
+
displayNumber: string;
|
|
86
|
+
/** SIP 号码 */
|
|
87
|
+
sip: string;
|
|
88
|
+
/** IM 账号 */
|
|
89
|
+
im: string;
|
|
90
|
+
/** 邮箱 */
|
|
91
|
+
email: string;
|
|
92
|
+
/** 振铃超时(秒) */
|
|
93
|
+
ringTimeout: number;
|
|
94
|
+
/** 呼叫权限:0-双向 1-仅呼入 2-仅呼出 */
|
|
95
|
+
callDirection: number;
|
|
96
|
+
/** 最大外呼并发 */
|
|
97
|
+
maxOutbound: number;
|
|
98
|
+
/** 登录模式:1-软电话 2-话机 3-手机 */
|
|
99
|
+
loginMode: number;
|
|
100
|
+
/** 语音信箱开关 */
|
|
101
|
+
voicemailEnable: boolean;
|
|
102
|
+
/** 通话录音开关 */
|
|
103
|
+
recordingEnable: boolean;
|
|
104
|
+
/** 满意度评价开关 */
|
|
105
|
+
satisfactionEnable: boolean;
|
|
106
|
+
/** 状态(0 启用 / 1 停用) */
|
|
107
|
+
status: number;
|
|
108
|
+
/** 是否在线(Redis 运行时存在) */
|
|
109
|
+
online?: boolean;
|
|
110
|
+
/** 注册服务器信息(未绑定时为空) */
|
|
111
|
+
registerServer?: RegisterServer;
|
|
112
|
+
/** 当前坐席可用的呼叫用户号码集合(坐席绑定优先、坐席组绑定兜底,均无绑定为空集合) */
|
|
113
|
+
userNumbers: CallNumber[];
|
|
114
|
+
/** 当前坐席可用的呼叫坐席号码集合(口径同 userNumbers) */
|
|
115
|
+
agentNumbers: CallNumber[];
|
|
116
|
+
/** 创建时间 */
|
|
117
|
+
createTime: string;
|
|
39
118
|
}
|
|
40
119
|
/** 刷新令牌返回的认证信息 */
|
|
41
120
|
export interface RefreshTokenInfo {
|
|
@@ -48,67 +127,53 @@ export interface RefreshTokenInfo {
|
|
|
48
127
|
/** 过期时间戳(毫秒) */
|
|
49
128
|
expiresTime: number;
|
|
50
129
|
}
|
|
51
|
-
/** 坐席外呼参数 */
|
|
52
|
-
export interface DialParams {
|
|
53
|
-
/** 客户号码 */
|
|
54
|
-
customerNumber: string;
|
|
55
|
-
/** 客户名称 */
|
|
56
|
-
customerName: string;
|
|
57
|
-
/** 主叫号码 */
|
|
58
|
-
userNumber: string;
|
|
59
|
-
/** 主叫号码类型,1:手机(默认), 2:分机支持分机呼叫 */
|
|
60
|
-
dniType?: number;
|
|
61
|
-
}
|
|
62
|
-
/** 坐席外呼返回的通话标识 */
|
|
63
|
-
export interface DialResult {
|
|
64
|
-
/** A 腿(主叫侧)通话 ID */
|
|
65
|
-
alegId: string;
|
|
66
|
-
/** B 腿(被叫侧)通话 ID */
|
|
67
|
-
blegId: string;
|
|
68
|
-
/** 通话 ID */
|
|
69
|
-
callId: string;
|
|
70
|
-
}
|
|
71
|
-
/** 坐席挂机参数 */
|
|
72
|
-
export interface HuangupParams {
|
|
73
|
-
/** 通话唯一标识 */
|
|
74
|
-
uuid: string;
|
|
75
|
-
/** 挂机原因,默认 NORMAL_CLEARNG */
|
|
76
|
-
case?: string;
|
|
77
|
-
}
|
|
78
130
|
export declare class HttpClient {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
131
|
+
protected baseUrl: string;
|
|
132
|
+
protected token: string;
|
|
133
|
+
/** 刷新令牌,访问令牌过期时用于换取新令牌 */
|
|
134
|
+
protected refreshToken: string;
|
|
135
|
+
protected tenantId: string | number;
|
|
82
136
|
/** 刷新中的令牌请求,并发 401 时复用同一个请求 */
|
|
83
|
-
|
|
137
|
+
protected refreshing: Promise<boolean> | null;
|
|
138
|
+
/** 刷新失败标记,置位后不再自动刷新,避免 refreshToken 失效时反复打刷新接口;手动 setToken/setRefreshToken 后重置 */
|
|
139
|
+
protected refreshFailed: boolean;
|
|
84
140
|
constructor(options: HttpClientOptions);
|
|
85
141
|
/** 设置接口基础地址 */
|
|
86
142
|
setBaseUrl(baseUrl: string): void;
|
|
87
143
|
/** 更新认证令牌 */
|
|
88
144
|
setToken(token: string): void;
|
|
145
|
+
/** 更新刷新令牌 */
|
|
146
|
+
setRefreshToken(refreshToken: string): void;
|
|
89
147
|
/** 坐席签入 */
|
|
90
148
|
checkIn(presence?: string): Promise<boolean>;
|
|
91
149
|
/** 坐席签出 */
|
|
92
150
|
checkOut(): Promise<boolean>;
|
|
93
151
|
/** 坐席心跳 */
|
|
94
152
|
heartbeat(body?: Record<string, unknown>): Promise<boolean>;
|
|
95
|
-
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
/** 查询坐席信息,仅返回 im 与 id */
|
|
153
|
+
/**
|
|
154
|
+
* 更新坐席状态(示忙/示闲等)
|
|
155
|
+
* @param reason 状态原因文本(切小休时必填,如:用餐、休息、培训、会议;其余状态忽略)
|
|
156
|
+
*/
|
|
157
|
+
agentPresence(presence: string, reason?: string): Promise<boolean>;
|
|
158
|
+
/** 查询坐席信息(坐席档案 + 系统用户信息) */
|
|
102
159
|
agentInfo(): Promise<AgentInfo | null>;
|
|
103
160
|
/** 获取 MQTT 连接令牌,只返回 token 字符串 */
|
|
104
161
|
getMqttToken(): Promise<string>;
|
|
105
|
-
/**
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
|
|
162
|
+
/** 刷新认证令牌:使用 refreshToken 换取新令牌,成功后更新本地及全局配置的 accessToken 与 refreshToken */
|
|
163
|
+
doRefreshToken(refreshToken?: string): Promise<RefreshTokenInfo | null>;
|
|
164
|
+
/** 令牌失效时尝试刷新,并发场景共用同一次刷新请求;开关关闭或 refreshToken 为空时不刷新 */
|
|
165
|
+
protected tryRefreshToken(): Promise<boolean>;
|
|
109
166
|
/**
|
|
110
167
|
* 发起请求;当响应 code 为 401(如 {"code":401,"data":null,"msg":"账号未登录"})
|
|
111
168
|
* 且存在令牌时,先刷新令牌再重试一次。allowRefresh 防止刷新令牌接口自身无限重试。
|
|
169
|
+
* 供上层模块(如 phone)实现业务接口时复用。
|
|
170
|
+
*/
|
|
171
|
+
request<T>(path: string, options: RequestOptions, allowRefresh?: boolean): Promise<T>;
|
|
172
|
+
/**
|
|
173
|
+
* 下载文件(返回 Blob,用于同步导出等场景)。
|
|
174
|
+
* 自动携带 Authorization / Tenant-Id 请求头,401 时刷新令牌并重试一次。
|
|
175
|
+
* @param path 请求路径(可含 querystring)
|
|
176
|
+
* @param allowRefresh 是否允许 401 时自动刷新令牌并重试
|
|
112
177
|
*/
|
|
113
|
-
|
|
178
|
+
download(path: string, allowRefresh?: boolean): Promise<Blob>;
|
|
114
179
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @cvoc/core v0.
|
|
1
|
+
/* @cvoc/core v0.8.0 | MIT */
|
|
2
2
|
import mqtt from 'mqtt';
|
|
3
3
|
|
|
4
4
|
const LogLevel = {
|
|
@@ -74,6 +74,7 @@ const log = new Logger();
|
|
|
74
74
|
const globalConfig = {
|
|
75
75
|
apiUrl: "",
|
|
76
76
|
accessToken: "",
|
|
77
|
+
refreshToken: "",
|
|
77
78
|
tenantId: "",
|
|
78
79
|
};
|
|
79
80
|
// phone 模块私有配置
|
|
@@ -83,16 +84,19 @@ class Config {
|
|
|
83
84
|
constructor() { }
|
|
84
85
|
/**
|
|
85
86
|
* 初始化全局配置,必须在创建任何模块客户端之前显式调用。
|
|
86
|
-
* apiUrl/accessToken/tenantId/logLevel 为公共字段,phone 为 phone 模块私有配置。
|
|
87
|
+
* apiUrl/accessToken/refreshToken/tenantId/logLevel 为公共字段,phone 为 phone 模块私有配置。
|
|
87
88
|
*/
|
|
88
89
|
static init(options) {
|
|
89
|
-
const { apiUrl, accessToken, tenantId, logLevel, phone } = options;
|
|
90
|
+
const { apiUrl, accessToken, refreshToken, tenantId, logLevel, phone } = options;
|
|
90
91
|
if (apiUrl !== undefined) {
|
|
91
92
|
globalConfig.apiUrl = apiUrl;
|
|
92
93
|
}
|
|
93
94
|
if (accessToken !== undefined) {
|
|
94
95
|
globalConfig.accessToken = accessToken;
|
|
95
96
|
}
|
|
97
|
+
if (refreshToken !== undefined) {
|
|
98
|
+
globalConfig.refreshToken = refreshToken;
|
|
99
|
+
}
|
|
96
100
|
if (tenantId !== undefined) {
|
|
97
101
|
globalConfig.tenantId = tenantId;
|
|
98
102
|
}
|
|
@@ -118,6 +122,16 @@ class Config {
|
|
|
118
122
|
static getGlobalConfig() {
|
|
119
123
|
return globalConfig;
|
|
120
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* 令牌刷新成功后回写全局配置,使其他模块与后续新建的客户端实例能读取到最新令牌。
|
|
127
|
+
* refreshToken 为 undefined 时不覆盖(兼容后端不轮换 refreshToken 的情况)。
|
|
128
|
+
*/
|
|
129
|
+
static updateTokens(tokens) {
|
|
130
|
+
globalConfig.accessToken = tokens.accessToken;
|
|
131
|
+
if (tokens.refreshToken !== undefined) {
|
|
132
|
+
globalConfig.refreshToken = tokens.refreshToken;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
121
135
|
/** 获取 phone 模块配置 */
|
|
122
136
|
static getPhoneConfig() {
|
|
123
137
|
return phoneConfig;
|
|
@@ -216,8 +230,11 @@ class HttpClient {
|
|
|
216
230
|
constructor(options) {
|
|
217
231
|
/** 刷新中的令牌请求,并发 401 时复用同一个请求 */
|
|
218
232
|
this.refreshing = null;
|
|
233
|
+
/** 刷新失败标记,置位后不再自动刷新,避免 refreshToken 失效时反复打刷新接口;手动 setToken/setRefreshToken 后重置 */
|
|
234
|
+
this.refreshFailed = false;
|
|
219
235
|
this.baseUrl = options.baseUrl.replace(/\/+$/, "");
|
|
220
236
|
this.token = options.token ?? "";
|
|
237
|
+
this.refreshToken = options.refreshToken ?? "";
|
|
221
238
|
this.tenantId = options.tenantId ?? "";
|
|
222
239
|
}
|
|
223
240
|
/** 设置接口基础地址 */
|
|
@@ -227,6 +244,12 @@ class HttpClient {
|
|
|
227
244
|
/** 更新认证令牌 */
|
|
228
245
|
setToken(token) {
|
|
229
246
|
this.token = token;
|
|
247
|
+
this.refreshFailed = false;
|
|
248
|
+
}
|
|
249
|
+
/** 更新刷新令牌 */
|
|
250
|
+
setRefreshToken(refreshToken) {
|
|
251
|
+
this.refreshToken = refreshToken;
|
|
252
|
+
this.refreshFailed = false;
|
|
230
253
|
}
|
|
231
254
|
/** 坐席签入 */
|
|
232
255
|
async checkIn(presence = "AVAILABLE") {
|
|
@@ -263,39 +286,22 @@ class HttpClient {
|
|
|
263
286
|
}
|
|
264
287
|
return res.data;
|
|
265
288
|
}
|
|
266
|
-
/**
|
|
267
|
-
|
|
289
|
+
/**
|
|
290
|
+
* 更新坐席状态(示忙/示闲等)
|
|
291
|
+
* @param reason 状态原因文本(切小休时必填,如:用餐、休息、培训、会议;其余状态忽略)
|
|
292
|
+
*/
|
|
293
|
+
async agentPresence(presence, reason) {
|
|
268
294
|
const res = await this.request("/acc/acd/agent/presence", {
|
|
269
295
|
method: "PUT",
|
|
270
296
|
contentType: "json",
|
|
271
|
-
body: { presence },
|
|
297
|
+
body: reason ? { presence, reason } : { presence },
|
|
272
298
|
});
|
|
273
299
|
if (res.data !== true) {
|
|
274
300
|
logger$1.warn('agentPresence returned abnormal:', res);
|
|
275
301
|
}
|
|
276
302
|
return res.data;
|
|
277
303
|
}
|
|
278
|
-
/**
|
|
279
|
-
async dial(params) {
|
|
280
|
-
return this.request("/acc/agent-call/dial", {
|
|
281
|
-
method: "POST",
|
|
282
|
-
contentType: "json",
|
|
283
|
-
body: { ...params },
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
/** 坐席挂机 */
|
|
287
|
-
async hangup(params) {
|
|
288
|
-
const res = await this.request("/acc/agent-call/hangup", {
|
|
289
|
-
method: "POST",
|
|
290
|
-
contentType: "json",
|
|
291
|
-
body: { ...params },
|
|
292
|
-
});
|
|
293
|
-
if (res.data !== true) {
|
|
294
|
-
logger$1.warn('huangup returned abnormal:', res);
|
|
295
|
-
}
|
|
296
|
-
return res.data;
|
|
297
|
-
}
|
|
298
|
-
/** 查询坐席信息,仅返回 im 与 id */
|
|
304
|
+
/** 查询坐席信息(坐席档案 + 系统用户信息) */
|
|
299
305
|
async agentInfo() {
|
|
300
306
|
const res = await this.request("/acc/acd/agent/self", {
|
|
301
307
|
method: "GET",
|
|
@@ -320,43 +326,21 @@ class HttpClient {
|
|
|
320
326
|
}
|
|
321
327
|
return res.data.token;
|
|
322
328
|
}
|
|
323
|
-
/**
|
|
324
|
-
async
|
|
325
|
-
|
|
326
|
-
logger$1.warn('
|
|
327
|
-
return null;
|
|
328
|
-
}
|
|
329
|
-
const res = await this.request(`/system/system/auth/refresh-token?refreshToken=${refreshToken}`, {
|
|
330
|
-
method: "POST",
|
|
331
|
-
contentType: "json",
|
|
332
|
-
}, false);
|
|
333
|
-
if (!res.data?.accessToken) {
|
|
334
|
-
logger$1.warn('refreshToken returned abnormal:', res);
|
|
329
|
+
/** 刷新认证令牌:使用 refreshToken 换取新令牌,成功后更新本地及全局配置的 accessToken 与 refreshToken */
|
|
330
|
+
async doRefreshToken(refreshToken = this.refreshToken) {
|
|
331
|
+
{
|
|
332
|
+
logger$1.warn('doRefreshToken skipped: refresh token is disabled');
|
|
335
333
|
return null;
|
|
336
334
|
}
|
|
337
|
-
this.token = res.data.accessToken;
|
|
338
|
-
return res.data;
|
|
339
335
|
}
|
|
340
|
-
/**
|
|
336
|
+
/** 令牌失效时尝试刷新,并发场景共用同一次刷新请求;开关关闭或 refreshToken 为空时不刷新 */
|
|
341
337
|
async tryRefreshToken() {
|
|
342
|
-
|
|
343
|
-
return false;
|
|
344
|
-
if (!this.refreshing) {
|
|
345
|
-
this.refreshing = this.refreshToken()
|
|
346
|
-
.then((info) => info !== null)
|
|
347
|
-
.catch((error) => {
|
|
348
|
-
logger$1.warn('refreshToken failed:', error);
|
|
349
|
-
return false;
|
|
350
|
-
})
|
|
351
|
-
.finally(() => {
|
|
352
|
-
this.refreshing = null;
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
return this.refreshing;
|
|
338
|
+
return false;
|
|
356
339
|
}
|
|
357
340
|
/**
|
|
358
341
|
* 发起请求;当响应 code 为 401(如 {"code":401,"data":null,"msg":"账号未登录"})
|
|
359
342
|
* 且存在令牌时,先刷新令牌再重试一次。allowRefresh 防止刷新令牌接口自身无限重试。
|
|
343
|
+
* 供上层模块(如 phone)实现业务接口时复用。
|
|
360
344
|
*/
|
|
361
345
|
async request(path, options, allowRefresh = true) {
|
|
362
346
|
const headers = {
|
|
@@ -397,6 +381,45 @@ class HttpClient {
|
|
|
397
381
|
}
|
|
398
382
|
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
399
383
|
}
|
|
384
|
+
/**
|
|
385
|
+
* 下载文件(返回 Blob,用于同步导出等场景)。
|
|
386
|
+
* 自动携带 Authorization / Tenant-Id 请求头,401 时刷新令牌并重试一次。
|
|
387
|
+
* @param path 请求路径(可含 querystring)
|
|
388
|
+
* @param allowRefresh 是否允许 401 时自动刷新令牌并重试
|
|
389
|
+
*/
|
|
390
|
+
async download(path, allowRefresh = true) {
|
|
391
|
+
const headers = {
|
|
392
|
+
Authorization: `Bearer ${this.token}`,
|
|
393
|
+
"Tenant-Id": String(this.tenantId),
|
|
394
|
+
};
|
|
395
|
+
const requestToken = this.token;
|
|
396
|
+
const response = await fetch(`${this.baseUrl}${path}`, {
|
|
397
|
+
method: "GET",
|
|
398
|
+
headers,
|
|
399
|
+
});
|
|
400
|
+
if (response.ok) {
|
|
401
|
+
// 部分后端会错误地返回 JSON 错误体而非文件流
|
|
402
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
403
|
+
if (contentType.includes("application/json")) {
|
|
404
|
+
const result = (await response.json());
|
|
405
|
+
if (allowRefresh &&
|
|
406
|
+
result.code === 401 &&
|
|
407
|
+
(this.token !== requestToken || (await this.tryRefreshToken()))) {
|
|
408
|
+
logger$1.info('Token expired, refreshing and retrying download:', path);
|
|
409
|
+
return this.download(path, false);
|
|
410
|
+
}
|
|
411
|
+
throw new Error(`下载失败:${result.msg || "未知错误"} (code=${result.code})`);
|
|
412
|
+
}
|
|
413
|
+
return response.blob();
|
|
414
|
+
}
|
|
415
|
+
if (allowRefresh &&
|
|
416
|
+
response.status === 401 &&
|
|
417
|
+
(this.token !== requestToken || (await this.tryRefreshToken()))) {
|
|
418
|
+
logger$1.info('Token expired, refreshing and retrying download:', path);
|
|
419
|
+
return this.download(path, false);
|
|
420
|
+
}
|
|
421
|
+
throw new Error(`HTTP ${response.status} ${response.statusText}`);
|
|
422
|
+
}
|
|
400
423
|
}
|
|
401
424
|
|
|
402
425
|
// mqtt 的浏览器 ESM 产物只暴露 default export,故运行时代码一律走默认导入
|
|
@@ -800,7 +823,7 @@ class MessageClient {
|
|
|
800
823
|
}
|
|
801
824
|
|
|
802
825
|
/** @cvoc/core 版本号,值与 package.json 保持一致 */
|
|
803
|
-
const coreVersion = "0.
|
|
826
|
+
const coreVersion = "0.8.0";
|
|
804
827
|
|
|
805
828
|
export { Config, EventEmitter as Event, EventEmitter, HttpClient, Logger as Log, LogLevel, Logger, MessageClient as Message, MessageClient, MessageConnectionState, coreVersion, event, getGlobalLogLevel, log, setGlobalLogLevel };
|
|
806
829
|
//# sourceMappingURL=index.js.map
|