crud.tsx 16 KB

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