crud.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. import { AddReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, uiContext, UserPageQuery } from '@fast-crud/fast-crud';
  2. import dayjs from 'dayjs';
  3. import * as api from './api';
  4. import { uploadFile } from './api';
  5. import { auth } from '/@/utils/authFunction';
  6. import { commonCrudConfig } from '/@/utils/commonCrud';
  7. import {request} from '/@/utils/service';
  8. import {getBaseURL} from '/@/utils/baseUrl';
  9. import Cookies from 'js-cookie';
  10. import { successMessage } from '/@/utils/message';
  11. import { dictionary } from '/@/utils/dictionary';
  12. import { APIResponseData } from '../columns/types';
  13. export const createCrudOptions = function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
  14. const pageRequest = async (query: UserPageQuery) => {
  15. return await api.GetList(query);
  16. };
  17. const editRequest = async ({ form, row }: EditReq) => {
  18. form.id = row.id;
  19. return await api.UpdateObj(form);
  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);
  26. };
  27. const ui = uiContext.get();
  28. return {
  29. crudOptions: {
  30. request: {
  31. pageRequest,
  32. addRequest,
  33. editRequest,
  34. delRequest,
  35. },
  36. toolbar:{
  37. show:false,
  38. },
  39. rowHandle: {
  40. fixed: 'right',
  41. width: 220,
  42. view: {
  43. show: true,
  44. click: ({ row }) => {
  45. window.dispatchEvent(new CustomEvent('workflow-view', { detail: row }));
  46. },
  47. },
  48. edit: {
  49. show: true,
  50. click: ({ row }) => {
  51. window.dispatchEvent(new CustomEvent('workflow-edit', { detail: row }));
  52. },
  53. },
  54. remove: {
  55. show: true,
  56. },
  57. },
  58. actionbar: {
  59. buttons: {
  60. add: {
  61. show: auth('user:Create')
  62. },
  63. batchimport:{
  64. text: "批量导入",//按钮文字
  65. title: "导入",//鼠标停留显示的信息
  66. // show: auth('user:batchimport'),
  67. },
  68. export: {
  69. text: "导出",//按钮文字
  70. title: "导出",//鼠标停留显示的信息
  71. show: auth('user:Export'),
  72. },
  73. downloadtemplate: {
  74. text: "下载模板",//按钮文字
  75. title: "下载模板",//鼠标停留显示的信息
  76. // show: auth('user:Export'),
  77. },
  78. batchdelete: {
  79. text: "批量删除",//按钮文字
  80. title: "批量删除",//鼠标停留显示的信息
  81. // show: auth('user:Export'),
  82. },
  83. }
  84. },
  85. columns: {
  86. _index: {
  87. title: '序号',
  88. form: { show: false },
  89. column: {
  90. align: 'center',
  91. width: '70px',
  92. columnSetDisabled: true,
  93. formatter: (context) => {
  94. let index = context.index ?? 1;
  95. let pagination = crudExpose!.crudBinding.value.pagination;
  96. return ((pagination!.currentPage ?? 1) - 1) * pagination!.pageSize + index + 1;
  97. },
  98. },
  99. },
  100. code: {
  101. title: '设备编号',
  102. search: { show: true },
  103. type: 'input',
  104. column: { minWidth: 120 },
  105. form: {
  106. component: { placeholder: '请输入设备编号' },
  107. rules: [{ required: true, message: '请输入设备编号' }],
  108. },
  109. },
  110. category_name: {
  111. title: '设备分类',
  112. search: { show: true },
  113. type: 'dict-select',
  114. dict: dict({
  115. url: '/api/system/device/category/',
  116. value: 'id',
  117. label: 'name',
  118. }),
  119. column: { minWidth: 100 },
  120. form: {
  121. show:false,
  122. component: { placeholder: '请选择设备分类' },
  123. rules: [{ required: true, message: '请选择设备分类' }],
  124. },
  125. },
  126. name: {
  127. title: '设备名称',
  128. search: { show: false },
  129. type: 'input',
  130. column: { minWidth: 120 },
  131. form: {
  132. component: { placeholder: '请输入设备名称' },
  133. rules: [{ required: true, message: '请输入设备名称' }]
  134. }
  135. },
  136. // statusss: {
  137. // title: '设备状态',
  138. // type: 'dict-select',
  139. // search: { show: false },
  140. // dict: dict({
  141. // data: [
  142. // { label: '在借', value: 1 },
  143. // { label: '空闲', value: 2 },
  144. // ],
  145. // }),
  146. // column: { minWidth: 100 },
  147. // form: {
  148. // component: { placeholder: '请选择设备状态' },
  149. // rules: [{ required: true, message: '请选择设备状态' }],
  150. // },
  151. // },
  152. "inventory.borrowed_quantity":{
  153. title: '设备状态',
  154. type: 'dict-select',
  155. search: { show: false },
  156. dict: dict({
  157. data: [
  158. { label: '在借', value: 1 },
  159. { label: '空闲', value: 0 },
  160. ],
  161. getData: (dict) => {
  162. return (value) => {
  163. return value > 0 ? '在借' : '空闲';
  164. }
  165. }
  166. }),
  167. column: { show:true, minWidth: 100 },
  168. form: {
  169. show:false,
  170. component: { placeholder: '请选择设备状态' },
  171. rules: [{ required: true, message: '请选择设备状态' }],
  172. },
  173. },
  174. borrowed_quantity:{
  175. title:"已借出",
  176. type:'input',
  177. search:{show:false},
  178. column: { minWidth: 100 },
  179. form: {
  180. show:false,
  181. component: { placeholder: '请输入已借出' },
  182. rules: [{ required: true, message: '请输入已借出' }],
  183. },
  184. },
  185. brand:{
  186. title:'品牌',
  187. type:"input",
  188. column: { minWidth: 100 },
  189. },
  190. specification: {
  191. title: '规格型号',
  192. search: { show: false },
  193. type: 'input',
  194. column: { minWidth: 100 },
  195. form: {
  196. component: { placeholder: '请输入规格型号' }
  197. }
  198. },
  199. serial_number:{
  200. title:'序列号',
  201. type:"input",
  202. column: { minWidth: 100 },
  203. },
  204. total_quantity:{
  205. title:'库存数量',
  206. type:"input",
  207. column: { minWidth: 100 },
  208. addForm:{
  209. show:false,
  210. },
  211. viewForm:{
  212. show:false,
  213. },
  214. editForm:{show:false},
  215. from:{
  216. show:false,
  217. component: { placeholder: '请输入已借出' },
  218. rules: [{ required: true, message: '请输入已借出' }],
  219. }
  220. },
  221. warehouse:{
  222. title:'库存仓库',
  223. type:"dict-select",
  224. column: { minWidth: 150 },
  225. dict: dict({
  226. url: '/api/system/warehouse/',
  227. value: 'id',
  228. label: 'name'
  229. }),
  230. form: {
  231. component: { placeholder: '请选择库存仓库' }
  232. }
  233. },
  234. status:{
  235. title:'是否可见',
  236. type: 'dict-switch',
  237. column: {
  238. minWidth: 90,
  239. component: {
  240. name: 'fs-dict-switch',
  241. activeText: '',
  242. inactiveText: '',
  243. style: '--el-switch-on-color: var(--el-color-primary); --el-switch-off-color: #dcdfe6',
  244. onChange: compute((context) => {
  245. return () => {
  246. api.UpdateObjStatus(context.row).then((res:APIResponseData) => {
  247. successMessage(res.msg as string);
  248. });
  249. };
  250. }),
  251. },
  252. },
  253. dict: dict({
  254. data: dictionary('device_button_status_bool'),
  255. }),
  256. },
  257. department: {
  258. title: '所属部门',
  259. search: { show: false },
  260. type: 'input',
  261. column: { minWidth: 120 },
  262. form: {
  263. component: { placeholder: '请输入所属部门' }
  264. }
  265. },
  266. purchase_date: {
  267. title: '采购时间',
  268. type: 'date',
  269. search: { show: false },
  270. column: {
  271. minWidth: 120,
  272. formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD') : ''),
  273. },
  274. form: {
  275. component: { placeholder: '请选择购买日期' },
  276. rules: [{ required: true, message: '请选择购买日期' }],
  277. },
  278. valueResolve({ form, value }) {
  279. form.purchase_date = value ? dayjs(value).format('YYYY-MM-DD') : null;
  280. },
  281. valueBuilder({ form, value }) {
  282. form.purchase_date = value;
  283. },
  284. },
  285. supplier: {
  286. title: '供应商',
  287. type: 'dict-select',
  288. search: { show: false },
  289. column: {
  290. minWidth: 120,
  291. },
  292. dict: dict({
  293. url: '/api/system/supplier/',
  294. value: 'id',
  295. label: 'name'
  296. }),
  297. form: {
  298. component: { placeholder: '请输入供应商' },
  299. },
  300. },
  301. price: {
  302. title: '价格',
  303. type: 'number',
  304. search: { show: false },
  305. column: { minWidth: 100 },
  306. form: {
  307. component: { placeholder: '请输入设备购入价格' },
  308. rules: [{ required: true, message: '请输入设备购入价格' }],
  309. },
  310. },
  311. warranty_expiration: {
  312. title: '保质期',
  313. type: 'date',
  314. search: { show: false },
  315. column: { minWidth: 120 ,
  316. formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD') : ''),
  317. },
  318. form: {
  319. component: { placeholder: '请选择购买日期' },
  320. rules: [{ required: true, message: '请选择购买日期' }],
  321. },
  322. valueResolve({ form, value }) {
  323. form.warranty_expiration = value ? dayjs(value).format('YYYY-MM-DD') : null;
  324. },
  325. valueBuilder({ form, value }) {
  326. form.warranty_expiration = value;
  327. },
  328. },
  329. image:{
  330. title: '设备图片',
  331. type: 'image-uploader',
  332. column: {
  333. minWidth: 120,
  334. show:false,
  335. },
  336. form: {
  337. show: auth('image:upload'),
  338. component: {
  339. uploader: {
  340. type: 'form',
  341. limit: 1,
  342. action: '/api/system/device/upload-image/',
  343. accept: ".jpg,.png",
  344. uploadRequest: async ({ action, file, onProgress }) => {
  345. const token = Cookies.get('token');
  346. // eslint-disable-next-line no-console
  347. console.log("token:::",token);
  348. const data = new FormData();
  349. data.append("image", file);
  350. return await request({
  351. url: action,
  352. method: "post",
  353. timeout: 60000,
  354. headers: {
  355. "Content-Type": "multipart/form-data",
  356. "Authorization": token ? `JWT ${token}` : ''
  357. },
  358. data,
  359. onUploadProgress: (p) => {
  360. onProgress({ percent: Math.round((p.loaded / p.total) * 100) });
  361. }
  362. });
  363. },
  364. successHandle(ret) {
  365. // eslint-disable-next-line no-console
  366. console.log("ret.data.image_url:::",ret.data.image_url);
  367. return {
  368. url: getBaseURL(ret.data.image_url),
  369. key: ret.data.id,
  370. ...ret.data
  371. };
  372. },
  373. }
  374. },
  375. },
  376. valueBuilder({ row, key }) {
  377. return row[key] ? [row[key]] : [];
  378. },
  379. valueResolve({ form, key }) {
  380. // eslint-disable-next-line no-console
  381. console.log("form[key]:::",form[key]);
  382. form[key] = Array.isArray(form[key]) ? form[key][0] : form[key];
  383. }
  384. // form: {
  385. // // component: { placeholder: '请上传图片' },
  386. // component: {
  387. // limit: 1,
  388. // uploader: {
  389. // type: "form"
  390. // }
  391. // },
  392. // rules: createUploaderRules([{ required: true, message: "此项必传", trigger: "change" }]),
  393. // change:()=>{
  394. // },
  395. // helper: "上传设备图片",
  396. // },
  397. },
  398. tenant_id:{
  399. title: '租户id',
  400. type: 'dict-select',
  401. column: {
  402. show:false,
  403. minWidth: 120,
  404. },
  405. dict: dict({
  406. url: '/api/system/tenant/list/',
  407. value: 'id',
  408. label: 'name'
  409. }),
  410. form: {
  411. component: { placeholder: '请填租户id' },
  412. rules: [{ required: false, message: '请填租户id' }],
  413. },
  414. },
  415. category:{
  416. show: false,
  417. title: '类别id',
  418. search: { show: false },
  419. type: 'dict-select',
  420. column: { show: false,minWidth: 120 },
  421. dict: dict({
  422. url: '/api/system/device/category/',
  423. value: 'id',
  424. label: 'name'
  425. }),
  426. form: {
  427. component: { placeholder: '请输入类别id' }
  428. }
  429. },
  430. quantity:{
  431. show: false,
  432. title: '库存数量',
  433. search: { show: false },
  434. type: 'input',
  435. column: { show: false,minWidth: 120 },
  436. form: {
  437. component: { placeholder: '请输入库存数量' }
  438. }
  439. },
  440. tags:{
  441. show: false,
  442. title: '设备标签',
  443. search: { show: false },
  444. type: 'dict-select',
  445. column: { show: false,minWidth: 120 },
  446. dict: dict({
  447. url: '/api/system/device_tags/',
  448. value: 'id',
  449. label: 'name'
  450. }),
  451. form: {
  452. component: { placeholder: '请选择设备标签' },
  453. valueResolve({ form, value }) {
  454. form.tags = Array.from(String(value), Number);
  455. },
  456. valueBuilder({ form, value }) {
  457. form.tags =Array.from(String(value), Number);
  458. },
  459. }
  460. },
  461. // maintenancename: {
  462. // title: '维修名称',
  463. // type: 'input',
  464. // search: { show: false },
  465. // column: { show:false,minWidth: 100 },
  466. // form: {
  467. // component: { placeholder: '请输入设备购入价格' },
  468. // rules: [{ required: true, message: '请输入设备购入价格' }],
  469. // },
  470. // },
  471. ...commonCrudConfig({
  472. create_datetime: { search: false },
  473. update_datetime: { search: false },
  474. }),
  475. },
  476. form: {
  477. row: { gutter: 20 },
  478. group: {
  479. groupType: "tabs", //collapse, tabs
  480. accordion: false,
  481. groups: {
  482. base: {
  483. slots: {
  484. label: (scope) => {
  485. return (
  486. <span style={{ color: scope.hasError ? "red" : "green" }}>
  487. <fs-icon icon={"ion:checkmark-circle"} />
  488. 基础信息
  489. </span>
  490. );
  491. }
  492. },
  493. icon: "el-icon-goods",
  494. columns: ["code","category_name", "name", "status","borrowed_quantity","brand","specification","serial_number","warehouse","is_visible","department","purchase_date","supplier","price","warranty_expiration","image","tenant_id","category","quantity","tags"]
  495. },
  496. borrow: {
  497. label: "借用记录",
  498. icon: "el-icon-price-tag",
  499. columns: []
  500. },
  501. maintenance: {
  502. label: "维修记录",
  503. collapsed: true, //默认折叠
  504. icon: "el-icon-warning-outline",
  505. columns: [""]
  506. },
  507. maintain: {
  508. label: "保养记录",
  509. collapsed: true, //默认折叠
  510. icon: "el-icon-warning-outline",
  511. columns: []
  512. },
  513. datastatis: {
  514. label: "数据统计",
  515. collapsed: true, //默认折叠
  516. icon: "el-icon-warning-outline",
  517. columns: []
  518. },
  519. }
  520. }
  521. },
  522. },
  523. };
  524. };