config-edit.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <script setup lang="ts">
  2. import { computed, reactive, ref } from "vue";
  3. import * as $configApi from "@/api/sys/config";
  4. import message from "@/utils/message";
  5. const formRef = ref();
  6. const pageData: any = reactive({
  7. mode: "add",
  8. modalParam: {
  9. visible: false,
  10. title: "新增系统配置",
  11. closeOnclickModal: true
  12. },
  13. formParam: {
  14. infoForm: {},
  15. fields: [
  16. {
  17. type: "select",
  18. label: "配置类型",
  19. prop: "type",
  20. placeholder: "请选择配置类型",
  21. dataSourceKey: "configTypeOptions",
  22. options: {
  23. clearable: true,
  24. filterable: true,
  25. keys: {
  26. prop: "value",
  27. value: "value",
  28. label: "label"
  29. }
  30. }
  31. },
  32. {
  33. type: "input",
  34. label: "配置项",
  35. prop: "code",
  36. placeholder: "请输入配置项",
  37. max: 50,
  38. options: {
  39. clearable: true
  40. }
  41. },
  42. {
  43. type: "textarea",
  44. label: "配置值",
  45. prop: "value",
  46. placeholder: "请输入配置值",
  47. max: 255,
  48. options: {
  49. clearable: true
  50. }
  51. },
  52. {
  53. type: "input",
  54. label: "备注",
  55. prop: "memo",
  56. placeholder: "请输入备注",
  57. max: 255,
  58. options: {
  59. clearable: true
  60. }
  61. }
  62. ],
  63. rules: {
  64. type: [{ required: true, message: "请选择配置类型", trigger: "change" }],
  65. code: [{ required: true, message: "请输入配置项", trigger: "blur" }],
  66. value: [{ required: true, message: "请输入配置值", trigger: "blur" }]
  67. },
  68. loading: false
  69. },
  70. dataSource: {}
  71. });
  72. const disabledField = computed(() => (item: any) => {
  73. if (pageData.mode === "add") {
  74. return false;
  75. }
  76. return item.prop === "code" || item.prop === "type";
  77. });
  78. const showBtn = computed(() => {
  79. if (pageData.mode === "detail") {
  80. return false;
  81. }
  82. return true;
  83. });
  84. const initData = () => {
  85. pageData.modalParam.visible = false;
  86. pageData.modalParam.title = "新增系统配置";
  87. pageData.modalParam.closeOnclickModal = true;
  88. pageData.formParam.infoForm = {};
  89. };
  90. const _handleClose = () => {
  91. initData();
  92. emits("close");
  93. };
  94. const open = (data: any, dataSource: any, mode: string) => {
  95. pageData.mode = mode;
  96. pageData.dataSource = dataSource;
  97. pageData.formParam.infoForm = data;
  98. pageData.modalParam.visible = true;
  99. };
  100. const handleConfirm = () => {
  101. formRef.value!.validate((isValid: boolean) => {
  102. if (isValid) {
  103. if (pageData.mode === "add") {
  104. save();
  105. } else {
  106. update();
  107. }
  108. }
  109. });
  110. };
  111. const save = () => {
  112. pageData.formParam.loading = true;
  113. const data = pageData.formParam.infoForm;
  114. const params = {
  115. type: data.type,
  116. code: data.code,
  117. value: data.value,
  118. memo: data.memo
  119. };
  120. $configApi
  121. .save(params)
  122. .then((res: any) => {
  123. if (res.success) {
  124. message.success("新增成功");
  125. initData();
  126. emits("confirm");
  127. pageData.modalParam.visible = false;
  128. } else {
  129. message.warning(res.message);
  130. }
  131. })
  132. .finally(() => {
  133. pageData.formParam.loading = false;
  134. });
  135. };
  136. const update = () => {
  137. pageData.formParam.loading = true;
  138. const data = pageData.formParam.infoForm;
  139. const params = {
  140. id: data.id,
  141. type: data.type,
  142. code: data.code,
  143. value: data.value,
  144. memo: data.memo
  145. };
  146. $configApi
  147. .update(data.id, params)
  148. .then((res: any) => {
  149. if (res.success) {
  150. message.success("修改成功");
  151. initData();
  152. emits("confirm");
  153. pageData.modalParam.visible = false;
  154. } else {
  155. message.warning(res.message);
  156. }
  157. })
  158. .finally(() => {
  159. pageData.formParam.loading = false;
  160. });
  161. };
  162. const emits = defineEmits(["close", "confirm"]);
  163. defineOptions({
  164. name: "ConfigEdit"
  165. });
  166. defineExpose({
  167. open
  168. });
  169. </script>
  170. <template>
  171. <div>
  172. <el-drawer
  173. v-model="pageData.modalParam.visible"
  174. :title="pageData.modalParam.title"
  175. destroy-on-close
  176. modal
  177. :close-on-click-modal="pageData.modalParam.closeOnclickModal"
  178. :before-close="_handleClose"
  179. >
  180. <template #header>
  181. <span style="font-weight: 700">{{ pageData.modalParam.title }}</span>
  182. </template>
  183. <el-form
  184. :model="pageData.formParam.infoForm"
  185. :rules="pageData.formParam.rules"
  186. label-position="right"
  187. ref="formRef"
  188. v-loading="pageData.formParam.loading"
  189. >
  190. <el-form-item
  191. v-for="(item, index) in pageData.formParam.fields"
  192. :key="index"
  193. :prop="item.prop"
  194. :label="item.label"
  195. >
  196. <template v-if="item.type === 'input'">
  197. <el-input
  198. v-model="pageData.formParam.infoForm[item.prop]"
  199. :clearable="item.options?.clearable ?? true"
  200. :max="item.max ?? 255"
  201. :disabled="disabledField(item)"
  202. />
  203. </template>
  204. <template v-else-if="item.type === 'textarea'">
  205. <el-input
  206. rows="25"
  207. v-model="pageData.formParam.infoForm[item.prop]"
  208. :clearable="item.options?.clearable ?? true"
  209. :max="item.max ?? 255"
  210. :disabled="disabledField(item)"
  211. type="textarea"
  212. />
  213. </template>
  214. <template v-else-if="item.type === 'select'">
  215. <el-select
  216. v-model="pageData.formParam.infoForm[item.prop]"
  217. :filterable="item.options?.filterable ?? true"
  218. :clearable="item.options?.clearable ?? true"
  219. :placeholder="item.placeholder"
  220. style="width: 100%"
  221. :disabled="disabledField(item)"
  222. >
  223. <el-option
  224. v-for="su in pageData.dataSource[item.dataSourceKey]"
  225. :key="su[item.options?.keys?.prop ?? 'value']"
  226. :label="su[item.options?.keys?.label ?? 'label']"
  227. :value="su[item.options?.keys?.value ?? 'value']"
  228. />
  229. </el-select>
  230. </template>
  231. </el-form-item>
  232. <!---->
  233. </el-form>
  234. <template #footer>
  235. <el-button plain @click="_handleClose">取 消</el-button>
  236. <el-popconfirm
  237. title="确认提交?"
  238. @confirm="handleConfirm"
  239. confirm-button-text="确定"
  240. cancel-button-text="取消"
  241. >
  242. <template #reference>
  243. <el-button
  244. type="primary"
  245. plain
  246. v-show="showBtn"
  247. v-loading="pageData.formParam.loading"
  248. >提 交</el-button
  249. >
  250. </template>
  251. </el-popconfirm>
  252. </template>
  253. <!---->
  254. </el-drawer>
  255. </div>
  256. </template>