管理员客户管理 — TypeScript SDK
终端客户(bizUser)生命周期:查询、创建、删除。
概览
管理员 Customer 资源管理终端客户,即 bizUser 层、管理员租户下实际付费的用户,与管理员操作员本身不同。partner_admin 的范围为其所属租户;sys_admin 的范围根据后端策略更广。
通过 admin.customer 访问。
可用操作
| 方法 | 说明 |
|---|---|
list() | 查询管理员权限范围内的终端客户 |
create() | 创建终端客户 |
delete() | 按 ID 删除终端客户 |
list
分页查询当前管理员权限范围内的终端客户,支持通过 userName 模糊搜索。
使用示例
const result = await admin.customer.list({ pageNum: 1, pageSize: 20 });
console.log(`total: ${result.total}`);
for (const it of result.list) {
console.log(it.id, it.userName, it.email);
}
按名称搜索:
const result = await admin.customer.list({ userName: 'acme', pageNum: 1, pageSize: 10 });
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| pageNum | number | 否 | 1 | 页码,从 1 开始 |
| pageSize | number | 否 | 10 | 每页条数 |
| userName | string | 否 | -- | 按 userName 模糊搜索 |
响应
返回包含 total、list、totalPages、currentPage、pageSize 的对象。每条记录提供 id、userName、email、phone、timezone、createdAtUtc、updatedAtUtc、userType、parentId 等字段。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
2007 | PermissionError | 管理员缺少客户管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
create
在当前管理员租户下创建终端客户。成功时返回 true,不会回传新建 ID;如需获取,可通过 list() 按对应 userName 查询。
使用示例
const ok = await admin.customer.create({
userName: 'acme-user-1',
password: 'StrongPass!',
email: 'user1@acme.example',
});
console.log(ok); // true
获取新建 ID:
const result = await admin.customer.list({ userName: 'acme-user-1', pageNum: 1, pageSize: 1 });
const newId = result.list[0].id;
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| userName | string | 是 | -- | 新客户的 userName,在当前范围内必须唯一 |
| password | string | 是 | -- | 初始密码,服务器端使用 bcrypt 哈希 |
| string | 否 | -- | 联系邮箱 | |
| phone | string | 否 | -- | 联系电话 |
| timezone | string | 否 | Asia/Shanghai | IANA 时区 |
响应
返回 boolean,成功时为 true;失败时抛出对应异常。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | 缺少必填字段、密码强度不足或邮箱格式错误 |
2007 | PermissionError | 管理员缺少客户管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
| 服务器 4xx | WltError | 租户范围内存在重复 userName |
delete
按 ID 删除终端客户。
使用示例
const ok = await admin.customer.delete(42);
console.log(ok); // true
// Verify no residue
const remaining = await admin.customer.list({ userName: 'acme-user-1', pageNum: 1, pageSize: 10 });
console.assert(remaining.list.length === 0);
参数
| 参数 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| userId | number | 是 | -- | 待删除终端客户的 ID(位置参数) |
响应
返回 boolean,成功时为 true。
错误
| 代码 | 异常 | 发生条件 |
|---|---|---|
1001 | ValidationError | userId 无效 |
2007 | PermissionError | 管理员缺少客户管理权限 |
3001 | AuthenticationError | 会话过期且刷新失败 |
| 服务器 4xx | WltError | 客户不存在或超出权限范围 |
典型生命周期
完整的创建 → 验证 → 删除 → 验证流程,适用于资源开通脚本和集成测试:
const temp = 'provisioning-test';
const ok = await admin.customer.create({ userName: temp, password: 'StrongPass!', email: 't@example.com' });
const found = await admin.customer.list({ userName: temp, pageNum: 1, pageSize: 1 });
const newId = found.list[0].id;
// ... do work under `newId` ...
await admin.customer.delete(newId);
const after = await admin.customer.list({ userName: temp, pageNum: 1, pageSize: 10 });
console.assert(after.list.length === 0);