Spaces:
Sleeping
Sleeping
File size: 6,244 Bytes
6e41657 | 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 | <script setup lang="ts">
import type { FormError } from '#ui/types';
import CodeSegment from '~/components/api/CodeSegment.vue';
import { apis } from '~/config/public-apis';
interface Props {
initialSelected: string;
}
const props = defineProps<Props>();
const isOpen = ref(false);
const selectedApi = ref(apis[0]);
const payload: Ref<Record<string, any>> = ref({});
const host = window.location.protocol + '//' + window.location.host;
function onOpen() {
isOpen.value = true;
selectedApi.value = apis.find(api => api.name === props.initialSelected)!;
}
function apiChange() {
payload.value = {};
}
function isEmpty(key: string, obj: Record<string, any>): boolean {
if (!obj.hasOwnProperty(key)) {
return true;
}
let value = obj[key];
if (typeof value === 'string') {
value = value.trim();
}
return value === undefined || value === null || value === '';
}
function validate(state: Record<string, any>): FormError[] {
const errors: FormError[] = [];
selectedApi.value.params.forEach(param => {
if (param.required && isEmpty(param.name, state)) {
errors.push({ path: param.name, message: param.name + '不能为空' });
}
if (!isEmpty(param.name, state) && param.type === 'Int') {
if (Number.isNaN(parseInt(state[param.name]))) {
errors.push({ path: param.name, message: param.name + '格式不正确' });
} else if (parseInt(state[param.name]) < 0) {
errors.push({ path: param.name, message: param.name + '不得小于0' });
}
}
});
return errors;
}
const resp = ref<Record<string, any> | null | string>(null);
const hasResponse = computed(() => {
return typeof resp.value === 'string' || (typeof resp.value === 'object' && resp.value);
});
const btnLoading = ref(false);
function submit() {
const params = toRaw(payload.value);
let url = selectedApi.value.url;
if (selectedApi.value.method === 'GET') {
url += '?' + new URLSearchParams(params).toString();
}
btnLoading.value = true;
resp.value = null;
fetch(url, {
method: selectedApi.value.method,
})
.then(resp => {
if (resp.headers.get('content-type') === 'application/json') {
return resp.json();
} else {
return resp.text();
}
})
.then(data => {
resp.value = data;
})
.finally(() => {
btnLoading.value = false;
});
}
</script>
<template>
<div>
<UTooltip text="在线调试" :popper="{ placement: 'top' }">
<UButton color="blue" variant="ghost" square @click="onOpen" icon="i-lucide:bug-play"></UButton>
</UTooltip>
<USlideover v-model="isOpen" :ui="{ width: 'max-w-[800px]' }">
<UCard
class="flex flex-col flex-1"
:ui="{
body: { base: 'overflow-y-scroll h-[calc(100vh-72px)]' },
ring: '',
divide: 'divide-y divide-gray-100 dark:divide-gray-800',
}"
>
<template #header>
<h1 class="font-semibold text-2xl">在线接口调试工具</h1>
</template>
<div class="space-y-5">
<div class="flex items-center gap-3">
<span>选择接口: </span>
<USelectMenu
class="flex-1"
v-model="selectedApi"
:options="apis"
option-attribute="name"
@change="apiChange"
>
<template #label>
<span
:class="[
selectedApi.method === 'GET' ? 'bg-green-400' : 'bg-fuchsia-400',
'inline-block size-2 flex-shrink-0 rounded-full',
]"
aria-hidden="true"
/>
<span class="font-medium">{{ selectedApi.name }}</span>
</template>
<template #option="{ option: api }">
<div>
<p class="font-medium">
<span
:class="[
api.method === 'GET' ? 'bg-green-400' : 'bg-fuchsia-400',
'inline-block size-2 mr-2 flex-shrink-0 rounded-full',
]"
aria-hidden="true"
/>
<span>{{ api.name }}</span>
</p>
<p class="text-gray-400 font-mono">{{ api.description }}</p>
</div>
</template>
</USelectMenu>
</div>
<div class="space-y-5">
<div>
<p class="font-semibold mb-2">请求URL:</p>
<p class="font-mono border p-2 rounded-md">
<span class="text-gray-400">{{ host }}</span>
<span class="font-semibold">{{ selectedApi.url }}</span>
</p>
</div>
<div>
<p class="font-semibold mb-2">请求方式:</p>
<p class="font-mono border p-2 rounded-md">{{ selectedApi.method }}</p>
</div>
<div>
<p class="font-semibold mb-2">参数:</p>
<UForm :state="payload" :validate="validate" @submit="submit" class="space-y-3">
<UFormGroup
v-for="p in selectedApi.params"
:key="p.name"
:label="p.name"
:name="p.name"
:required="p.required"
>
<UInput
v-model="payload[p.name]"
:type="p.type === 'Int' ? 'number' : 'text'"
:placeholder="p.label + (p.remark ? ',' + p.remark : '')"
/>
</UFormGroup>
<UButton type="submit" color="black" class="px-5" :loading="btnLoading">提交</UButton>
</UForm>
</div>
</div>
<div v-if="hasResponse">
<h3 class="font-bold text-2xl">响应</h3>
<CodeSegment :code="resp!" :lang="typeof resp === 'object' ? 'json' : 'xml'" />
</div>
</div>
</UCard>
</USlideover>
</div>
</template>
|