AddDict.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <el-drawer :model-value="true" title="新建字典" @close="handleClose" :size="600">
  3. <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" class="add-dict-form">
  4. <el-form-item label="字典名称" prop="dictName">
  5. <el-input v-model="formData.dictName" />
  6. </el-form-item>
  7. <el-form-item label="字典编码" prop="dictCode">
  8. <el-input v-model="formData.dictCode" />
  9. </el-form-item>
  10. <el-form-item label="字典分类" prop="dictType">
  11. <el-select v-model="formData.dictType" placeholder="请选择">
  12. <el-option v-for="(item, index) in dictionaryTypeOptions" :key="index" :label="item.label"
  13. :value="item.value" />
  14. </el-select>
  15. </el-form-item>
  16. <div class="subDictWrapper">
  17. <div class="el-form-item__label" style="width: 100px">字典项</div>
  18. <div class="subDictList">
  19. <div>
  20. <div v-for="(item, index) in formData.sysDictDataList" :key="index" class="dict-item-group">
  21. <!-- 移动按钮 -->
  22. <div class="dict-item-header">
  23. <div class="move-buttons">
  24. <el-button v-if="index > 0" type="primary" link @click="moveDictItem(index, 'up')">
  25. <el-icon>
  26. <Top />
  27. </el-icon>
  28. </el-button>
  29. <el-button v-if="index < formData.sysDictDataList.length - 1" type="primary" link
  30. @click="moveDictItem(index, 'down')">
  31. <el-icon>
  32. <Bottom />
  33. </el-icon>
  34. </el-button>
  35. <el-button v-if="formData.sysDictDataList.length > 1" type="danger" link
  36. @click="removeDictItem(index)">
  37. <el-icon>
  38. <Delete />
  39. </el-icon>删除
  40. </el-button>
  41. </div>
  42. </div>
  43. <el-form-item :label="`字典项值`" :prop="`sysDictDataList.${index}.itemValue`"
  44. :rules="[{ required: true, message: '请输入字典项值', trigger: 'blur' }]">
  45. <el-input v-model="item.itemValue" placeholder="请输入" />
  46. </el-form-item>
  47. <el-form-item :label="`字典项编码`" :prop="`sysDictDataList.${index}.itemCode`"
  48. :rules="[{ required: true, message: '请输入字典项编码', trigger: 'blur' }]">
  49. <el-input v-model="item.itemCode" placeholder="请输入" />
  50. </el-form-item>
  51. <el-form-item label="图标">
  52. <el-upload
  53. v-model:file-list="item.fileList"
  54. :auto-upload="false"
  55. :action="actionUrl"
  56. list-type="picture-card"
  57. :limit="1"
  58. :on-preview="(file) => handlePictureCardPreview(file, index)" :on-remove="() => handleRemove(index)"
  59. :on-change="(file, fileList) => handleChange(file, fileList, index)"
  60. >
  61. <el-icon>
  62. <Plus />
  63. </el-icon>
  64. </el-upload>
  65. </el-form-item>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <el-form-item>
  71. <el-button type="primary" link @click="addDictItem">
  72. <el-icon>
  73. <CirclePlus />
  74. </el-icon> 新增字典分类
  75. </el-button>
  76. </el-form-item>
  77. <el-form-item label="字典描述" prop="description">
  78. <el-input v-model="formData.description" type="textarea" :rows="3" placeholder="请描述灾害处置过程, 不超过1000字"
  79. maxlength="1000" show-word-limit />
  80. </el-form-item>
  81. <el-form-item label="状态" prop="status">
  82. <el-radio-group v-model="formData.status">
  83. <el-radio-button v-for="item in dictionaryStatusOptions" :key="item.value" :value="item.value">{{ item.label
  84. }}</el-radio-button>
  85. </el-radio-group>
  86. </el-form-item>
  87. <el-form-item class="form-footer">
  88. <el-button @click="handleCancel">取消</el-button>
  89. <el-button type="primary" @click="handleSubmit">提交</el-button>
  90. </el-form-item>
  91. </el-form>
  92. </el-drawer>
  93. <el-dialog v-model="dialogVisible">
  94. <div class="dialog-content">
  95. <img :src="dialogImageUrl" alt="Preview Image" />
  96. </div>
  97. </el-dialog>
  98. </template>
  99. <script lang="ts" setup>
  100. import { ref, reactive, PropType, watch, computed } from 'vue';
  101. import {
  102. ElForm,
  103. ElFormItem,
  104. ElInput,
  105. ElSelect,
  106. ElOption,
  107. ElButton,
  108. ElRadioGroup,
  109. ElRadioButton,
  110. ElInputNumber,
  111. ElUpload,
  112. ElIcon,
  113. ElDialog,
  114. ElDivider,
  115. FormInstance,
  116. FormRules,
  117. UploadProps,
  118. UploadUserFile,
  119. } from 'element-plus';
  120. import { Plus, Delete, CirclePlus, Minus, Picture, Top, Bottom } from '@element-plus/icons-vue';
  121. import { dictionaryTypeOptions, DictionaryStatus, dictionaryStatusOptions } from '../constants';
  122. import { queryDictTypeDetail, uploadDictImage } from '@/api/dict'
  123. import { getHeaders } from '@/utils/http/axios';
  124. import { useGlobSetting } from '@/hooks/setting';
  125. import urlJoin from 'url-join';
  126. interface SysDictDataItem {
  127. id?: string | number; // 可选,用于编辑时
  128. dictId: string | number | undefined; // 关联的字典ID
  129. dictCode: string;
  130. itemValue: string;
  131. itemCode: string;
  132. itemSort: number | undefined;
  133. isDefault: 0 | 1,
  134. imageUrl?: string; // 用于显示已上传的图片
  135. status: DictionaryStatus.disabled | DictionaryStatus.enabled;
  136. }
  137. interface FormData {
  138. dictId?: string | number; // 可选,用于编辑时
  139. dictName: string;
  140. dictCode: string;
  141. dictType: string;
  142. sysDictDataList: SysDictDataItem[];
  143. description: string;
  144. status: DictionaryStatus.disabled | DictionaryStatus.enabled;
  145. }
  146. const props = defineProps({
  147. dictCode: {
  148. type: String,
  149. default: '',
  150. }
  151. });
  152. const { urlPrefix } = useGlobSetting();
  153. const actionUrl = computed(() => {
  154. return urlJoin(urlPrefix!, `/admin/minio/uploadFile`);
  155. });
  156. const emit = defineEmits(['submit', 'close']);
  157. const handleClose = () => {
  158. emit('close');
  159. };
  160. const formRef = ref<FormInstance>();
  161. const formData = reactive<FormData>({
  162. dictName: '',
  163. dictCode: '',
  164. dictType: '',
  165. sysDictDataList: [
  166. {
  167. id: undefined,
  168. dictId: undefined,
  169. dictCode: '',
  170. itemCode: '',
  171. itemValue: '',
  172. itemSort: 1, // 默认排序值
  173. isDefault: 0,
  174. status: DictionaryStatus.disabled,
  175. imageUrl: '',
  176. },
  177. ],
  178. description: '',
  179. status: DictionaryStatus.disabled,
  180. });
  181. watch(() => props.dictCode, async (newCode) => {
  182. if (!newCode) return;
  183. const res = await queryDictTypeDetail(newCode);
  184. // 处理字典项数据转换
  185. const processDictItem = (item: any) => ({
  186. ...item,
  187. fileList: item.imageUrl ? [{
  188. name: item.imageUrl.split('/').pop() || '',
  189. url: item.imageUrl
  190. }] : []
  191. });
  192. Object.assign(formData, {
  193. ...res,
  194. sysDictDataList: res.sysDictDataList?.map(processDictItem) ?? []
  195. });
  196. }, { immediate: true });
  197. const formRules = reactive<FormRules<FormData>>({
  198. dictName: [{ required: true, message: '请选择字典名称', trigger: 'change' }],
  199. dictCode: [{ required: true, message: '请选择字典编码', trigger: 'change' }],
  200. dictType: [{ required: true, message: '请选择字典分类', trigger: 'change' }],
  201. description: [{ max: 1000, message: '描述不超过1000字', trigger: 'blur' }],
  202. status: [{ required: true, message: '请选择状态', trigger: 'change' }],
  203. });
  204. const addDictItem = () => {
  205. formData.sysDictDataList.push({
  206. id: undefined,
  207. dictId: formData.dictId,
  208. dictCode: formData.dictCode,
  209. itemCode: '',
  210. itemValue: '',
  211. itemSort: 1, // 默认排序值
  212. isDefault: 0,
  213. status: DictionaryStatus.disabled,
  214. imageUrl: '',
  215. },
  216. );
  217. };
  218. const removeDictItem = (index: number) => {
  219. formData.sysDictDataList.splice(index, 1);
  220. };
  221. // 图片上传相关
  222. const dialogImageUrl = ref('');
  223. const dialogVisible = ref(false);
  224. const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  225. dialogImageUrl.value = uploadFile.url!;
  226. dialogVisible.value = true;
  227. };
  228. const handleRemove = (itemIndex: number) => {
  229. if (formData.sysDictDataList[itemIndex]) {
  230. formData.sysDictDataList[itemIndex].imageUrl = ''; // 如果有单独的 URL 字段
  231. }
  232. };
  233. const handleChange = (uploadFile: UploadUserFile, uploadFiles: UploadUserFile[], itemIndex: number) => {
  234. // Element Plus 的 onChange 会在文件状态改变时触发多次,通常在 ready 状态时文件已可选
  235. // 这里简单地将文件对象存起来,实际上传应在 handleSubmit 中处理
  236. if (formData.sysDictDataList[itemIndex]) {
  237. if (!uploadFile.raw) return;
  238. uploadDictImage(uploadFile.raw, 'DICTIONARY').then((res) => {
  239. formData.sysDictDataList[itemIndex].imageUrl = res.url;
  240. })
  241. }
  242. };
  243. const handleSubmit = async () => {
  244. if (!formRef.value) return;
  245. await formRef.value.validate((valid: boolean ) => {
  246. if (valid) {
  247. formData.sysDictDataList.forEach(item => {
  248. item.dictCode = formData.dictCode;
  249. if(item.fileList && item.fileList.length > 0) {
  250. delete item.fileList;
  251. }
  252. });
  253. emit('submit', JSON.parse(JSON.stringify(formData))); // 暂时发送原始数据,上传需单独处理
  254. } else {
  255. return false;
  256. }
  257. });
  258. };
  259. // 移动方法
  260. const moveDictItem = (index: number, direction: 'up' | 'down') => {
  261. const items = formData.sysDictDataList;
  262. if (direction === 'up' && index > 0) {
  263. // 交换位置
  264. [items[index - 1], items[index]] = [items[index], items[index - 1]];
  265. } else if (direction === 'down' && index < items.length - 1) {
  266. // 交换位置
  267. [items[index + 1], items[index]] = [items[index], items[index + 1]];
  268. }
  269. // 根据最新位置重新设置排序值
  270. items.forEach((item, idx) => {
  271. item.itemSort = idx + 1;
  272. });
  273. };
  274. const handleCancel = () => {
  275. emit('close');
  276. };
  277. // 暴露方法给父组件,例如重置表单
  278. defineExpose({
  279. resetForm: () => {
  280. formRef.value?.resetFields();
  281. formData.sysDictDataList = [
  282. {
  283. id: undefined,
  284. dictId: undefined,
  285. dictCode: '',
  286. itemCode: '',
  287. itemValue: '',
  288. itemSort: 1, // 默认排序值
  289. isDefault: 0,
  290. status: DictionaryStatus.disabled,
  291. imageUrl: '',
  292. },
  293. ];
  294. },
  295. getFormData: () => formData, // 允许父组件获取当前表单数据
  296. });
  297. </script>
  298. <style lang="scss" scoped>
  299. .add-dict-form {
  300. .el-select,
  301. .el-input-number {
  302. width: 100%;
  303. }
  304. }
  305. .dict-item-group {
  306. padding: 15px;
  307. border: 1px solid #dcdfe6;
  308. border-radius: 4px;
  309. margin-bottom: 20px;
  310. position: relative;
  311. .dict-item-header {
  312. display: flex;
  313. justify-content: flex-end;
  314. align-items: center;
  315. margin-bottom: 10px;
  316. h4 {
  317. margin: 0;
  318. font-size: 16px;
  319. }
  320. }
  321. }
  322. .form-footer {
  323. text-align: right;
  324. margin-top: 20px;
  325. }
  326. // 覆盖 el-upload 的样式,使其适应小图标场景
  327. :deep(.el-upload--picture-card) {
  328. width: 100px;
  329. height: 100px;
  330. line-height: 110px;
  331. }
  332. :deep(.el-upload-list--picture-card .el-upload-list__item) {
  333. width: 100px;
  334. height: 100px;
  335. }
  336. .subDictWrapper {
  337. display: flex;
  338. .dictItemsLabel {
  339. width: 30%;
  340. font-size: 14px;
  341. color: #606266;
  342. text-align: right;
  343. margin-right: 26px;
  344. }
  345. .subDictList {
  346. flex: 1;
  347. }
  348. }
  349. .dict-item-header {
  350. display: flex;
  351. justify-content: flex-end;
  352. align-items: center;
  353. margin-bottom: 10px;
  354. .move-buttons {
  355. margin-right: auto;
  356. .el-button {
  357. padding: 5px;
  358. margin-right: 8px;
  359. }
  360. }
  361. }
  362. .dialog-content {
  363. width: 100%;
  364. height: 100%;
  365. img {
  366. width: 100%;
  367. height: 100%;
  368. }
  369. }
  370. </style>