crud.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import {AddReq, DelReq, EditReq, dict, CreateCrudOptionsRet, CreateCrudOptionsProps} from '@fast-crud/fast-crud';
  2. import * as api from './api';
  3. import {auth} from '/@/utils/authFunction'
  4. import {request} from '/@/utils/service';
  5. import { successNotification } from '/@/utils/message';
  6. import { ElMessage } from 'element-plus';
  7. import { nextTick, ref } from 'vue';
  8. import XEUtils from 'xe-utils';
  9. //此处为crudOptions配置
  10. export const createCrudOptions = function ({crudExpose, context}: CreateCrudOptionsProps): CreateCrudOptionsRet {
  11. const pageRequest = async () => {
  12. if (context!.selectOptions.value.id) {
  13. return await api.GetList({menu: context!.selectOptions.value.id} as any);
  14. } else {
  15. return undefined;
  16. }
  17. };
  18. const editRequest = async ({form, row}: EditReq) => {
  19. return await api.UpdateObj({...form, menu: row.menu});
  20. };
  21. const delRequest = async ({row}: DelReq) => {
  22. return await api.DelObj(row.id);
  23. };
  24. const addRequest = async ({form}: AddReq) => {
  25. return await api.AddObj({...form, ...{menu: context!.selectOptions.value.id}});
  26. };
  27. // 记录选中的行
  28. const selectedRows = ref<any>([]);
  29. const onSelectionChange = (changed: any) => {
  30. const tableData = crudExpose.getTableData();
  31. const unChanged = tableData.filter((row: any) => !changed.includes(row));
  32. // 添加已选择的行
  33. XEUtils.arrayEach(changed, (item: any) => {
  34. const ids = XEUtils.pluck(selectedRows.value, 'id');
  35. if (!ids.includes(item.id)) {
  36. selectedRows.value = XEUtils.union(selectedRows.value, [item]);
  37. }
  38. });
  39. // 剔除未选择的行
  40. XEUtils.arrayEach(unChanged, (unItem: any) => {
  41. selectedRows.value = XEUtils.remove(selectedRows.value, (item: any) => item.id !== unItem.id);
  42. });
  43. };
  44. const toggleRowSelection = () => {
  45. // 多选后,回显默认勾选
  46. const tableRef = crudExpose.getBaseTableRef();
  47. const tableData = crudExpose.getTableData();
  48. const selected = XEUtils.filter(tableData, (item: any) => {
  49. const ids = XEUtils.pluck(selectedRows.value, 'id');
  50. return ids.includes(item.id);
  51. });
  52. nextTick(() => {
  53. XEUtils.arrayEach(selected, (item) => {
  54. tableRef.toggleRowSelection(item, true);
  55. });
  56. });
  57. };
  58. return {
  59. selectedRows,
  60. crudOptions: {
  61. toolbar:{
  62. buttons:{
  63. search:{show:true},
  64. // 刷新按钮
  65. refresh:{
  66. show:true},
  67. // 紧凑模式
  68. compact:{show:false},
  69. // 导出按钮
  70. /* export:{
  71. text: '导出',
  72. type: 'primary',
  73. size: 'small',
  74. icon: 'upload',
  75. circle: false,
  76. display: true
  77. }, */
  78. // 列设置按钮
  79. columns:{
  80. show:false
  81. },
  82. }
  83. },
  84. pagination:{
  85. show:true
  86. },
  87. search: {
  88. container: {
  89. action: {
  90. //按钮栏配置
  91. col: {
  92. span: 8,
  93. },
  94. },
  95. },
  96. },
  97. actionbar: {
  98. buttons: {
  99. add: {
  100. show: auth('btn:Create')
  101. },
  102. batchAdd: {
  103. show: true,
  104. type: 'primary',
  105. text: '批量生成',
  106. click: async () => {
  107. if (context!.selectOptions.value.id == undefined) {
  108. ElMessage.error('请选择菜单');
  109. return;
  110. }
  111. const result = await api.BatchAdd({ menu: context!.selectOptions.value.id });
  112. if (result.code == 2000) {
  113. successNotification(result.msg);
  114. crudExpose.doRefresh();
  115. }
  116. },
  117. },
  118. },
  119. },
  120. rowHandle: {
  121. //固定右侧
  122. fixed: 'right',
  123. width: 200,
  124. buttons: {
  125. view: {
  126. show: false,
  127. },
  128. edit: {
  129. icon: '',
  130. type: 'primary',
  131. show: auth('btn:Update')
  132. },
  133. remove: {
  134. show: auth('btn:Delete')
  135. },
  136. },
  137. },
  138. request: {
  139. pageRequest,
  140. addRequest,
  141. editRequest,
  142. delRequest,
  143. },
  144. table: {
  145. rowKey: 'id', //设置你的主键id, 默认rowKey=id
  146. onSelectionChange,
  147. onRefreshed: () => toggleRowSelection(),
  148. },
  149. form: {
  150. col: {span: 24},
  151. labelWidth: '100px',
  152. wrapper: {
  153. is: 'el-dialog',
  154. width: '600px',
  155. },
  156. },
  157. columns: {
  158. $checked: {
  159. title: '选择',
  160. form: { show: false },
  161. column: {
  162. type: 'selection',
  163. align: 'center',
  164. width: '70px',
  165. columnSetDisabled: true, //禁止在列设置中选择
  166. },
  167. },
  168. _index: {
  169. title: '序号',
  170. form: {show: false},
  171. column: {
  172. type: 'index',
  173. align: 'center',
  174. width: '70px',
  175. columnSetDisabled: true, //禁止在列设置中选择
  176. },
  177. },
  178. search: {
  179. title: '关键词',
  180. column: {show: false},
  181. type: 'text',
  182. search: {show: true},
  183. form: {
  184. show: false,
  185. component: {
  186. placeholder: '输入关键词搜索',
  187. },
  188. },
  189. },
  190. id: {
  191. title: 'ID',
  192. type: 'text',
  193. column: {show: false},
  194. search: {show: false},
  195. form: {show: false},
  196. },
  197. name: {
  198. title: '权限名称',
  199. type: 'text',
  200. search: {show: true},
  201. column: {
  202. minWidth: 120,
  203. sortable: true,
  204. },
  205. form: {
  206. rules: [{required: true, message: '权限名称必填'}],
  207. component: {
  208. placeholder: '输入权限名称搜索',
  209. props: {
  210. clearable: true,
  211. allowCreate: true,
  212. filterable: true,
  213. },
  214. },
  215. helper: {
  216. render() {
  217. return <el-alert title="手动输入" type="warning"
  218. description="页面中按钮的名称或者自定义一个名称"/>;
  219. },
  220. },
  221. },
  222. },
  223. value: {
  224. title: '权限值',
  225. type: 'text',
  226. search: {show: false},
  227. column: {
  228. width: 200,
  229. sortable: true,
  230. },
  231. form: {
  232. rules: [{required: true, message: '权限标识必填'}],
  233. placeholder: '输入权限标识',
  234. helper: {
  235. render() {
  236. return <el-alert title="唯一值" type="warning"
  237. description="用于判断前端按钮权限或接口权限"/>;
  238. },
  239. },
  240. },
  241. },
  242. method: {
  243. title: '请求方式',
  244. search: {show: false},
  245. type: 'dict-select',
  246. column: {
  247. width: 120,
  248. sortable: true,
  249. },
  250. dict: dict({
  251. data: [
  252. {label: 'GET', value: 0},
  253. {label: 'POST', value: 1, color: 'success'},
  254. {label: 'PUT', value: 2, color: 'warning'},
  255. {label: 'DELETE', value: 3, color: 'danger'},
  256. ],
  257. }),
  258. form: {
  259. rules: [{required: true, message: '必填项'}],
  260. },
  261. },
  262. api: {
  263. title: '接口地址',
  264. search: {show: false},
  265. type: 'dict-select',
  266. dict: dict({
  267. /* getData() {
  268. return request({url: '/swagger.json'}).then((res: any) => {
  269. const ret = Object.keys(res.paths);
  270. const data = [];
  271. for (const item of ret) {
  272. const obj: any = {};
  273. obj.label = item;
  274. obj.value = item;
  275. data.push(obj);
  276. }
  277. return data;
  278. });
  279. }, */
  280. }),
  281. column: {
  282. minWidth: 250,
  283. sortable: true,
  284. },
  285. form: {
  286. rules: [{required: true, message: '必填项'}],
  287. component: {
  288. props: {
  289. allowCreate: true,
  290. filterable: true,
  291. clearable: true,
  292. },
  293. },
  294. helper: {
  295. render() {
  296. return <el-alert title="请正确填写,以免请求时被拦截。匹配单例使用正则,例如:/api/xx/.*?/"
  297. type="warning"/>;
  298. },
  299. },
  300. },
  301. },
  302. },
  303. },
  304. };
  305. };