index.vue
7.86 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<template>
<div>
<div class="mt-8">
<div>
<Button @click="handleTest" type="primary"> 测试接口 </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">
<TestEditCellTable ref="testEditCellTableRef" :method="method" />
</a-col>
</a-row>
</div>
</div>
<div v-if="showTestEditCell" class="mt-8">
<div>
<Button @click="handleExcute" type="primary"> 执行 </Button>
</div>
<div class="mt-8">
<a-row type="flex" justify="center">
<a-col :span="3"> 测试结果 </a-col>
<a-col :span="21">
<a-textarea
allow-clear
show-count
v-model:value="testResult"
placeholder="测试结果为:"
:rows="8"
/>
</a-col>
</a-row>
</div>
</div>
</div>
</template>
<script lang="ts" setup name="testRequest">
import { ref, nextTick, reactive } from 'vue';
import { Button } from 'ant-design-vue';
import { TestEditCellTable } from '../TestEditCellTable/index';
import { otherHttp } from '/@/utils/http/axios';
import moment from 'moment';
import { useWebSocket } from '@vueuse/core';
import { JWT_TOKEN_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import { useMessage } from '/@/hooks/web/useMessage';
import { useGlobSetting } from '/@/hooks/setting';
const emits = defineEmits(['testInterface']);
const props = defineProps({
method: {
type: String,
},
data: {
type: Object,
},
});
const { createMessage } = useMessage();
const token = getAuthCache(JWT_TOKEN_KEY);
const { socketUrl } = useGlobSetting();
const socketMessage = reactive({
server: `${socketUrl}${token}`,
sendValue: {
tsSubCmds: [],
},
});
const showTestEditCell = ref(false);
const testEditCellTableRef = ref<InstanceType<typeof TestEditCellTable>>();
const testResult = ref('');
// const testNeedKeys = ['organizationId', 'deviceProfileId', 'entityId', 'keys', 'date'];
const handleTest = () => {
showTestEditCell.value = true;
emits('testInterface');
getValue();
};
//获取多个key
const getMultipleKeys = (list) => {
let temps = [];
list?.forEach((it) => {
const keys = it.key.split(',');
const temp = keys.map((item) => {
let obj: { key: string; value: string } = { key: '', value: '' };
obj.key = item;
obj.value = item === 'scope' ? it.value : '';
return obj;
});
temps = temp;
});
return temps;
};
//获取测试表格的key value 数组对象形式
const getTestTableKeyValue = () => {
//把日期拆分成多个
const keyValueList = [];
testEditCellTableRef.value?.getValue()?.forEach((item) => {
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) => {
const value = it.key === 'scope' || it.key === 'fixed_date' ? it.keyValue : it.value;
const key = it.key === 'scope' || it.key === 'fixed_date' ? it.value : it.key;
return {
key,
value,
};
});
};
//格式化"http:xxxx/api/xx/{xx}/{xx}/{xx}这种格式"
String.prototype.restfulFormat = function (replacements) {
var formatString = function (str, replacements) {
replacements =
typeof replacements === 'object' ? replacements : Array.prototype.slice.call(arguments, 1);
return str.replace(/\{\{|\}\}|\{(\w+)\}/g, function (m, n) {
if (m == '{{') {
return '{';
}
if (m == '}}') {
return '}';
}
return replacements[n];
});
};
replacements =
typeof replacements === 'object' ? replacements : Array.prototype.slice.call(arguments, 0);
return formatString(this, replacements);
};
//TODO 待优化 项目自带第三方请求
const otherHttpRequest = async (apiType, params = {}, api, joinPrefix = false) => {
switch (apiType) {
case 'GET':
Reflect.deleteProperty(params, 'deviceProfileId');
Reflect.deleteProperty(params, 'organizationId');
Reflect.deleteProperty(params, 'entityId');
Reflect.deleteProperty(params, 'entityType');
Reflect.deleteProperty(params, 'scope');
Reflect.deleteProperty(params, 'id');
Reflect.deleteProperty(params, 'type');
return await otherHttp.get(
{ url: api, params },
{
apiUrl: '',
joinPrefix,
}
);
case 'POST':
return await otherHttp.post(
{ url: api, data: params },
{
apiUrl: '',
joinPrefix,
}
);
case 'PUT':
return await otherHttp.put(
{ url: api, data: params },
{
apiUrl: '',
joinPrefix,
}
);
}
};
const getValue = async () => {
await nextTick();
await nextTick(() => {
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);
});
};
//执行测试接口
const handleExcute = async () => {
await nextTick();
await nextTick(async () => {
const getTable = getTestTableKeyValue();
const apiGetUrl = `${props.data?.requestOriginUrl}${props.data?.requestTypeAndUrl?.requestUrl}`;
const apiType = props.data?.requestTypeAndUrl?.requestHttpType;
const params: any = {};
getTable?.map((it) => (params[it.key!] = it.value!));
if (props.method === '0') {
//普通请求
const formatApi = apiGetUrl.restfulFormat(params);
const rest = await otherHttpRequest(apiType, params, formatApi.split('{?')[0], false);
testResult.value = JSON.stringify(rest);
} else if (props.method === '2') {
//websocket请求
Reflect.deleteProperty(params, 'deviceProfileId');
Reflect.deleteProperty(params, 'organizationId');
Reflect.set(params, 'cmdId', Number(params?.cmdId));
socketMessage.sendValue.tsSubCmds.push(params);
const { send, close } = useWebSocket(socketMessage.server, {
onConnected() {
send(JSON.stringify(socketMessage.sendValue));
console.log('建立连接了');
},
onMessage(_, e) {
const { data } = JSON.parse(e.data);
testResult.value = JSON.stringify(data);
},
onDisconnected() {
console.log('断开连接了');
close();
},
onError() {
createMessage.error('webSocket连接超时,请联系管理员');
},
});
}
});
};
//设置数据
const setValue = () => {
showTestEditCell.value = false;
testResult.value = '';
};
defineExpose({
setValue,
handleTest,
});
</script>
<style scoped lang="less"></style>