index.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { ref, getCurrentInstance, onMounted } from "vue";
  2. // prettier-ignore
  3. import { listConfig, getConfig, delConfig, addConfig, updateConfig, exportConfig, clearCache } from "@/api/system/config";
  4. import { ElForm, ElTable } from "element-plus";
  5. export default () => {
  6. const { proxy } = getCurrentInstance() as any;
  7. // 遮罩层
  8. const loading = ref<boolean>(true);
  9. // 选中数组
  10. const ids = ref<any>();
  11. // 非单个禁用
  12. const single = ref<boolean>(true);
  13. // 非多个禁用
  14. const multiple = ref<boolean>(true);
  15. // 显示搜索条件
  16. const showSearch = ref<boolean>(true);
  17. // 总条数
  18. const total = ref<number>(0);
  19. // 参数表格数据
  20. const configList = ref<any>();
  21. // 弹出层标题
  22. const title = ref<string>("");
  23. // 是否显示弹出层
  24. const open = ref<boolean>(false);
  25. // 类型数据字典
  26. const typeOptions = ref<any>();
  27. // 日期范围
  28. const dateRange = ref<any>();
  29. // 查询参数
  30. const queryParams = ref({
  31. pageNum: 1,
  32. pageSize: 10,
  33. configName: undefined,
  34. configKey: undefined,
  35. configType: undefined,
  36. });
  37. const queryFormRef = ref<InstanceType<typeof ElForm>>();
  38. // 表单参数
  39. const form = ref<any>();
  40. const formRef = ref<InstanceType<typeof ElForm>>();
  41. const pageTableRef = ref<InstanceType<typeof ElTable>>();
  42. // 表单校验
  43. const rules = ref({
  44. configName: [
  45. {
  46. required: true,
  47. message: "参数名称不能为空",
  48. trigger: "blur",
  49. },
  50. ],
  51. configKey: [
  52. {
  53. required: true,
  54. message: "参数键名不能为空",
  55. trigger: "blur",
  56. },
  57. ],
  58. configValue: [
  59. {
  60. required: true,
  61. message: "参数键值不能为空",
  62. trigger: "blur",
  63. },
  64. ],
  65. });
  66. /** 查询参数列表 */
  67. const getList = () => {
  68. loading.value = true;
  69. // prettier-ignore
  70. listConfig(proxy.addDateRange(queryParams.value, dateRange.value)).then((response: any) => {
  71. configList.value = response.rows;
  72. total.value = parseInt(response.total);
  73. loading.value = false;
  74. });
  75. };
  76. // 参数系统内置字典翻译
  77. const typeFormat = (row: any, column: any) => {
  78. return proxy.selectDictLabel(typeOptions.value, row.configType);
  79. };
  80. const cleanSelect = () => {
  81. pageTableRef.value?.clearSelection();
  82. };
  83. // 取消按钮
  84. const cancel = () => {
  85. open.value = false;
  86. reset();
  87. cleanSelect();
  88. };
  89. // 表单重置
  90. const reset = () => {
  91. form.value = {
  92. configId: undefined,
  93. configName: undefined,
  94. configKey: undefined,
  95. configValue: undefined,
  96. configType: "Y",
  97. remark: undefined,
  98. };
  99. proxy.resetForm(formRef);
  100. };
  101. /** 搜索按钮操作 */
  102. const handleQuery = () => {
  103. queryParams.value.pageNum = 1;
  104. getList();
  105. };
  106. /** 重置按钮操作 */
  107. const resetQuery = () => {
  108. dateRange.value = [];
  109. proxy.resetForm(queryFormRef);
  110. handleQuery();
  111. };
  112. /** 新增按钮操作 */
  113. const handleAdd = () => {
  114. reset();
  115. title.value = "添加参数";
  116. open.value = true;
  117. };
  118. // 多选框选中数据
  119. const handleSelectionChange = (selection: any) => {
  120. ids.value = selection.map((item: any) => item.configId);
  121. single.value = selection.length != 1;
  122. multiple.value = !selection.length;
  123. };
  124. /** 修改按钮操作 */
  125. const handleUpdate = (row: any) => {
  126. reset();
  127. const configId = row.configId || ids.value;
  128. getConfig(configId).then((response: any) => {
  129. form.value = response.data;
  130. open.value = true;
  131. title.value = "修改参数";
  132. proxy.setTableRowSelected(pageTableRef, row, true);
  133. });
  134. };
  135. /** 提交按钮 */
  136. const submitForm = async () => {
  137. await formRef.value?.validate((valid: boolean) => {
  138. if (valid) {
  139. if (form.value.configId != undefined) {
  140. updateConfig(form.value).then((response: any) => {
  141. if (response.code === 200) {
  142. proxy.$modal.msgSuccess("修改成功");
  143. open.value = false;
  144. getList();
  145. }
  146. });
  147. } else {
  148. addConfig(form.value).then((response: any) => {
  149. if (response.code === 200) {
  150. proxy.$modal.msgSuccess("新增成功");
  151. open.value = false;
  152. getList();
  153. }
  154. });
  155. }
  156. }
  157. });
  158. };
  159. /** 删除按钮操作 */
  160. const handleDelete = (row: any) => {
  161. const configIds = row.configId || ids.value;
  162. proxy.setTableRowSelected(pageTableRef, row, true);
  163. // prettier-ignore
  164. proxy.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项?', "警告")
  165. .then(() =>{
  166. return delConfig(configIds);
  167. })
  168. .then((response: any) => {
  169. if (response.code === 200) {
  170. getList();
  171. proxy.$modal.msgSuccess("删除成功");
  172. }
  173. })
  174. .catch(() => {
  175. cleanSelect();
  176. console.log("取消了删除");
  177. });
  178. };
  179. /** 导出按钮操作 */
  180. const handleExport = () => {
  181. // prettier-ignore
  182. proxy.download('/system/config/exportByStream', {...queryParams}, `参数配置${new Date().getTime()}.xlsx`);
  183. };
  184. /** 清理缓存按钮操作 */
  185. const handleClearCache = () => {
  186. clearCache().then((response: any) => {
  187. if (response.code === 200) {
  188. proxy.$modal.msgSuccess("清理成功");
  189. }
  190. });
  191. };
  192. onMounted(() => {
  193. getList();
  194. proxy.getDicts("sys_yes_no").then((response: any) => {
  195. if (response.code === 200) {
  196. typeOptions.value = response.data;
  197. }
  198. });
  199. });
  200. // prettier-ignore
  201. return {
  202. loading, single, multiple, open, showSearch, total, configList, title, typeOptions, dateRange, queryParams, queryFormRef, form, formRef, rules,
  203. getList, typeFormat, cancel, reset, handleQuery, resetQuery, handleAdd, handleSelectionChange, handleUpdate, submitForm, handleDelete,
  204. handleExport, handleClearCache, pageTableRef, cleanSelect,
  205. };
  206. };