index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!-- eslint-disable vue/multi-word-component-names -->
  2. <template>
  3. <BasicTable @register="registerTable">
  4. <template #tableTitle>
  5. <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
  6. <a-button preIcon="ant-design:container-outlined" type="primary" @click="handleSwitchPageType">更换版本</a-button>
  7. <a-button preIcon="ant-design:question-circle-outlined" type="primary" @click="handleHelp">帮助</a-button>
  8. <a-popconfirm title="确定从剪切板导入配置?" @confirm="handleImport">
  9. <a-button preIcon="ant-design:container-outlined" type="primary">导入</a-button>
  10. </a-popconfirm>
  11. </template>
  12. <template #action="{ record }">
  13. <a class="table-action-link" @click="handleConfig(record)">配置</a>
  14. <a class="table-action-link" @click="handleEdit(record)">编辑</a>
  15. <a-popconfirm title="确定删除?" @confirm="handleDelete(record)">
  16. <a class="table-action-link">删除</a>
  17. </a-popconfirm>
  18. </template>
  19. <!-- <template #bodyCell="{ column, record }">
  20. <slot name="filterCell" v-bind="{ column, record }"></slot>
  21. </template> -->
  22. </BasicTable>
  23. <DeviceModal @register="registerModal" @save-or-update="saveOrUpdateHandler" :showTab="false" :deviceType="deviceType" />
  24. <BasicModal @register="registerConfigModal" @ok="handleUpdate" title="配置文件编辑" defaultFullscreen>
  25. <a-row class="h-full">
  26. <a-col class="h-full" :span="12">
  27. <CodeEditor class="h-100%" :value="configJSON" @change="editorChangeHandler" />
  28. </a-col>
  29. <a-col class="justify-center items-center" style="display: flex" :span="12">
  30. <ModulePlain
  31. :device-type="configJSON!.deviceType"
  32. :module-data="configJSON!.moduleData"
  33. :module-name="configJSON!.moduleName"
  34. :show-style="configJSON!.showStyle"
  35. :data="{}"
  36. :visible="true"
  37. />
  38. </a-col>
  39. </a-row>
  40. </BasicModal>
  41. <BasicModal @register="registerPageTypeModal" @ok="handleUpdatePageType" title="一键修改模块版本">
  42. <BasicForm @register="registerForm" />
  43. </BasicModal>
  44. </template>
  45. <script lang="ts" setup>
  46. // 本页面是用于配置可配置首页的管理页面,更详细的文档见 /views/vent/home/configurable
  47. import { ref, provide, reactive, toRaw } from 'vue';
  48. import { BasicTable } from '/@/components/Table';
  49. import { BasicForm, useForm } from '/@/components/Form';
  50. import { useModal } from '/@/components/Modal';
  51. import DeviceModal from '../comment/DeviceModal.vue';
  52. // import { getToken } from '/@/utils/auth';
  53. // import { useGlobSetting } from '/@/hooks/setting';
  54. import { useListPage } from '/@/hooks/system/useListPage';
  55. import { list, deleteById, saveOrUpdate } from './configuration.api';
  56. import { searchFormSchema, columns, formSchema, pageTypeFormSchema } from './configuration.data';
  57. import { message } from 'ant-design-vue';
  58. import { BasicModal } from '/@/components/Modal';
  59. import CodeEditor from '/@/components/CodeEditor/src/CodeEditor.vue';
  60. import { ModulePresetMap } from './options';
  61. import { pick } from 'lodash-es';
  62. /** @ts-ignore-next-line */
  63. import helpContext from './types?raw';
  64. import ModulePlain from './ModulePlain.vue';
  65. import { Config } from './types';
  66. const formData = reactive<any>({});
  67. const isUpdate = ref(false);
  68. const deviceType = ref('');
  69. const formSchemaData = ref(formSchema);
  70. // const pageType = ref('');
  71. const configJSON = ref<Config>();
  72. provide('formSchema', formSchemaData);
  73. provide('isUpdate', isUpdate);
  74. provide('formData', formData);
  75. provide('deviceType', deviceType);
  76. // const glob = useGlobSetting();
  77. const [registerModal, { openModal, closeModal }] = useModal();
  78. const [registerConfigModal, configModalCtx] = useModal();
  79. const [registerPageTypeModal, pageTypeModalCtx] = useModal();
  80. // 列表页面公共参数、方法
  81. const { tableContext } = useListPage({
  82. tableProps: {
  83. title: '配置列表',
  84. api: list,
  85. columns,
  86. showTableSetting: false,
  87. // size: 'small',
  88. // bordered: false,
  89. formConfig: {
  90. showAdvancedButton: true,
  91. // labelWidth: 100,
  92. labelAlign: 'left',
  93. labelCol: {
  94. xs: 24,
  95. sm: 24,
  96. md: 24,
  97. lg: 9,
  98. xl: 7,
  99. xxl: 5,
  100. },
  101. schemas: searchFormSchema,
  102. },
  103. useSearchForm: true,
  104. striped: true,
  105. actionColumn: {
  106. width: 180,
  107. },
  108. },
  109. });
  110. const [registerForm, { validate }] = useForm({
  111. schemas: pageTypeFormSchema,
  112. showActionButtonGroup: false,
  113. });
  114. //注册table数据
  115. const [registerTable, { reload }] = tableContext;
  116. const saveOrUpdateHandler = async (params) => {
  117. // 如果是新增或者选择了覆盖配置选项的则初始化配置
  118. if (!isUpdate.value || params.allowCover) {
  119. params = {
  120. ...ModulePresetMap[params.desc],
  121. ...params,
  122. };
  123. }
  124. try {
  125. await saveOrUpdate(params, isUpdate.value);
  126. closeModal();
  127. reload();
  128. message.success('保存成功');
  129. } catch (error) {
  130. message.error('保存失败,请联系管理员');
  131. }
  132. };
  133. /**
  134. * 新增事件
  135. */
  136. function handleAdd() {
  137. for (let key in formData) {
  138. delete formData[key];
  139. }
  140. isUpdate.value = false;
  141. openModal(true);
  142. }
  143. function handleHelp() {
  144. configJSON.value = helpContext;
  145. configModalCtx.openModal();
  146. }
  147. /**
  148. * 编辑事件
  149. */
  150. function handleEdit(record) {
  151. isUpdate.value = true;
  152. Object.assign(formData, toRaw(record));
  153. openModal(true, { formData }, false);
  154. }
  155. /**
  156. * 删除事件
  157. */
  158. async function handleDelete(record) {
  159. await deleteById({ id: record.id }, reload);
  160. }
  161. function handleConfig(record) {
  162. Object.assign(formData, toRaw(record));
  163. configJSON.value = pick(formData, ['deviceType', 'pageType', 'moduleName', 'moduleData', 'showStyle']);
  164. configModalCtx.openModal();
  165. }
  166. /** 更新配置详情 */
  167. async function handleUpdate() {
  168. isUpdate.value = true;
  169. try {
  170. saveOrUpdateHandler({
  171. id: formData.id,
  172. ...configJSON.value,
  173. })
  174. .then(() => {
  175. message.success('保存成功');
  176. })
  177. .catch(() => {
  178. message.error('保存失败,请联系管理员');
  179. })
  180. .finally(() => {
  181. configModalCtx.closeModal();
  182. });
  183. } catch (e) {
  184. message.error(`无效信息:${e}`);
  185. console.error(e);
  186. }
  187. }
  188. function handleSwitchPageType() {
  189. pageTypeModalCtx.openModal();
  190. }
  191. /** 一件更改某个页面的所有模块版本 */
  192. async function handleUpdatePageType() {
  193. try {
  194. const params = await validate();
  195. const arr = await list({
  196. pageType: params.pageType,
  197. });
  198. await Promise.all(
  199. arr.records.map((r) => {
  200. return saveOrUpdate(
  201. {
  202. ...r,
  203. ...params,
  204. },
  205. true
  206. );
  207. })
  208. );
  209. pageTypeModalCtx.closeModal();
  210. reload();
  211. } catch (e) {
  212. message.error(`错误:${e}`);
  213. console.error(e);
  214. }
  215. }
  216. /** 从剪切板导入 */
  217. async function handleImport() {
  218. try {
  219. let text = '';
  220. // 浏览器会阻止用户在不安全的站点使用剪切板
  221. if (window.navigator.clipboard) {
  222. text = await window.navigator.clipboard.readText();
  223. } else {
  224. text = prompt('请粘贴JSON配置') || '';
  225. }
  226. const arr = JSON.parse(text);
  227. if (!Array.isArray(arr)) {
  228. throw '剪切板内容格式错误';
  229. }
  230. await Promise.all(
  231. arr.map((r) => {
  232. return saveOrUpdate(r, false);
  233. })
  234. );
  235. reload();
  236. } catch (e) {
  237. message.error(`错误:${e}`);
  238. console.error(e);
  239. }
  240. }
  241. function editorChangeHandler(val) {
  242. configJSON.value = JSON.parse(val);
  243. }
  244. </script>
  245. <style scoped lang="less">
  246. @ventSpace: zxm;
  247. @vent-table-no-hover: #00bfff10;
  248. :deep(.@{ventSpace}-table-cell-row-hover) {
  249. background: #264d8833 !important;
  250. }
  251. :deep(.@{ventSpace}-table-row-selected) {
  252. background: #268bc522 !important;
  253. }
  254. :deep(.@{ventSpace}-table-tbody > tr > td) {
  255. background-color: #0dc3ff05;
  256. }
  257. :deep(.jeecg-basic-table-row__striped) {
  258. td {
  259. background-color: @vent-table-no-hover !important;
  260. }
  261. }
  262. :deep(.@{ventSpace}-select-dropdown) {
  263. .@{ventSpace}-select-item-option-selected,
  264. .@{ventSpace}-select-item-option-active {
  265. background-color: #ffffff33 !important;
  266. }
  267. .@{ventSpace}-select-item:hover {
  268. background-color: #ffffff33 !important;
  269. }
  270. }
  271. </style>