|
|
@ -7,10 +7,10 @@ |
|
|
</template> |
|
|
</template> |
|
|
|
|
|
|
|
|
<script lang="ts" setup> |
|
|
<script lang="ts" setup> |
|
|
import { type Recordable, type AnyFunction } from '@vben/types'; |
|
|
import { type Recordable } from '@vben/types'; |
|
|
import { type PropType, computed, watch, ref, onMounted, unref, useAttrs } from 'vue'; |
|
|
import { type PropType, computed, watch, ref, onMounted, unref, useAttrs } from 'vue'; |
|
|
import { Tree, TreeProps } from 'ant-design-vue'; |
|
|
import { Tree, TreeProps } from 'ant-design-vue'; |
|
|
import { isArray, isFunction } from '@/utils/is'; |
|
|
import { isFunction } from '@/utils/is'; |
|
|
import { get } from 'lodash-es'; |
|
|
import { get } from 'lodash-es'; |
|
|
import { DataNode } from 'ant-design-vue/es/tree'; |
|
|
import { DataNode } from 'ant-design-vue/es/tree'; |
|
|
import { useRuleFormItem } from '@/hooks/component/useFormItem'; |
|
|
import { useRuleFormItem } from '@/hooks/component/useFormItem'; |
|
|
@ -22,7 +22,14 @@ |
|
|
params: { type: Object }, |
|
|
params: { type: Object }, |
|
|
immediate: { type: Boolean, default: true }, |
|
|
immediate: { type: Boolean, default: true }, |
|
|
resultField: { type: String, default: '' }, |
|
|
resultField: { type: String, default: '' }, |
|
|
afterFetch: { type: Function as PropType<AnyFunction> }, |
|
|
beforeFetch: { |
|
|
|
|
|
type: Function as PropType<Fn>, |
|
|
|
|
|
default: null, |
|
|
|
|
|
}, |
|
|
|
|
|
afterFetch: { |
|
|
|
|
|
type: Function as PropType<Fn>, |
|
|
|
|
|
default: null, |
|
|
|
|
|
}, |
|
|
value: { |
|
|
value: { |
|
|
type: Array as PropType<TreeProps['selectedKeys']>, |
|
|
type: Array as PropType<TreeProps['selectedKeys']>, |
|
|
}, |
|
|
}, |
|
|
@ -72,25 +79,28 @@ |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
async function fetch() { |
|
|
async function fetch() { |
|
|
const { api, afterFetch } = props; |
|
|
let { api, beforeFetch, afterFetch, params, resultField } = props; |
|
|
if (!api || !isFunction(api)) return; |
|
|
if (!api || !isFunction(api)) return; |
|
|
loading.value = true; |
|
|
loading.value = true; |
|
|
treeData.value = []; |
|
|
treeData.value = []; |
|
|
let result; |
|
|
let res; |
|
|
try { |
|
|
try { |
|
|
result = await api(props.params); |
|
|
if (beforeFetch && isFunction(beforeFetch)) { |
|
|
|
|
|
params = (await beforeFetch(params)) || params; |
|
|
|
|
|
} |
|
|
|
|
|
res = await api(params); |
|
|
|
|
|
if (afterFetch && isFunction(afterFetch)) { |
|
|
|
|
|
res = (await afterFetch(res)) || res; |
|
|
|
|
|
} |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
console.error(e); |
|
|
console.error(e); |
|
|
} |
|
|
} |
|
|
if (afterFetch && isFunction(afterFetch)) { |
|
|
|
|
|
result = afterFetch(result); |
|
|
|
|
|
} |
|
|
|
|
|
loading.value = false; |
|
|
loading.value = false; |
|
|
if (!result) return; |
|
|
if (!res) return; |
|
|
if (!isArray(result)) { |
|
|
if (resultField) { |
|
|
result = get(result, props.resultField); |
|
|
res = get(res, resultField) || []; |
|
|
} |
|
|
} |
|
|
treeData.value = (result as (Recordable & { key: string | number })[]) || []; |
|
|
treeData.value = (res as (Recordable & { key: string | number })[]) || []; |
|
|
isFirstLoaded.value = true; |
|
|
isFirstLoaded.value = true; |
|
|
emit('options-change', treeData.value); |
|
|
emit('options-change', treeData.value); |
|
|
} |
|
|
} |
|
|
|