index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <CollapseContainer
  3. class="sys-container"
  4. title="系统配置"
  5. :canExpan="false"
  6. helpMessage="可以在此增改系统的变量和分组,也可以自定义分组和变量"
  7. >
  8. <a-button
  9. v-for="(group, index) in groupList"
  10. :key="index"
  11. type="default"
  12. class="mr-2"
  13. @click="handleGroupBtn(group)"
  14. >
  15. {{ group }}
  16. </a-button>
  17. <a-button
  18. preIcon="bx:bx-plus-medical"
  19. @mouseenter="showTip"
  20. @mouseleave="hideTip"
  21. @click="addConfig"
  22. class="mr-2"
  23. />
  24. <span v-if="tipShow" class="tip">点击添加新的配置</span>
  25. <BasicTable ref="tableRef" :canResize="true" v-if="tableShow" @register="registerTable">
  26. <template #action="{ record, column }">
  27. <TableAction :actions="createActions(record, column)" stopButtonPropagation />
  28. </template>
  29. </BasicTable>
  30. <BasicForm
  31. class="config-form"
  32. v-if="formShow"
  33. @register="registerForm"
  34. @submit="handleFormSubmit"
  35. />
  36. <div v-if="tableShow" class="actions">
  37. <a-button class="mr-2" type="default" @click="handleTableReset">
  38. {{ t('common.resetText') }}
  39. </a-button>
  40. <a-button class="mr-2" type="primary" @click="handleTableSubmit">
  41. {{ t('common.submitText') }}
  42. </a-button>
  43. </div>
  44. </CollapseContainer>
  45. </template>
  46. <script lang="ts">
  47. import { useMessage } from '/@/hooks/web/useMessage';
  48. import { defineComponent, nextTick, reactive, ref, toRefs, unref } from 'vue';
  49. import { CollapseContainer } from '/@/components/Container/index';
  50. import { BasicForm, useForm } from '/@/components/Form/index';
  51. import { schemas } from './data';
  52. import { useI18n } from '/@/hooks/web/useI18n';
  53. import { adapt } from '/@/utils/adapt';
  54. import { validateType } from '/@/utils/validTools';
  55. import {
  56. BasicTable,
  57. useTable,
  58. TableAction,
  59. // BasicColumn,
  60. ActionItem,
  61. EditRecordRow,
  62. TableActionType,
  63. } from '/@/components/Table';
  64. import {
  65. getConfigGroup,
  66. getConfigInfo,
  67. addConfigInfo,
  68. editConfigInfo,
  69. deleteConfigInfo,
  70. } from '/@/api/sys/general';
  71. import { columns } from './data';
  72. export default defineComponent({
  73. name: 'Config',
  74. components: { CollapseContainer, BasicTable, BasicForm, TableAction },
  75. setup() {
  76. const { t } = useI18n();
  77. const { createMessage } = useMessage();
  78. const { success /*, error */ } = createMessage;
  79. const tableHeight = adapt().tableHeight;
  80. const state = reactive({
  81. tipShow: false,
  82. tableShow: true,
  83. formShow: false,
  84. groupList: [] as object[],
  85. group: 'basic',
  86. });
  87. const tableRef = ref<Nullable<TableActionType>>(null);
  88. const [registerTable] = useTable({
  89. columns: columns,
  90. maxHeight: tableHeight,
  91. api: getConfigInfo,
  92. afterFetch: afterFetch,
  93. actionColumn: {
  94. width: 160,
  95. // title: '操作',
  96. dataIndex: 'action',
  97. slots: { customRender: 'action' },
  98. fixed: undefined,
  99. },
  100. showIndexColumn: false,
  101. pagination: false,
  102. });
  103. const [
  104. registerForm,
  105. // { validateFields, clearValidate, getFieldsValue, resetFields, setFieldsValue },
  106. ] = useForm({
  107. labelWidth: 120,
  108. schemas,
  109. actionColOptions: {
  110. span: 24,
  111. },
  112. showActionButtonGroup: true,
  113. });
  114. function getTableAction() {
  115. // 获取组件
  116. const tableAction = unref(tableRef);
  117. if (!tableAction) {
  118. throw new Error('tableAction is null');
  119. }
  120. return tableAction;
  121. }
  122. // 处理请求数据
  123. function afterFetch(result) {
  124. result = result[state.group].list;
  125. return result;
  126. }
  127. getGroupList();
  128. async function getGroupList() {
  129. const res = await getConfigGroup();
  130. state.groupList = Object.values(res.group);
  131. }
  132. function showTip() {
  133. state.tipShow = true;
  134. }
  135. function hideTip() {
  136. state.tipShow = false;
  137. }
  138. async function handleGroupBtn(group) {
  139. state.tableShow = true;
  140. state.formShow = false;
  141. await nextTick();
  142. getTableAction().reload();
  143. state.group = group.toLowerCase();
  144. }
  145. function addConfig() {
  146. state.tableShow = false;
  147. state.formShow = true;
  148. }
  149. function handleTableReset() {
  150. getTableAction().reload();
  151. }
  152. async function handleTableSubmit() {
  153. console.log('==========handleTableSubmit=========');
  154. const data = getTableAction().getDataSource();
  155. let flag = true;
  156. data.map((item) => {
  157. if (item.rule && item.type !== 'array') {
  158. const rule = item.rule.split(',');
  159. const res = validateType(rule, item.value);
  160. item.errMsg = res.errMsg;
  161. if (!res.isValid) {
  162. flag = res.isValid;
  163. }
  164. }
  165. });
  166. if (flag) {
  167. const params = {};
  168. data.map((item) => {
  169. if (item.type === 'array') {
  170. params[item.name] = JSON.stringify(item.value);
  171. console.log(`item`, item);
  172. console.log(`item.value`, item.value);
  173. } else {
  174. if (item.type === 'selects' || item.type === 'checkbox') {
  175. params[item.name] = item.value.toString();
  176. } else {
  177. params[item.name] = item.value;
  178. }
  179. }
  180. });
  181. await editConfigInfo(params).then(() => {
  182. getTableAction().reload();
  183. success('修改成功!');
  184. });
  185. } else {
  186. console.log('======未通过校验====');
  187. }
  188. }
  189. async function handleFormSubmit(e) {
  190. if (!e.rule) {
  191. e.rule = '';
  192. } else {
  193. e.rule = e.rule.toString();
  194. }
  195. await addConfigInfo(e).then(() => {
  196. success('创建成功!');
  197. handleGroupBtn('Basic'); // 跳转显示到table基础配置
  198. });
  199. }
  200. async function handleDelete(record: Recordable) {
  201. await deleteConfigInfo({ id: record.id }).then(() => {
  202. getTableAction().reload();
  203. success('删除成功!');
  204. });
  205. }
  206. function createActions(record: EditRecordRow): ActionItem[] {
  207. return [
  208. {
  209. label: '',
  210. color: 'error',
  211. icon: 'ic:outline-delete-outline',
  212. popConfirm: {
  213. title: '是否确认删除',
  214. confirm: handleDelete.bind(null, record),
  215. },
  216. },
  217. ];
  218. }
  219. return {
  220. t,
  221. createActions,
  222. tableRef,
  223. registerTable,
  224. registerForm,
  225. showTip,
  226. hideTip,
  227. handleGroupBtn,
  228. addConfig,
  229. handleTableReset,
  230. handleTableSubmit,
  231. handleFormSubmit,
  232. ...toRefs(state),
  233. };
  234. },
  235. });
  236. </script>
  237. <style>
  238. .sys-container {
  239. position: relative;
  240. }
  241. .vben-collapse-container__body > .mr-2 {
  242. margin-top: 5px;
  243. font-weight: 550 !important;
  244. }
  245. .tip {
  246. position: absolute;
  247. top: 30px;
  248. padding: 3px 6px;
  249. color: #fff;
  250. background-color: black;
  251. border-radius: 3px;
  252. }
  253. .config-form {
  254. margin-top: 10px;
  255. }
  256. .ant-form-item-children {
  257. display: flex;
  258. justify-content: center;
  259. }
  260. .actions {
  261. display: flex;
  262. justify-content: center;
  263. }
  264. </style>