crud.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import { inject } from 'vue';
  2. import { dict, UserPageQuery, AddReq, DelReq, EditReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet } from '@fast-crud/fast-crud';
  3. import { request } from '/@/utils/service';
  4. import * as api from './api';
  5. import { dictionary } from '/@/utils/dictionary';
  6. import { successMessage } from '/@/utils/message';
  7. import {auth} from "/@/utils/authFunction";
  8. export const createCrudOptions = function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  9. const pageRequest = async (query: UserPageQuery) => {
  10. const show_all = context?.isShowChildFlag.value ? '1' : '0';
  11. const res = await api.GetList({ ...query, show_all });
  12. /**
  13. * 处理crud警告:Invalid prop: type check failed for prop "name". Expected String with value "2", got Number with value 2.
  14. */
  15. // res.data.forEach((item: any) => {
  16. // item.dept = String(item.dept);
  17. // if (item.role && Array.isArray(item.role) && item.role.length > 0) {
  18. // item.role = item.role.map((r: number) => String(r));
  19. // }
  20. // });
  21. return res;
  22. };
  23. const editRequest = async ({ form, row }: EditReq) => {
  24. form.id = row.id;
  25. return await api.UpdateObj(form);
  26. };
  27. const delRequest = async ({ row }: DelReq) => {
  28. const res = await api.DelObj(row.id);
  29. context?.getDeptInfo();
  30. return res;
  31. };
  32. const addRequest = async ({ form }: AddReq) => {
  33. const res = await api.AddObj(form);
  34. context?.getDeptInfo();
  35. return res;
  36. };
  37. const exportRequest = async (query: UserPageQuery) => {
  38. return await api.exportData(query);
  39. };
  40. return {
  41. crudOptions: {
  42. table: {
  43. remove: {
  44. confirmMessage: '是否删除该用户?',
  45. },
  46. },
  47. request: {
  48. pageRequest,
  49. addRequest,
  50. editRequest,
  51. delRequest,
  52. },
  53. actionbar: {
  54. buttons: {
  55. add: {
  56. show: auth('user:Create')
  57. },
  58. export: {
  59. text: '导出', //按钮文字
  60. title: '导出', //鼠标停留显示的信息
  61. show: auth('user:Export'),
  62. click() {
  63. return exportRequest(crudExpose!.getSearchFormData());
  64. },
  65. },
  66. },
  67. },
  68. // search: {
  69. // container: {
  70. // layout: 'multi-line',
  71. // action: {
  72. // col: {
  73. // span: 10,
  74. // },
  75. // },
  76. // },
  77. // },
  78. rowHandle: {
  79. //固定右侧
  80. fixed: 'right',
  81. width: 250,
  82. buttons: {
  83. view: {
  84. show: false,
  85. },
  86. edit: {
  87. show: auth('user:Update'),
  88. },
  89. remove: {
  90. show: auth('user:Delete'),
  91. },
  92. custom: {
  93. text: '重设密码',
  94. type: 'primary',
  95. show: auth('user:ResetPassword'),
  96. tooltip: {
  97. placement: 'top',
  98. content: '重设密码',
  99. },
  100. click: (ctx: any) => {
  101. const { row } = ctx;
  102. context?.handleResetPwdOpen(row);
  103. },
  104. },
  105. },
  106. },
  107. columns: {
  108. _index: {
  109. title: '序号',
  110. form: { show: false },
  111. column: {
  112. type: 'index',
  113. align: 'center',
  114. width: '70px',
  115. columnSetDisabled: true, //禁止在列设置中选择
  116. },
  117. },
  118. search: {
  119. title: '关键词',
  120. column: {
  121. show: false,
  122. },
  123. search: {
  124. show: true,
  125. component: {
  126. props: {
  127. clearable: true,
  128. },
  129. placeholder: '请输入关键词',
  130. },
  131. },
  132. form: {
  133. show: false,
  134. component: {
  135. props: {
  136. clearable: true,
  137. },
  138. },
  139. },
  140. },
  141. username: {
  142. title: '账号',
  143. type: 'input',
  144. column: {
  145. minWidth: 100, //最小列宽
  146. },
  147. form: {
  148. rules: [
  149. // 表单校验规则
  150. {
  151. required: true,
  152. message: '账号必填项',
  153. },
  154. ],
  155. component: {
  156. placeholder: '请输入账号',
  157. },
  158. },
  159. },
  160. password: {
  161. title: '密码',
  162. type: 'input',
  163. column: {
  164. show: false,
  165. },
  166. editForm: {
  167. show: false,
  168. },
  169. form: {
  170. rules: [
  171. // 表单校验规则
  172. {
  173. required: true,
  174. message: '密码必填项',
  175. },
  176. ],
  177. component: {
  178. span: 12,
  179. showPassword: true,
  180. placeholder: '请输入密码',
  181. },
  182. // value: vm.systemConfig('base.default_password'),
  183. },
  184. /* valueResolve(row, key) {
  185. if (row.password) {
  186. row.password = vm.$md5(row.password)
  187. }
  188. } */
  189. },
  190. name: {
  191. title: '姓名',
  192. type: 'input',
  193. column: {
  194. minWidth: 100, //最小列宽
  195. },
  196. form: {
  197. rules: [
  198. // 表单校验规则
  199. {
  200. required: true,
  201. message: '姓名必填项',
  202. },
  203. ],
  204. component: {
  205. span: 12,
  206. placeholder: '请输入姓名',
  207. },
  208. },
  209. },
  210. dept: {
  211. title: '部门',
  212. type: 'dict-tree',
  213. dict: dict({
  214. isTree: true,
  215. url: '/api/system/dept/all_dept/',
  216. value: 'id',
  217. label: 'name',
  218. }),
  219. column: {
  220. minWidth: 150, //最小列宽
  221. },
  222. form: {
  223. rules: [
  224. // 表单校验规则
  225. {
  226. required: true,
  227. message: '必填项',
  228. },
  229. ],
  230. component: {
  231. filterable: true,
  232. placeholder: '请选择',
  233. props: {
  234. props: {
  235. value: 'id',
  236. label: 'name',
  237. },
  238. },
  239. },
  240. },
  241. },
  242. role: {
  243. title: '角色',
  244. search: {
  245. show: true,
  246. component: {
  247. props: {
  248. clearable: true,
  249. },
  250. },
  251. },
  252. type: 'dict-select',
  253. dict: dict({
  254. url: '/api/system/role/',
  255. value: 'id',
  256. label: 'name',
  257. }),
  258. column: {
  259. minWidth: 100, //最小列宽
  260. },
  261. form: {
  262. rules: [
  263. // 表单校验规则
  264. {
  265. required: true,
  266. message: '必填项',
  267. },
  268. ],
  269. component: {
  270. multiple: true,
  271. filterable: true,
  272. placeholder: '请选择角色',
  273. },
  274. },
  275. },
  276. mobile: {
  277. title: '手机号码',
  278. type: 'input',
  279. column: {
  280. minWidth: 120, //最小列宽
  281. },
  282. form: {
  283. rules: [
  284. {
  285. max: 20,
  286. message: '请输入正确的手机号码',
  287. trigger: 'blur',
  288. },
  289. {
  290. pattern: /^1[3-9]\d{9}$/,
  291. message: '请输入正确的手机号码',
  292. },
  293. ],
  294. component: {
  295. placeholder: '请输入手机号码',
  296. },
  297. },
  298. },
  299. email: {
  300. title: '邮箱',
  301. column: {
  302. width: 260,
  303. },
  304. form: {
  305. rules: [
  306. {
  307. type: 'email',
  308. message: '请输入正确的邮箱地址',
  309. trigger: ['blur', 'change'],
  310. },
  311. ],
  312. component: {
  313. placeholder: '请输入邮箱',
  314. },
  315. },
  316. },
  317. gender: {
  318. title: '性别',
  319. type: 'dict-select',
  320. dict: dict({
  321. data: dictionary('gender'),
  322. }),
  323. form: {
  324. value: 1,
  325. component: {
  326. span: 12,
  327. },
  328. },
  329. component: { props: { color: 'auto' } }, // 自动染色
  330. },
  331. user_type: {
  332. title: '用户类型',
  333. search: {
  334. show: true,
  335. },
  336. type: 'dict-select',
  337. dict: dict({
  338. data: dictionary('user_type'),
  339. }),
  340. column: {
  341. minWidth: 100, //最小列宽
  342. },
  343. form: {
  344. show: false,
  345. value: 0,
  346. component: {
  347. span: 12,
  348. },
  349. },
  350. },
  351. is_active: {
  352. title: '锁定',
  353. search: {
  354. show: true,
  355. },
  356. type: 'dict-radio',
  357. column: {
  358. component: {
  359. name: 'fs-dict-switch',
  360. activeText: '',
  361. inactiveText: '',
  362. style: '--el-switch-on-color: var(--el-color-primary); --el-switch-off-color: #dcdfe6',
  363. onChange: compute((context) => {
  364. return () => {
  365. api.UpdateObj(context.row).then((res: APIResponseData) => {
  366. successMessage(res.msg as string);
  367. });
  368. };
  369. }),
  370. },
  371. },
  372. dict: dict({
  373. data: dictionary('button_status_bool'),
  374. }),
  375. },
  376. avatar: {
  377. title: '头像',
  378. type: 'avatar-cropper',
  379. form: {
  380. show: false,
  381. },
  382. },
  383. },
  384. },
  385. };
  386. };