index.vue
6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<template>
<div>
<div class="mt-8">
<div class="flex">
<div class="flex" v-if="interfaceType === 'SYSTEM' && isAdmin(role)">
<Select
allowClear
v-model:value="selectTenant"
@change="onSelect"
style="width: 150px"
placeholder="请选择租户"
:options="selectOptions"
/>
<Select
v-model:value="selectSysTenant"
allowClear
@change="onSelctTenant"
style="width: 150px; margin-left: 10px"
v-if="selectTenantOptions.length > 0"
placeholder="请选择租户"
:options="selectTenantOptions"
/>
</div>
<Button
:disabled="interfaceType === 'SYSTEM' && isAdmin(role) ? testDisabled : false"
class="ml-2"
@click="handleTest(isSingleClickText)"
type="primary"
>{{ `${isSingleClickText === 'open' ? '打开' : '关闭'}测试接口` }}
</Button>
</div>
<div v-if="showTestEditCell" class="mt-8">
<a-row type="flex" justify="center">
<a-col :span="3"> 参数设置 </a-col>
<a-col :span="21">
<TestParamsCellTable :token="getToken" ref="testEditCellTableRef" />
</a-col>
</a-row>
</div>
</div>
<div class="mt-8"> </div>
</div>
</template>
<script lang="ts" setup name="testRequest">
import { ref, nextTick, onMounted } from 'vue';
import { Button } from 'ant-design-vue';
import TestParamsCellTable from './testEditParamsCellTable.vue';
import moment from 'moment';
import { useUtils } from '../../../hooks/useUtils';
import { useMessage } from '/@/hooks/web/useMessage';
import { getTenantAllPageLists, getTenantPageList } from '/@/api/tenant/tenantApi';
import { selectType } from '../../../types';
import { Select } from 'ant-design-vue';
import { getUserToken } from '/@/api/sys/user';
import { USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import { isAdmin } from '/@/enums/roleEnum';
const userInfo: any = getAuthCache(USER_INFO_KEY);
const role: string = userInfo?.roles[0];
const emits = defineEmits(['testParamsInterface', 'closeTest']);
const props = defineProps({
data: {
type: Object,
},
interfaceType: {
type: String,
},
});
onMounted(async () => {
if (isAdmin(role)) {
const res = await getTenantPageList();
selectOptions.value = res.map((m) => ({ label: m.name, value: m.tenantId }));
} else {
//租户
const { token } = await getUserToken(userInfo?.userId);
getToken.value = token;
}
});
const selectOptions = ref<selectType[]>([]);
const testDisabled = ref(true);
const selectTenantOptions = ref<selectType[]>([]);
const { createMessage } = useMessage();
const showTestEditCell = ref(false);
const excuteData = ref({});
const selectTenant = ref(undefined);
const selectSysTenant = ref(undefined);
const testEditCellTableRef = ref<InstanceType<typeof TestParamsCellTable>>();
const testResult = ref('');
const { getMultipleKeys } = useUtils();
const getToken = ref('');
const onSelect = async (e) => {
if (e) {
testDisabled.value = false;
} else {
testDisabled.value = true;
}
selectSysTenant.value = undefined;
const rest = (await getTenantAllPageLists(e)) as any;
selectTenantOptions.value = rest?.map((m) => ({ label: m.realName, value: m.id }));
};
const onSelctTenant = async (e) => {
if (e) {
testDisabled.value = false;
} else {
testDisabled.value = true;
}
const { token } = await getUserToken(e);
getToken.value = token;
};
const isSingleClickText = ref('open');
const handleTest = (o) => {
if (o === 'open') {
showTestEditCell.value = true;
emits('testParamsInterface');
getValue();
isSingleClickText.value = 'close';
} else {
isSingleClickText.value = 'open';
onCloseTest();
}
};
const getValue = async () => {
await nextTick();
await nextTick(() => {
//拆分"xx,xx,xx"多个
const getSingleKey = props.data?.list;
const getMuteKey = props.data?.list?.filter((it: any) => it.key?.split(',').length > 1);
const getMuteKeys = getMultipleKeys(getMuteKey);
const mergeKeys = [...getSingleKey, ...getMuteKeys]?.filter(
(it: any) => it.key?.split(',').length === 1
);
testEditCellTableRef.value?.setTableArray(mergeKeys);
});
};
//测试表格里的数据转化为 key value 数组对象形式
//TODO:待优化这里的TS类型
const getTestTableKeyValue = () => {
//把日期拆分成多个
const keyValueList: any = [];
testEditCellTableRef.value?.getValue()?.forEach((item: any) => {
const splitDateKey = item?.key === 'date_range' ? item?.value?.split(',') : [];
const splitDateValue = item?.key === 'date_range' ? item?.dateValue : [];
for (let i in splitDateKey) {
const obj = {
key: splitDateKey[i],
value: moment(splitDateValue[i]).valueOf(),
};
keyValueList.push(obj);
}
});
return testEditCellTableRef.value
?.getValue()
.concat(keyValueList)
.filter((it) => it.key !== 'date_range')
.map((it: any) => {
const value =
it?.key === 'scope'
? it?.keyValue
: it?.key === 'fixed_date'
? moment(it?.fixed_date_value).valueOf()
: it?.value;
const key = it?.key === 'scope' || it?.key === 'fixed_date' ? it?.value : it?.key;
return {
key,
value,
required: it.required,
};
});
};
//获取数据
const getTestValue = () => {
const getTable = getTestTableKeyValue();
const hasRequired = getTable?.some((it) => it.required === true && !it.value);
if (hasRequired) {
createMessage.error('参数不能为空');
throw new Error('参数不能为空');
}
const params: any = {};
getTable?.map((it) => (params[it.key!] = it.value!));
if (params['keys']) {
Reflect.set(params, 'keys', params['keys'].join(','));
}
excuteData.value = {
params,
token: getToken.value,
};
return excuteData.value;
};
//设置数据
const setValue = () => {
showTestEditCell.value = false;
testResult.value = '';
selectTenant.value = undefined;
selectSysTenant.value = undefined;
selectTenantOptions.value = [];
};
const onCloseTest = () => {
showTestEditCell.value = false;
emits('closeTest');
};
defineExpose({
setValue,
handleTest,
getTestValue,
});
</script>
<style scoped lang="less"></style>