DropdownDataEditor.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="dropdown-data-editor">
  3. <a-form layout="vertical" size="small">
  4. <a-form-item label="数据源类型">
  5. <a-select
  6. :value="currentType"
  7. style="width: 100%"
  8. :options="typeOptions"
  9. placeholder="选择类型"
  10. @change="onTypeChange"
  11. />
  12. </a-form-item>
  13. <template v-if="currentType === 'commonContent'">
  14. <a-form-item label="分类类型 ID (typeId)">
  15. <a-input-number
  16. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent)?.typeId"
  17. style="width: 100%"
  18. :min="1"
  19. placeholder="typeId"
  20. @update:value="(v: number | undefined) => setCommonContent('typeId', v)"
  21. />
  22. </a-form-item>
  23. <a-form-item label="ID 键 (idKey)">
  24. <a-input
  25. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent)?.idKey"
  26. placeholder="可选,默认 id"
  27. @change="(e: Event) => setCommonContent('idKey', (e.target as HTMLInputElement)?.value)"
  28. />
  29. </a-form-item>
  30. <a-form-item label="名称键 (nameKey)">
  31. <a-input
  32. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent)?.nameKey"
  33. placeholder="可选,默认 title"
  34. @change="(e: Event) => setCommonContent('nameKey', (e.target as HTMLInputElement)?.value)"
  35. />
  36. </a-form-item>
  37. <a-form-item label="其他参数 (otherParams)">
  38. <KeyValueEditor
  39. :model-value="(modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent)?.otherParams"
  40. @update:modelValue="setCommonContentOtherParams"
  41. />
  42. </a-form-item>
  43. </template>
  44. <template v-else-if="currentType === 'static'">
  45. <a-form-item label="静态数据 (data) JSON 数组">
  46. <a-input
  47. :value="staticDataJson"
  48. placeholder='[{"id": 1, "title": "选项1"}]'
  49. :rows="4"
  50. @change="(e: Event) => setStaticData((e.target as HTMLInputElement)?.value)"
  51. />
  52. </a-form-item>
  53. <a-form-item label="ID 键 (idKey)">
  54. <a-input
  55. :value="(modelValue as IHomeCommonCategoryDropdownStaticData)?.idKey"
  56. placeholder="可选"
  57. @change="(e: Event) => setStatic('idKey', (e.target as HTMLInputElement)?.value)"
  58. />
  59. </a-form-item>
  60. <a-form-item label="名称键 (nameKey)">
  61. <a-input
  62. :value="(modelValue as IHomeCommonCategoryDropdownStaticData)?.nameKey"
  63. placeholder="可选"
  64. @change="(e: Event) => setStatic('nameKey', (e.target as HTMLInputElement)?.value)"
  65. />
  66. </a-form-item>
  67. </template>
  68. <template v-else-if="currentType === 'request'">
  69. <a-form-item label="请求方法 (method)">
  70. <a-select
  71. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.method"
  72. style="width: 100%"
  73. :options="methodOptions"
  74. @change="(v: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE') => setRequest('method', v)"
  75. />
  76. </a-form-item>
  77. <a-form-item label="请求 URL (url)">
  78. <a-input
  79. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.url"
  80. placeholder="https://..."
  81. @change="(e: Event) => setRequest('url', (e.target as HTMLInputElement)?.value)"
  82. />
  83. </a-form-item>
  84. <a-form-item label="查询参数 (querys)">
  85. <KeyValueEditor
  86. :model-value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.querys"
  87. @update:modelValue="setRequestQuerys"
  88. />
  89. </a-form-item>
  90. <a-form-item label="请求体参数 (params)">
  91. <KeyValueEditor
  92. :model-value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.params"
  93. @update:modelValue="setRequestParams"
  94. />
  95. </a-form-item>
  96. <a-form-item label="ID 键 (idKey)">
  97. <a-input
  98. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.idKey"
  99. placeholder="可选"
  100. @change="(e: Event) => setRequest('idKey', (e.target as HTMLInputElement)?.value)"
  101. />
  102. </a-form-item>
  103. <a-form-item label="名称键 (nameKey)">
  104. <a-input
  105. :value="(modelValue as IHomeCommonCategoryDropdownDynamicDataRequest)?.nameKey"
  106. placeholder="可选"
  107. @change="(e: Event) => setRequest('nameKey', (e.target as HTMLInputElement)?.value)"
  108. />
  109. </a-form-item>
  110. </template>
  111. </a-form>
  112. </div>
  113. </template>
  114. <script setup lang="ts">
  115. import { computed } from 'vue';
  116. import KeyValueEditor from './KeyValueEditor.vue';
  117. import type {
  118. IHomeCommonCategoryDropdownDynamicData,
  119. IHomeCommonCategoryDropdownDynamicDataCommonContent,
  120. IHomeCommonCategoryDropdownDynamicDataRequest,
  121. IHomeCommonCategoryDropdownStaticData,
  122. } from '@/pages/article/data/data-defines/Dropdown';
  123. const props = defineProps<{
  124. modelValue?: IHomeCommonCategoryDropdownDynamicData | null;
  125. }>();
  126. const emit = defineEmits<{
  127. (e: 'update:modelValue', v: IHomeCommonCategoryDropdownDynamicData | undefined): void;
  128. }>();
  129. const typeOptions = [
  130. { value: 'commonContent', label: '通用内容 (commonContent)' },
  131. { value: 'static', label: '静态数据 (static)' },
  132. { value: 'request', label: '请求 (request)' },
  133. ];
  134. const methodOptions = [
  135. { value: 'GET', label: 'GET' },
  136. { value: 'POST', label: 'POST' },
  137. { value: 'PUT', label: 'PUT' },
  138. { value: 'DELETE', label: 'DELETE' },
  139. { value: 'HEAD', label: 'HEAD' },
  140. { value: 'OPTIONS', label: 'OPTIONS' },
  141. ];
  142. const currentType = computed(() => props.modelValue?.type ?? null);
  143. const staticDataJson = computed(() => {
  144. const cur = props.modelValue as IHomeCommonCategoryDropdownStaticData | undefined;
  145. if (!cur?.data) return '';
  146. try {
  147. return Array.isArray(cur.data) ? JSON.stringify(cur.data, null, 2) : '';
  148. } catch {
  149. return '';
  150. }
  151. });
  152. function onTypeChange(type: 'commonContent' | 'static' | 'request') {
  153. if (type === 'commonContent') {
  154. emit('update:modelValue', { type: 'commonContent', typeId: 1 });
  155. } else if (type === 'static') {
  156. emit('update:modelValue', { type: 'static', data: [] });
  157. } else if (type === 'request') {
  158. emit('update:modelValue', { type: 'request', method: 'GET', url: '' });
  159. }
  160. }
  161. function setCommonContent(key: 'typeId' | 'idKey' | 'nameKey', value: number | string | undefined) {
  162. const cur = props.modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent | undefined;
  163. if (!cur || cur.type !== 'commonContent') return;
  164. const next = { ...cur, [key]: value };
  165. emit('update:modelValue', next);
  166. }
  167. function setCommonContentOtherParams(otherParams: Record<string, any>) {
  168. const cur = props.modelValue as IHomeCommonCategoryDropdownDynamicDataCommonContent | undefined;
  169. if (!cur || cur.type !== 'commonContent') return;
  170. const value = otherParams && Object.keys(otherParams).length > 0 ? otherParams : undefined;
  171. emit('update:modelValue', { ...cur, otherParams: value });
  172. }
  173. function setStatic(key: 'idKey' | 'nameKey', value: string | undefined) {
  174. const cur = props.modelValue as IHomeCommonCategoryDropdownStaticData | undefined;
  175. if (!cur || cur.type !== 'static') return;
  176. emit('update:modelValue', { ...cur, [key]: value || undefined });
  177. }
  178. function setStaticData(str: string) {
  179. const cur = props.modelValue as IHomeCommonCategoryDropdownStaticData | undefined;
  180. if (!cur || cur.type !== 'static') return;
  181. let data: Record<string, any>[];
  182. try {
  183. const parsed = str?.trim() ? JSON.parse(str) : [];
  184. data = Array.isArray(parsed) ? parsed : [];
  185. } catch {
  186. return;
  187. }
  188. emit('update:modelValue', { ...cur, data });
  189. }
  190. function setRequest(
  191. key: 'method' | 'url' | 'idKey' | 'nameKey',
  192. value: string | undefined
  193. ) {
  194. const cur = props.modelValue as IHomeCommonCategoryDropdownDynamicDataRequest | undefined;
  195. if (!cur || cur.type !== 'request') return;
  196. const next = { ...cur, [key]: value } as IHomeCommonCategoryDropdownDynamicDataRequest;
  197. emit('update:modelValue', next);
  198. }
  199. function setRequestQuerys(querys: Record<string, any>) {
  200. const cur = props.modelValue as IHomeCommonCategoryDropdownDynamicDataRequest | undefined;
  201. if (!cur || cur.type !== 'request') return;
  202. const value = querys && Object.keys(querys).length > 0 ? querys : undefined;
  203. emit('update:modelValue', { ...cur, querys: value });
  204. }
  205. function setRequestParams(params: Record<string, any>) {
  206. const cur = props.modelValue as IHomeCommonCategoryDropdownDynamicDataRequest | undefined;
  207. if (!cur || cur.type !== 'request') return;
  208. const value = params && Object.keys(params).length > 0 ? params : undefined;
  209. emit('update:modelValue', { ...cur, params: value });
  210. }
  211. </script>
  212. <style scoped>
  213. .dropdown-data-editor {
  214. font-size: 12px;
  215. margin-left: 8px;
  216. padding: 8px;
  217. background: #fafafa;
  218. border-radius: 4px;
  219. }
  220. </style>