mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 22:19:20 +08:00
feat: 优化开放接口创建
This commit is contained in:
+1
@@ -38,6 +38,7 @@ public class OpenApiKeyController {
|
|||||||
return ApiResponse.success("获取 API Key 列表成功", list);
|
return ApiResponse.success("获取 API Key 列表成功", list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 当前公司开放域权限树(排除 api-key 管理;仅 api 叶子可绑定) */
|
||||||
@GetMapping("/available-permissions")
|
@GetMapping("/available-permissions")
|
||||||
@RequirePermission(value = "open:api-key:manage", description = "查询可绑定开放权限", allowSuper = true)
|
@RequirePermission(value = "open:api-key:manage", description = "查询可绑定开放权限", allowSuper = true)
|
||||||
public ApiResponse<List<OpenApiAvailablePermissionVO>> getAvailablePermissions() {
|
public ApiResponse<List<OpenApiAvailablePermissionVO>> getAvailablePermissions() {
|
||||||
|
|||||||
+14
@@ -2,6 +2,12 @@ package top.xyquan.ecomdmbbackend.modules.open.key.model.vo;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可绑到 API Key 的开放权限节点;树形返回公司开放域权限。
|
||||||
|
* companyPermissionId 仅在可勾选叶子(bindable)上有值。
|
||||||
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class OpenApiAvailablePermissionVO {
|
public class OpenApiAvailablePermissionVO {
|
||||||
|
|
||||||
@@ -12,4 +18,12 @@ public class OpenApiAvailablePermissionVO {
|
|||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
/** 权限类型:module / api 等 */
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
/** 是否可作为 Key 绑定的叶子 scope */
|
||||||
|
private Boolean bindable;
|
||||||
|
|
||||||
|
private List<OpenApiAvailablePermissionVO> children;
|
||||||
}
|
}
|
||||||
|
|||||||
+96
-24
@@ -25,12 +25,17 @@ import top.xyquan.ecomdmbbackend.modules.org.company.permission.mapper.CompanyPe
|
|||||||
import top.xyquan.ecomdmbbackend.modules.org.company.permission.model.po.CompanyPermissionPO;
|
import top.xyquan.ecomdmbbackend.modules.org.company.permission.model.po.CompanyPermissionPO;
|
||||||
import top.xyquan.ecomdmbbackend.modules.sys.permission.mapper.PermissionMapper;
|
import top.xyquan.ecomdmbbackend.modules.sys.permission.mapper.PermissionMapper;
|
||||||
import top.xyquan.ecomdmbbackend.modules.sys.permission.model.po.PermissionPO;
|
import top.xyquan.ecomdmbbackend.modules.sys.permission.model.po.PermissionPO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.sys.permission.model.vo.PermissionVO;
|
||||||
|
import top.xyquan.ecomdmbbackend.modules.sys.permission.service.PermissionService;
|
||||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -40,10 +45,14 @@ public class OpenApiKeyServiceImpl implements OpenApiKeyService {
|
|||||||
|
|
||||||
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
|
||||||
|
|
||||||
|
/** 控制台 Key 管理权限,不可绑到开放 API Key */
|
||||||
|
private static final String OPEN_API_KEY_MANAGE_CODE = "open:api-key:manage";
|
||||||
|
|
||||||
private final OpenApiKeyMapper openApiKeyMapper;
|
private final OpenApiKeyMapper openApiKeyMapper;
|
||||||
private final OpenApiKeyPermissionMapper openApiKeyPermissionMapper;
|
private final OpenApiKeyPermissionMapper openApiKeyPermissionMapper;
|
||||||
private final CompanyPermissionMapper companyPermissionMapper;
|
private final CompanyPermissionMapper companyPermissionMapper;
|
||||||
private final PermissionMapper permissionMapper;
|
private final PermissionMapper permissionMapper;
|
||||||
|
private final PermissionService permissionService;
|
||||||
private final CurrentUserContext currentUserContext;
|
private final CurrentUserContext currentUserContext;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
@@ -169,21 +178,96 @@ public class OpenApiKeyServiceImpl implements OpenApiKeyService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前公司开放域权限树(已分配 + 祖先),供创建 Key 勾选。
|
||||||
|
* 仅 type=api 且 open:*(排除 api-key 管理)的叶子可绑定。
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<OpenApiAvailablePermissionVO> getAvailablePermissions() {
|
public List<OpenApiAvailablePermissionVO> getAvailablePermissions() {
|
||||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||||
return companyPermissionMapper.selectPermissionsByCompanyId(companyId).stream()
|
Map<Long, Long> permissionIdToCompanyPermissionId = loadCompanyPermissionIdMap(companyId);
|
||||||
.filter(p -> isBindableOpenApiPermission(p.getCode(), p.getType()))
|
List<PermissionVO> companyTree = permissionService.getCompanyVisiblePermissionTree(companyId);
|
||||||
.map(p -> {
|
List<PermissionVO> openRoots = extractOpenRoots(companyTree);
|
||||||
CompanyPermissionPO cp = getCompanyPermission(companyId, p.getId());
|
|
||||||
|
List<OpenApiAvailablePermissionVO> result = new ArrayList<>();
|
||||||
|
for (PermissionVO root : openRoots) {
|
||||||
|
OpenApiAvailablePermissionVO node = toAvailableTreeNode(root, permissionIdToCompanyPermissionId);
|
||||||
|
if (node != null) {
|
||||||
|
result.add(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Long, Long> loadCompanyPermissionIdMap(Long companyId) {
|
||||||
|
LambdaQueryWrapper<CompanyPermissionPO> wrapper = new LambdaQueryWrapper<>();
|
||||||
|
wrapper.eq(CompanyPermissionPO::getCompanyId, companyId);
|
||||||
|
Map<Long, Long> map = new HashMap<>();
|
||||||
|
for (CompanyPermissionPO cp : companyPermissionMapper.selectList(wrapper)) {
|
||||||
|
map.put(cp.getPermissionId(), cp.getId());
|
||||||
|
}
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 从公司可见树中抽出 open 域(根 code=open 或 open:*) */
|
||||||
|
private List<PermissionVO> extractOpenRoots(List<PermissionVO> nodes) {
|
||||||
|
if (CollectionUtils.isEmpty(nodes)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<PermissionVO> result = new ArrayList<>();
|
||||||
|
for (PermissionVO node : nodes) {
|
||||||
|
String code = node.getCode();
|
||||||
|
if (isOpenDomainCode(code)) {
|
||||||
|
result.add(node);
|
||||||
|
} else if (!CollectionUtils.isEmpty(node.getChildren())) {
|
||||||
|
result.addAll(extractOpenRoots(node.getChildren()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isOpenDomainCode(String code) {
|
||||||
|
return "open".equals(code) || (code != null && code.startsWith("open:"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private OpenApiAvailablePermissionVO toAvailableTreeNode(
|
||||||
|
PermissionVO src, Map<Long, Long> permissionIdToCompanyPermissionId) {
|
||||||
|
if (src == null || OPEN_API_KEY_MANAGE_CODE.equals(src.getCode())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<OpenApiAvailablePermissionVO> childVos = new ArrayList<>();
|
||||||
|
if (!CollectionUtils.isEmpty(src.getChildren())) {
|
||||||
|
for (PermissionVO child : src.getChildren()) {
|
||||||
|
OpenApiAvailablePermissionVO childVo =
|
||||||
|
toAvailableTreeNode(child, permissionIdToCompanyPermissionId);
|
||||||
|
if (childVo != null) {
|
||||||
|
childVos.add(childVo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean bindable = isBindableOpenApiPermission(src.getCode(), src.getType());
|
||||||
|
Long companyPermissionId = permissionIdToCompanyPermissionId.get(src.getId());
|
||||||
|
|
||||||
|
// 不可绑定且无子节点:对创建 Key 无意义,丢弃
|
||||||
|
if (!bindable && childVos.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 可绑定叶子必须已落在公司权限表
|
||||||
|
if (bindable && companyPermissionId == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
OpenApiAvailablePermissionVO vo = new OpenApiAvailablePermissionVO();
|
OpenApiAvailablePermissionVO vo = new OpenApiAvailablePermissionVO();
|
||||||
vo.setCompanyPermissionId(cp.getId());
|
vo.setPermissionId(src.getId());
|
||||||
vo.setPermissionId(p.getId());
|
vo.setCode(src.getCode());
|
||||||
vo.setCode(p.getCode());
|
vo.setName(src.getName());
|
||||||
vo.setName(p.getName());
|
vo.setType(src.getType());
|
||||||
|
vo.setBindable(bindable);
|
||||||
|
vo.setCompanyPermissionId(bindable ? companyPermissionId : null);
|
||||||
|
vo.setChildren(childVos.isEmpty() ? null : childVos);
|
||||||
return vo;
|
return vo;
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<CompanyPermissionPO> validateAndLoadCompanyPermissions(Long companyId, List<Long> companyPermissionIds) {
|
private List<CompanyPermissionPO> validateAndLoadCompanyPermissions(Long companyId, List<Long> companyPermissionIds) {
|
||||||
@@ -191,7 +275,7 @@ public class OpenApiKeyServiceImpl implements OpenApiKeyService {
|
|||||||
throw new BusinessException("权限不能为空");
|
throw new BusinessException("权限不能为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
List<CompanyPermissionPO> result = companyPermissionIds.stream()
|
return companyPermissionIds.stream()
|
||||||
.distinct()
|
.distinct()
|
||||||
.map(id -> {
|
.map(id -> {
|
||||||
CompanyPermissionPO cp = companyPermissionMapper.selectById(id);
|
CompanyPermissionPO cp = companyPermissionMapper.selectById(id);
|
||||||
@@ -206,18 +290,6 @@ public class OpenApiKeyServiceImpl implements OpenApiKeyService {
|
|||||||
return cp;
|
return cp;
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private CompanyPermissionPO getCompanyPermission(Long companyId, Long permissionId) {
|
|
||||||
LambdaQueryWrapper<CompanyPermissionPO> wrapper = new LambdaQueryWrapper<>();
|
|
||||||
wrapper.eq(CompanyPermissionPO::getCompanyId, companyId)
|
|
||||||
.eq(CompanyPermissionPO::getPermissionId, permissionId);
|
|
||||||
CompanyPermissionPO cp = companyPermissionMapper.selectOne(wrapper);
|
|
||||||
if (cp == null) {
|
|
||||||
throw new BusinessException("公司未分配该开放权限");
|
|
||||||
}
|
|
||||||
return cp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private OpenApiKeyPO getOwnedKey(Long id) {
|
private OpenApiKeyPO getOwnedKey(Long id) {
|
||||||
@@ -276,6 +348,6 @@ public class OpenApiKeyServiceImpl implements OpenApiKeyService {
|
|||||||
if (!"api".equals(type) || code == null) {
|
if (!"api".equals(type) || code == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return code.startsWith("open:") && !"open:api-key:manage".equals(code);
|
return code.startsWith("open:") && !OPEN_API_KEY_MANAGE_CODE.equals(code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,15 @@ export namespace OpenApiKeyDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export namespace OpenApiKeyVO {
|
export namespace OpenApiKeyVO {
|
||||||
|
/** 可绑开放权限节点;树形,仅 bindable 叶子带 companyPermissionId */
|
||||||
export interface OpenApiAvailablePermissionVO {
|
export interface OpenApiAvailablePermissionVO {
|
||||||
companyPermissionId: number;
|
companyPermissionId?: number;
|
||||||
permissionId: number;
|
permissionId: number;
|
||||||
code: string;
|
code: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
type?: string;
|
||||||
|
bindable?: boolean;
|
||||||
|
children?: OpenApiAvailablePermissionVO[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OpenApiKeyInfoVO {
|
export interface OpenApiKeyInfoVO {
|
||||||
|
|||||||
+179
-26
@@ -7,14 +7,15 @@ import { useVbenModal } from '@vben/common-ui';
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
Alert,
|
Alert,
|
||||||
Checkbox,
|
|
||||||
DatePicker,
|
DatePicker,
|
||||||
|
Empty,
|
||||||
Form,
|
Form,
|
||||||
Input,
|
Input,
|
||||||
InputNumber,
|
InputNumber,
|
||||||
message,
|
message,
|
||||||
Select,
|
Select,
|
||||||
Tag,
|
Tag,
|
||||||
|
Tree,
|
||||||
} from 'ant-design-vue';
|
} from 'ant-design-vue';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -30,9 +31,18 @@ defineOptions({ name: 'CreateApiKeyModal' });
|
|||||||
|
|
||||||
const emit = defineEmits<Emits>();
|
const emit = defineEmits<Emits>();
|
||||||
|
|
||||||
|
type PermissionTreeNode = {
|
||||||
|
key: number;
|
||||||
|
title: string;
|
||||||
|
code?: string;
|
||||||
|
bindable?: boolean;
|
||||||
|
companyPermissionId?: number;
|
||||||
|
children?: PermissionTreeNode[];
|
||||||
|
};
|
||||||
|
|
||||||
const [Modal, modalApi] = useVbenModal({
|
const [Modal, modalApi] = useVbenModal({
|
||||||
draggable: true,
|
draggable: true,
|
||||||
class: 'w-[560px]',
|
class: 'w-[820px]',
|
||||||
onCancel() {
|
onCancel() {
|
||||||
handleCancel();
|
handleCancel();
|
||||||
},
|
},
|
||||||
@@ -43,7 +53,9 @@ const [Modal, modalApi] = useVbenModal({
|
|||||||
|
|
||||||
const formRef = ref();
|
const formRef = ref();
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const permissionOptions = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
|
const permissionTree = ref<OpenApiKeyVO.OpenApiAvailablePermissionVO[]>([]);
|
||||||
|
const checkedPermissionIds = ref<number[]>([]);
|
||||||
|
const expandedKeys = ref<number[]>([]);
|
||||||
const createdKey = ref<string | null>(null);
|
const createdKey = ref<string | null>(null);
|
||||||
|
|
||||||
const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
||||||
@@ -54,10 +66,104 @@ const formData = ref<OpenApiKeyDTO.CreateOpenApiKeyDTO>({
|
|||||||
ipWhitelist: [],
|
ipWhitelist: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const modalTitle = computed(() => (createdKey.value ? 'API Key 已创建' : '创建 API Key'));
|
const modalTitle = computed(() =>
|
||||||
|
createdKey.value ? 'API Key 已创建' : '创建 API Key',
|
||||||
|
);
|
||||||
|
|
||||||
|
const treeData = computed(() => mapToTreeData(permissionTree.value));
|
||||||
|
|
||||||
|
const hasBindablePermission = computed(
|
||||||
|
() => collectBindableCompanyPermissionIds(permissionTree.value).length > 0,
|
||||||
|
);
|
||||||
|
|
||||||
|
const selectedBindableCount = computed(
|
||||||
|
() => formData.value.companyPermissionIds.length,
|
||||||
|
);
|
||||||
|
|
||||||
async function loadPermissions() {
|
async function loadPermissions() {
|
||||||
permissionOptions.value = await getOpenApiKeyAvailablePermissionsApi();
|
permissionTree.value = await getOpenApiKeyAvailablePermissionsApi();
|
||||||
|
expandedKeys.value = collectExpandableKeys(permissionTree.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapToTreeData(
|
||||||
|
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||||
|
): PermissionTreeNode[] {
|
||||||
|
return nodes.map((node) => ({
|
||||||
|
key: node.permissionId,
|
||||||
|
title: node.name,
|
||||||
|
code: node.code,
|
||||||
|
bindable: node.bindable === true,
|
||||||
|
companyPermissionId: node.companyPermissionId,
|
||||||
|
children:
|
||||||
|
node.children && node.children.length > 0
|
||||||
|
? mapToTreeData(node.children)
|
||||||
|
: undefined,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectExpandableKeys(
|
||||||
|
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||||
|
): number[] {
|
||||||
|
const keys: number[] = [];
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (node.children && node.children.length > 0) {
|
||||||
|
keys.push(node.permissionId);
|
||||||
|
keys.push(...collectExpandableKeys(node.children));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 树中全部可绑定叶子的 companyPermissionId */
|
||||||
|
function collectBindableCompanyPermissionIds(
|
||||||
|
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||||
|
): number[] {
|
||||||
|
const ids: number[] = [];
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (node.bindable && node.companyPermissionId != null) {
|
||||||
|
ids.push(node.companyPermissionId);
|
||||||
|
}
|
||||||
|
if (node.children?.length) {
|
||||||
|
ids.push(...collectBindableCompanyPermissionIds(node.children));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 根据勾选的 permissionId,收集可绑定叶子的 companyPermissionId */
|
||||||
|
function resolveCompanyPermissionIds(
|
||||||
|
nodes: OpenApiKeyVO.OpenApiAvailablePermissionVO[],
|
||||||
|
checkedPermissionIdSet: Set<number>,
|
||||||
|
): number[] {
|
||||||
|
const ids: number[] = [];
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (
|
||||||
|
node.bindable &&
|
||||||
|
node.companyPermissionId != null &&
|
||||||
|
checkedPermissionIdSet.has(node.permissionId)
|
||||||
|
) {
|
||||||
|
ids.push(node.companyPermissionId);
|
||||||
|
}
|
||||||
|
if (node.children?.length) {
|
||||||
|
ids.push(
|
||||||
|
...resolveCompanyPermissionIds(node.children, checkedPermissionIdSet),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTreeCheck(
|
||||||
|
checked:
|
||||||
|
| (number | string)[]
|
||||||
|
| { checked: (number | string)[]; halfChecked: (number | string)[] },
|
||||||
|
) {
|
||||||
|
const keys = Array.isArray(checked) ? checked : checked.checked;
|
||||||
|
checkedPermissionIds.value = keys.map((k) => Number(k));
|
||||||
|
formData.value.companyPermissionIds = resolveCompanyPermissionIds(
|
||||||
|
permissionTree.value,
|
||||||
|
new Set(checkedPermissionIds.value),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetForm() {
|
function resetForm() {
|
||||||
@@ -68,6 +174,7 @@ function resetForm() {
|
|||||||
rateLimit: 60,
|
rateLimit: 60,
|
||||||
ipWhitelist: [],
|
ipWhitelist: [],
|
||||||
};
|
};
|
||||||
|
checkedPermissionIds.value = [];
|
||||||
createdKey.value = null;
|
createdKey.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +189,7 @@ async function handleSubmit() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (permissionOptions.value.length === 0) {
|
if (!hasBindablePermission.value) {
|
||||||
message.warning('当前公司尚未分配开放接口权限,请联系平台管理员');
|
message.warning('当前公司尚未分配开放接口权限,请联系平台管理员');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -158,15 +265,19 @@ defineExpose({
|
|||||||
<Tag color="blue" class="cursor-pointer" @click="copyKey">点击复制 Key</Tag>
|
<Tag color="blue" class="cursor-pointer" @click="copyKey">点击复制 Key</Tag>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Form v-else ref="formRef" :model="formData" layout="vertical">
|
<Form v-else ref="formRef" :model="formData" layout="vertical" class="mb-0">
|
||||||
<Alert
|
<Alert
|
||||||
v-if="permissionOptions.length === 0"
|
v-if="!hasBindablePermission"
|
||||||
class="mb-4"
|
class="mb-4"
|
||||||
message="当前公司尚未分配开放接口权限(open:* 叶子 api),请先在「公司权限分配」中配置。"
|
message="当前公司尚未分配可绑定的开放接口权限,请先在「公司权限分配」中配置 open 域权限。"
|
||||||
show-icon
|
show-icon
|
||||||
type="info"
|
type="info"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||||
|
<!-- 左:基本信息 -->
|
||||||
|
<div class="min-w-0 space-y-1">
|
||||||
|
<div class="mb-2 text-sm font-medium text-foreground">基本信息</div>
|
||||||
<Form.Item
|
<Form.Item
|
||||||
label="Key 名称"
|
label="Key 名称"
|
||||||
name="name"
|
name="name"
|
||||||
@@ -175,24 +286,13 @@ defineExpose({
|
|||||||
<Input v-model:value="formData.name" placeholder="如:Agent-订单导入" />
|
<Input v-model:value="formData.name" placeholder="如:Agent-订单导入" />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item
|
|
||||||
label="开放权限"
|
|
||||||
name="companyPermissionIds"
|
|
||||||
:rules="[{ required: true, type: 'array', min: 1, message: '请选择至少一个权限' }]"
|
|
||||||
>
|
|
||||||
<Checkbox.Group v-model:value="formData.companyPermissionIds" class="flex flex-col gap-2">
|
|
||||||
<Checkbox
|
|
||||||
v-for="item in permissionOptions"
|
|
||||||
:key="item.companyPermissionId"
|
|
||||||
:value="item.companyPermissionId"
|
|
||||||
>
|
|
||||||
{{ item.name }} ({{ item.code }})
|
|
||||||
</Checkbox>
|
|
||||||
</Checkbox.Group>
|
|
||||||
</Form.Item>
|
|
||||||
|
|
||||||
<Form.Item label="每分钟请求上限" name="rateLimit">
|
<Form.Item label="每分钟请求上限" name="rateLimit">
|
||||||
<InputNumber v-model:value="formData.rateLimit" :min="1" :max="10000" class="w-full" />
|
<InputNumber
|
||||||
|
v-model:value="formData.rateLimit"
|
||||||
|
:min="1"
|
||||||
|
:max="10000"
|
||||||
|
class="w-full"
|
||||||
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
|
||||||
<Form.Item label="过期时间(可选)" name="expireAt">
|
<Form.Item label="过期时间(可选)" name="expireAt">
|
||||||
@@ -215,6 +315,59 @@ defineExpose({
|
|||||||
:token-separators="[',', ' ']"
|
:token-separators="[',', ' ']"
|
||||||
/>
|
/>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- 右:开放权限树 -->
|
||||||
|
<div class="min-w-0">
|
||||||
|
<div class="mb-2 flex items-center justify-between gap-2">
|
||||||
|
<span class="text-sm font-medium text-foreground">开放权限</span>
|
||||||
|
<span class="text-xs text-muted-foreground">
|
||||||
|
已选 {{ selectedBindableCount }} 项 scope
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<Form.Item
|
||||||
|
name="companyPermissionIds"
|
||||||
|
class="mb-0"
|
||||||
|
:rules="[
|
||||||
|
{
|
||||||
|
required: true,
|
||||||
|
type: 'array',
|
||||||
|
min: 1,
|
||||||
|
message: '请选择至少一个开放权限',
|
||||||
|
},
|
||||||
|
]"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="max-h-[360px] min-h-[280px] overflow-y-auto rounded-md border border-border px-2 py-2"
|
||||||
|
>
|
||||||
|
<Empty
|
||||||
|
v-if="!hasBindablePermission"
|
||||||
|
class="py-10"
|
||||||
|
description="暂无可选开放权限"
|
||||||
|
/>
|
||||||
|
<Tree
|
||||||
|
v-else
|
||||||
|
v-model:expanded-keys="expandedKeys"
|
||||||
|
checkable
|
||||||
|
:tree-data="treeData"
|
||||||
|
:checked-keys="checkedPermissionIds"
|
||||||
|
:selectable="false"
|
||||||
|
@check="handleTreeCheck"
|
||||||
|
>
|
||||||
|
<template #title="node">
|
||||||
|
<span>{{ node.title }}</span>
|
||||||
|
<span
|
||||||
|
v-if="node.code"
|
||||||
|
class="ml-1 text-xs text-muted-foreground"
|
||||||
|
>
|
||||||
|
{{ node.code }}
|
||||||
|
</span>
|
||||||
|
</template>
|
||||||
|
</Tree>
|
||||||
|
</div>
|
||||||
|
</Form.Item>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</Form>
|
</Form>
|
||||||
|
|
||||||
<template #confirmText>{{ createdKey ? '我已保存,关闭' : '创建' }}</template>
|
<template #confirmText>{{ createdKey ? '我已保存,关闭' : '创建' }}</template>
|
||||||
|
|||||||
@@ -25,12 +25,6 @@ const PlusIcon = createIconifyIcon('lucide:plus');
|
|||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const createModalRef = ref<InstanceType<typeof CreateApiKeyModal>>();
|
const createModalRef = ref<InstanceType<typeof CreateApiKeyModal>>();
|
||||||
|
|
||||||
const permissionLabelMap: Record<string, string> = {
|
|
||||||
'open:product:adfee': '商品广告费',
|
|
||||||
'open:shop:pdd:read': '拼多多店铺',
|
|
||||||
'open:extension:feature:read': '扩展功能',
|
|
||||||
};
|
|
||||||
|
|
||||||
function formatExpireAt(cellValue?: null | string) {
|
function formatExpireAt(cellValue?: null | string) {
|
||||||
return cellValue ? String(cellValue) : '永不过期';
|
return cellValue ? String(cellValue) : '永不过期';
|
||||||
}
|
}
|
||||||
@@ -127,10 +121,7 @@ function getPermissionTooltip(row: OpenApiKeyVO.OpenApiKeyInfoVO) {
|
|||||||
if (row.permissions?.length) {
|
if (row.permissions?.length) {
|
||||||
return row.permissions.map((item) => `${item.name} (${item.code})`);
|
return row.permissions.map((item) => `${item.name} (${item.code})`);
|
||||||
}
|
}
|
||||||
return (row.permissionCodes ?? []).map((code) => {
|
return row.permissionCodes ?? [];
|
||||||
const label = permissionLabelMap[code] || code;
|
|
||||||
return label === code ? code : `${label} (${code})`;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user