dict-item.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <!-- 字典数据 -->
  2. <script setup lang="ts">
  3. import {
  4. getDictPage,
  5. getDictFormData,
  6. addDict,
  7. updateDict,
  8. deleteDict,
  9. } from "@/api/system/dict";
  10. import { DictPageVO, DictForm, DictQuery } from "@/api/system/dict/types";
  11. import ButtonPermKeys from "@/common/configs/buttonPermission";
  12. defineOptions({
  13. name: "DictData",
  14. inheritAttrs: false,
  15. });
  16. const props = defineProps({
  17. typeCode: {
  18. type: String,
  19. default: () => {
  20. return "";
  21. },
  22. },
  23. typeName: {
  24. type: String,
  25. default: () => {
  26. return "";
  27. },
  28. },
  29. });
  30. watch?.(
  31. () => props.typeCode,
  32. (newVal: string) => {
  33. queryParams.dictCode = newVal;
  34. formData.dictCode = newVal;
  35. resetQuery();
  36. }
  37. );
  38. const queryFormRef = ref(ElForm);
  39. const dataFormRef = ref(ElForm);
  40. const loading = ref(false);
  41. const ids = ref<string[]>([]);
  42. const total = ref(0);
  43. const queryParams = reactive<any>({
  44. pageNo: 1,
  45. pageSize: 10,
  46. dictCode: props.typeCode,
  47. });
  48. const dictList = ref<DictPageVO[]>();
  49. const dialog = reactive({
  50. title: "",
  51. visible: false,
  52. });
  53. const formData = reactive<any>({
  54. state: 0,
  55. dictCode: props.typeCode,
  56. dictSort: 1,
  57. });
  58. const rules = reactive({
  59. name: [{ required: true, message: "请输入字典名称", trigger: "blur" }],
  60. value: [{ required: true, message: "请输入字典值", trigger: "blur" }],
  61. });
  62. /**
  63. * 查询
  64. */
  65. function handleQuery() {
  66. if (queryParams.dictCode) {
  67. loading.value = true;
  68. getDictPage(queryParams)
  69. .then(({ data }) => {
  70. dictList.value = data.records;
  71. total.value = data.totalCount;
  72. })
  73. .finally(() => (loading.value = false));
  74. }
  75. }
  76. /**
  77. * 重置查询
  78. */
  79. function resetQuery() {
  80. queryParams.pageNo = 1;
  81. handleQuery();
  82. }
  83. /**
  84. * 行checkbox change事件
  85. *
  86. * @param selection
  87. */
  88. function handleSelectionChange(selection: any) {
  89. ids.value = selection.map((item: any) => item.id);
  90. }
  91. /**
  92. * 打开字典表单弹窗
  93. *
  94. * @param dictId 字典ID
  95. */
  96. function openDialog(form?: any) {
  97. dialog.visible = true;
  98. if (form) {
  99. dialog.title = "修改字典";
  100. Object.assign(formData, form);
  101. } else {
  102. dialog.title = "新增字典";
  103. dataFormRef.value.label = "";
  104. dataFormRef.value = "";
  105. formData.dictLabel = "";
  106. formData.dictValue = ""
  107. }
  108. }
  109. /**
  110. * 字典表单提交
  111. */
  112. function handleSubmit() {
  113. dataFormRef.value.validate((isValid: boolean) => {
  114. if (isValid) {
  115. const dictId = formData.id;
  116. if (dictId) {
  117. updateDict(formData)
  118. .then(() => {
  119. ElMessage.success("修改成功");
  120. closeDialog();
  121. resetQuery();
  122. })
  123. .finally(() => (loading.value = false));
  124. } else {
  125. addDict(formData)
  126. .then(() => {
  127. ElMessage.success("新增成功");
  128. closeDialog();
  129. resetQuery();
  130. })
  131. .finally(() => (loading.value = false));
  132. }
  133. }
  134. });
  135. }
  136. /**
  137. * 关闭弹窗
  138. */
  139. function closeDialog() {
  140. dialog.visible = false;
  141. resetForm();
  142. }
  143. /**
  144. * 重置表单
  145. */
  146. function resetForm() {
  147. dataFormRef.value.resetFields();
  148. dataFormRef.value.clearValidate();
  149. formData.id = undefined;
  150. formData.state = 0;
  151. formData.dictSort = 1;
  152. formData.dictCode = props.typeCode;
  153. }
  154. /**
  155. * 删除字典
  156. */
  157. function handleDelete(dictId?: string) {
  158. if (dictId) {
  159. ids.value.push(dictId);
  160. }
  161. if (!ids.value) {
  162. ElMessage.warning("请勾选删除项");
  163. return;
  164. }
  165. ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
  166. confirmButtonText: "确定",
  167. cancelButtonText: "取消",
  168. type: "warning",
  169. }).then(() => {
  170. deleteDict(ids.value).then(() => {
  171. ElMessage.success("删除成功");
  172. resetQuery();
  173. });
  174. });
  175. }
  176. onMounted?.(() => {
  177. handleQuery();
  178. });
  179. </script>
  180. <template>
  181. <div class="app-container">
  182. <!-- <div class="search-container">
  183. &lt;!&ndash; 搜索表单 &ndash;&gt;
  184. <el-form ref="queryFormRef" :model="queryParams" :inline="true">
  185. <el-form-item label="关键字" prop="name">
  186. <el-input
  187. v-model="queryParams.name"
  188. placeholder="字典名称"
  189. clearable
  190. />
  191. </el-form-item>
  192. <el-form-item>
  193. <el-button type="primary" @click="handleQuery"
  194. ><i-ep-search />搜索</el-button
  195. >
  196. <el-button @click="resetQuery"> <i-ep-refresh />重置</el-button>
  197. </el-form-item>
  198. </el-form>
  199. </div>-->
  200. <el-card shadow="never">
  201. <template #header>
  202. <el-button
  203. v-hasPerm="[ButtonPermKeys.SYSTEM.BTNS.dict_add]"
  204. type="primary"
  205. @click="openDialog()"
  206. ><i-ep-plus />新增</el-button
  207. >
  208. <!-- <el-button
  209. v-hasPerm="[ButtonPermKeys.SYSTEM.BTNS.dict_del]"
  210. type="danger"
  211. :disabled="ids.length === 0"
  212. @click="handleDelete()"
  213. ><i-ep-delete />删除</el-button
  214. >-->
  215. </template>
  216. <!-- 数据表格 -->
  217. <el-table
  218. v-loading="loading"
  219. :data="dictList"
  220. border
  221. @selection-change="handleSelectionChange"
  222. >
  223. <el-table-column type="selection" width="50" />
  224. <el-table-column label="字典名称" prop="dictLabel" />
  225. <el-table-column label="字典值" prop="dictValue" />
  226. <el-table-column label="排序" prop="dictSort" />
  227. <el-table-column label="状态" align="center">
  228. <template #default="scope">
  229. <el-tag v-if="scope.row.state === 0" type="success">启用</el-tag>
  230. <el-tag v-else type="info">禁用</el-tag>
  231. </template>
  232. </el-table-column>
  233. <el-table-column fixed="right" label="操作" align="center">
  234. <template #default="scope">
  235. <el-button
  236. v-hasPerm="[ButtonPermKeys.SYSTEM.BTNS.dict_edit]"
  237. type="primary"
  238. link
  239. @click="openDialog(scope.row)"
  240. ><i-ep-edit />编辑</el-button
  241. >
  242. <!-- <el-button
  243. v-hasPerm="[ButtonPermKeys.SYSTEM.BTNS.dict_del]"
  244. type="primary"
  245. link
  246. @click.stop="handleDelete(scope.row.id)"
  247. ><i-ep-delete />删除</el-button
  248. >-->
  249. </template>
  250. </el-table-column>
  251. </el-table>
  252. <pagination
  253. v-if="total > 0"
  254. v-model:total="total"
  255. v-model:page="queryParams.pageNo"
  256. v-model:limit="queryParams.pageSize"
  257. @pagination="handleQuery"
  258. />
  259. </el-card>
  260. <!-- 表单弹窗 -->
  261. <el-dialog
  262. v-model="dialog.visible"
  263. :title="dialog.title"
  264. width="500px"
  265. @close="closeDialog"
  266. >
  267. <el-form
  268. ref="dataFormRef"
  269. :model="formData"
  270. :rules="rules"
  271. label-width="100px"
  272. >
  273. <el-form-item label="字典名称" prop="dictLabel">
  274. <el-input v-model="formData.dictLabel" placeholder="请输入字典名称" />
  275. </el-form-item>
  276. <el-form-item label="字典值" prop="dictValue">
  277. <el-input
  278. v-if="formData.id"
  279. disabled
  280. v-model="formData.dictValue"
  281. placeholder="字典值"
  282. />
  283. <el-input
  284. v-if="!formData.id"
  285. v-model="formData.dictValue"
  286. placeholder="字典值"
  287. />
  288. </el-form-item>
  289. <el-form-item label="排序" prop="dictSort">
  290. <el-input-number
  291. v-model="formData.dictSort"
  292. controls-position="right"
  293. :min="0"
  294. />
  295. </el-form-item>
  296. <el-form-item label="状态" prop="status">
  297. <el-radio-group v-model="formData.state">
  298. <el-radio :value="0">正常</el-radio>
  299. <el-radio :value="1">停用</el-radio>
  300. </el-radio-group>
  301. </el-form-item>
  302. <el-form-item label="备注" prop="remark">
  303. <el-input v-model="formData.remark" type="textarea" />
  304. </el-form-item>
  305. </el-form>
  306. <template #footer>
  307. <div class="dialog-footer">
  308. <el-button type="primary" @click="handleSubmit">确 定</el-button>
  309. <el-button @click="closeDialog">取 消</el-button>
  310. </div>
  311. </template>
  312. </el-dialog>
  313. </div>
  314. </template>