2024-10-10 14:28:46 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="remote-select">
|
|
|
|
|
<div class="flex flex-row">
|
2026-03-17 13:54:02 +08:00
|
|
|
<a-select ref="selectRef" class="remote-select-input" show-search :filter-option="filterOption" :options="optionsRef" :value="value" v-bind="attrs" @click="onClick" @update:value="updateValue($event)">
|
2025-05-20 01:11:26 +08:00
|
|
|
<template #dropdownRender="{ menuNode: menu }">
|
|
|
|
|
<template v-if="search">
|
|
|
|
|
<div class="flex w-full" style="padding: 4px 8px">
|
|
|
|
|
<a-input ref="inputRef" v-model:value="searchKeyRef" class="flex-1" allow-clear placeholder="查询关键字" @keydown.enter="doSearch" />
|
|
|
|
|
<a-button class="ml-2" :loading="loading" type="text" @click="doSearch">
|
|
|
|
|
<template #icon>
|
|
|
|
|
<search-outlined />
|
|
|
|
|
</template>
|
|
|
|
|
查询
|
|
|
|
|
</a-button>
|
|
|
|
|
</div>
|
|
|
|
|
<div v-if="hasError" class="helper p-2" :class="{ error: hasError }">
|
|
|
|
|
{{ message }}
|
|
|
|
|
</div>
|
|
|
|
|
<a-divider style="margin: 4px 0" />
|
|
|
|
|
</template>
|
|
|
|
|
<v-nodes :vnodes="menu" />
|
2025-05-28 11:22:39 +08:00
|
|
|
|
2025-05-28 23:01:55 +08:00
|
|
|
<div v-if="pager === true" class="pager text-center p-5">
|
2025-06-29 19:59:13 +08:00
|
|
|
<a-pagination v-model:current="pagerRef.pageNo" simple :total="pagerRef.total" :page-size="pagerRef.pageSize" @change="onPageChange" />
|
2025-05-28 11:22:39 +08:00
|
|
|
</div>
|
2025-05-20 01:11:26 +08:00
|
|
|
</template>
|
|
|
|
|
</a-select>
|
2024-10-10 14:28:46 +08:00
|
|
|
<div class="ml-5">
|
2024-10-24 12:10:20 +08:00
|
|
|
<fs-button :loading="loading" title="刷新选项" icon="ion:refresh-outline" @click="refreshOptions"></fs-button>
|
2024-10-10 14:28:46 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-10-24 12:10:20 +08:00
|
|
|
<div class="helper" :class="{ error: hasError }">
|
2024-10-10 14:28:46 +08:00
|
|
|
{{ message }}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
2024-09-27 02:15:41 +08:00
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ComponentPropsType, doRequest } from "/@/components/plugins/lib";
|
2025-05-28 11:22:39 +08:00
|
|
|
import { defineComponent, inject, ref, useAttrs, watch, Ref } from "vue";
|
2024-10-25 16:51:36 +08:00
|
|
|
import { PluginDefine } from "@certd/pipeline";
|
2026-02-15 12:59:08 +08:00
|
|
|
import { getInputFromForm } from "./utils";
|
2024-09-27 02:15:41 +08:00
|
|
|
|
2024-10-07 03:21:16 +08:00
|
|
|
defineOptions({
|
2025-03-26 12:05:28 +08:00
|
|
|
name: "RemoteSelect",
|
2024-10-07 03:21:16 +08:00
|
|
|
});
|
|
|
|
|
|
2026-03-17 13:54:02 +08:00
|
|
|
const selectRef = ref(null);
|
|
|
|
|
|
2025-05-20 01:11:26 +08:00
|
|
|
const VNodes = defineComponent({
|
|
|
|
|
props: {
|
|
|
|
|
vnodes: {
|
|
|
|
|
type: Object,
|
|
|
|
|
required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
render() {
|
|
|
|
|
return this.vnodes;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-09-27 02:15:41 +08:00
|
|
|
const props = defineProps<
|
|
|
|
|
{
|
2026-02-09 19:19:26 +08:00
|
|
|
watches?: string[];
|
2025-05-28 11:22:39 +08:00
|
|
|
search?: boolean;
|
|
|
|
|
pager?: boolean;
|
2026-03-17 13:54:02 +08:00
|
|
|
multi?: boolean;
|
2024-09-27 02:15:41 +08:00
|
|
|
} & ComponentPropsType
|
|
|
|
|
>();
|
|
|
|
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
"update:value": any;
|
|
|
|
|
}>();
|
|
|
|
|
|
2026-03-12 00:46:05 +08:00
|
|
|
function updateValue(value: any) {
|
2026-03-17 13:54:02 +08:00
|
|
|
// if (props.multi !== false) {
|
|
|
|
|
// emit("update:value", value);
|
|
|
|
|
// } else {
|
|
|
|
|
// const last = value?.[value.length - 1];
|
|
|
|
|
// emit("update:value", last);
|
|
|
|
|
// selectRef.value.blur();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
emit("update:value", value);
|
2026-03-12 00:46:05 +08:00
|
|
|
}
|
|
|
|
|
|
2024-10-10 14:28:46 +08:00
|
|
|
const attrs = useAttrs();
|
|
|
|
|
|
2025-06-26 18:43:16 +08:00
|
|
|
const getCurrentPluginDefine: any = inject("getCurrentPluginDefine", () => {
|
|
|
|
|
return {};
|
|
|
|
|
});
|
|
|
|
|
const getScope: any = inject("get:scope", () => {
|
|
|
|
|
return {};
|
|
|
|
|
});
|
|
|
|
|
const getPluginType: any = inject("get:plugin:type", () => {
|
|
|
|
|
return "plugin";
|
|
|
|
|
});
|
2024-11-25 11:35:16 +08:00
|
|
|
|
2025-05-20 01:11:26 +08:00
|
|
|
const searchKeyRef = ref("");
|
2024-09-27 02:15:41 +08:00
|
|
|
const optionsRef = ref([]);
|
2024-10-03 01:29:12 +08:00
|
|
|
const message = ref("");
|
2024-10-24 12:10:20 +08:00
|
|
|
const hasError = ref(false);
|
|
|
|
|
const loading = ref(false);
|
2025-05-28 11:22:39 +08:00
|
|
|
const pagerRef: Ref = ref({
|
2026-01-22 18:33:39 +08:00
|
|
|
pageNo: 1,
|
|
|
|
|
total: 0,
|
|
|
|
|
pageSize: 100,
|
2025-05-28 11:22:39 +08:00
|
|
|
});
|
2024-09-27 02:15:41 +08:00
|
|
|
const getOptions = async () => {
|
2024-10-25 16:51:36 +08:00
|
|
|
if (loading.value) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!getCurrentPluginDefine) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const define: PluginDefine = getCurrentPluginDefine()?.value;
|
|
|
|
|
if (!define) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-29 18:40:13 +08:00
|
|
|
const pluginType = getPluginType();
|
|
|
|
|
const { form } = getScope();
|
2026-02-15 12:59:08 +08:00
|
|
|
const { input, record } = getInputFromForm(form, pluginType);
|
2025-04-29 18:40:13 +08:00
|
|
|
|
2024-10-25 16:51:36 +08:00
|
|
|
for (let key in define.input) {
|
2025-09-06 00:01:17 +08:00
|
|
|
const inWatches = props.watches?.includes(key);
|
2024-10-25 16:51:36 +08:00
|
|
|
const inputDefine = define.input[key];
|
|
|
|
|
if (inWatches && inputDefine.required) {
|
2025-04-29 18:40:13 +08:00
|
|
|
const value = input[key];
|
2024-10-25 16:51:36 +08:00
|
|
|
if (value == null || value === "") {
|
|
|
|
|
console.log("remote-select required", key);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 18:46:29 +08:00
|
|
|
message.value = "";
|
2024-10-24 12:10:20 +08:00
|
|
|
hasError.value = false;
|
|
|
|
|
loading.value = true;
|
2025-06-29 19:59:13 +08:00
|
|
|
const pageNo = pagerRef.value.pageNo;
|
|
|
|
|
const pageSize = pagerRef.value.pageSize;
|
2024-10-24 12:10:20 +08:00
|
|
|
try {
|
|
|
|
|
const res = await doRequest(
|
|
|
|
|
{
|
2024-11-25 11:35:16 +08:00
|
|
|
type: pluginType,
|
|
|
|
|
typeName: form.type,
|
2024-10-24 12:10:20 +08:00
|
|
|
action: props.action,
|
2025-04-29 18:40:13 +08:00
|
|
|
input,
|
2026-02-15 12:59:08 +08:00
|
|
|
record,
|
2025-05-20 01:11:26 +08:00
|
|
|
data: {
|
|
|
|
|
searchKey: props.search ? searchKeyRef.value : "",
|
2025-06-29 19:59:13 +08:00
|
|
|
pageNo,
|
|
|
|
|
pageSize,
|
2025-05-20 01:11:26 +08:00
|
|
|
},
|
2024-10-22 18:46:29 +08:00
|
|
|
},
|
2024-10-24 12:10:20 +08:00
|
|
|
{
|
|
|
|
|
onError(err: any) {
|
|
|
|
|
hasError.value = true;
|
|
|
|
|
message.value = `获取选项出错:${err.message}`;
|
2025-12-29 18:57:22 +08:00
|
|
|
optionsRef.value = [];
|
2024-10-24 12:10:20 +08:00
|
|
|
},
|
2025-03-26 12:05:28 +08:00
|
|
|
showErrorNotify: false,
|
2024-10-24 12:10:20 +08:00
|
|
|
}
|
|
|
|
|
);
|
2026-01-28 23:53:57 +08:00
|
|
|
let list = res?.list || res || [];
|
2025-05-28 11:22:39 +08:00
|
|
|
if (list.length > 0) {
|
2024-10-24 12:10:20 +08:00
|
|
|
message.value = "获取数据成功,请从下拉框中选择";
|
2025-08-07 18:30:47 +08:00
|
|
|
} else {
|
|
|
|
|
message.value = "获取数据成功,没有数据";
|
2024-10-03 01:29:12 +08:00
|
|
|
}
|
2026-01-28 23:53:57 +08:00
|
|
|
list = list.map((item: any) => {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
title: `${item.domain || item.value}`,
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-05-28 11:22:39 +08:00
|
|
|
optionsRef.value = list;
|
|
|
|
|
pagerRef.value.total = list.length;
|
|
|
|
|
if (props.pager) {
|
2025-06-29 19:59:13 +08:00
|
|
|
if (res.pageNo != null) {
|
|
|
|
|
pagerRef.value.pageNo = res.pageNo ?? 1;
|
2025-05-28 11:22:39 +08:00
|
|
|
}
|
2025-06-29 19:59:13 +08:00
|
|
|
if (res.pageSize != null) {
|
|
|
|
|
pagerRef.value.pageSize = res.pageSize ?? 100;
|
2025-05-28 11:22:39 +08:00
|
|
|
}
|
|
|
|
|
if (res.total != null) {
|
|
|
|
|
pagerRef.value.total = res.total ?? list.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-24 12:10:20 +08:00
|
|
|
return res;
|
|
|
|
|
} finally {
|
|
|
|
|
loading.value = false;
|
|
|
|
|
}
|
2024-09-27 02:15:41 +08:00
|
|
|
};
|
|
|
|
|
|
2024-09-30 18:00:51 +08:00
|
|
|
const filterOption = (input: string, option: any) => {
|
2025-09-05 22:19:03 +08:00
|
|
|
return option.label.toLowerCase().includes(input.toLowerCase()) || String(option.value).toLowerCase().includes(input.toLowerCase());
|
2024-09-30 18:00:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function onClick() {
|
2024-10-25 16:51:36 +08:00
|
|
|
if (optionsRef.value?.length === 0) {
|
|
|
|
|
await refreshOptions();
|
2024-09-30 18:00:51 +08:00
|
|
|
}
|
2024-10-10 14:28:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function refreshOptions() {
|
2024-10-25 16:51:36 +08:00
|
|
|
await getOptions();
|
2024-09-30 18:00:51 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 01:11:26 +08:00
|
|
|
async function doSearch() {
|
2025-06-29 19:59:13 +08:00
|
|
|
pagerRef.value.pageNo = 1;
|
2025-05-20 01:11:26 +08:00
|
|
|
await refreshOptions();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 02:15:41 +08:00
|
|
|
watch(
|
|
|
|
|
() => {
|
2025-04-29 18:40:13 +08:00
|
|
|
const pluginType = getPluginType();
|
2025-05-28 11:22:39 +08:00
|
|
|
const { form, key } = getScope();
|
2026-02-15 12:59:08 +08:00
|
|
|
const { input, record } = getInputFromForm(form, pluginType);
|
2026-02-09 19:19:26 +08:00
|
|
|
const watches: any = {};
|
|
|
|
|
if (props.watches && props.watches.length > 0) {
|
|
|
|
|
for (const key of props.watches) {
|
|
|
|
|
watches[key] = input[key];
|
|
|
|
|
}
|
2024-09-27 02:15:41 +08:00
|
|
|
}
|
2026-02-09 19:19:26 +08:00
|
|
|
|
2024-09-27 02:15:41 +08:00
|
|
|
return {
|
2025-05-28 11:22:39 +08:00
|
|
|
form: watches,
|
|
|
|
|
key,
|
2024-09-27 02:15:41 +08:00
|
|
|
};
|
|
|
|
|
},
|
2025-05-28 11:22:39 +08:00
|
|
|
async (value, oldValue) => {
|
|
|
|
|
const { form } = value;
|
2025-06-26 18:43:16 +08:00
|
|
|
const oldForm: any = oldValue?.form;
|
2025-05-28 11:22:39 +08:00
|
|
|
let changed = oldForm == null || optionsRef.value.length == 0;
|
2026-02-09 19:19:26 +08:00
|
|
|
if (props.watches && props.watches.length > 0) {
|
|
|
|
|
for (const key of props.watches) {
|
|
|
|
|
if (oldForm && form[key] != oldForm[key]) {
|
|
|
|
|
changed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-05-28 11:22:39 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (changed) {
|
|
|
|
|
await getOptions();
|
|
|
|
|
}
|
2024-10-25 16:51:36 +08:00
|
|
|
},
|
|
|
|
|
{
|
2025-03-26 12:05:28 +08:00
|
|
|
immediate: true,
|
2024-09-30 18:00:51 +08:00
|
|
|
}
|
2024-09-27 02:15:41 +08:00
|
|
|
);
|
2025-06-29 19:59:13 +08:00
|
|
|
|
|
|
|
|
async function onPageChange(current: any) {
|
|
|
|
|
await refreshOptions();
|
|
|
|
|
}
|
2024-09-27 02:15:41 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="less"></style>
|