index.vue 7.6 KB

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