crud.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/',
  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: 'dict-select',
  150. search: { show: false },
  151. dict: dict({
  152. data: [
  153. { label: '在借', value: 1 },
  154. { label: '空闲', value: 0 },
  155. ],
  156. getData: (dict) => {
  157. return (value) => {
  158. return value > 0 ? '在借' : '空闲';
  159. }
  160. }
  161. }),
  162. column: { show:true, minWidth: 100 },
  163. form: {
  164. show:false,
  165. component: { placeholder: '请选择设备状态' },
  166. rules: [{ required: true, message: '请选择设备状态' }],
  167. },
  168. },
  169. borrowed_quantity:{
  170. title:"已借出",
  171. type:'input',
  172. search:{show:false},
  173. column: { minWidth: 100 },
  174. form: {
  175. show:false,
  176. component: { placeholder: '请输入已借出' },
  177. rules: [{ required: true, message: '请输入已借出' }],
  178. },
  179. },
  180. brand:{
  181. title:'品牌',
  182. type:"input",
  183. column: { minWidth: 100 },
  184. },
  185. specification: {
  186. title: '规格型号',
  187. search: { show: false },
  188. type: 'input',
  189. column: { minWidth: 100 },
  190. form: {
  191. component: { placeholder: '请输入规格型号' }
  192. }
  193. },
  194. serial_number:{
  195. title:'序列号',
  196. type:"input",
  197. column: { minWidth: 100 },
  198. },
  199. total_quantity:{
  200. title:'库存数量',
  201. type:"input",
  202. column: { minWidth: 100 },
  203. addForm:{
  204. show:false,
  205. },
  206. viewForm:{
  207. show:false,
  208. },
  209. editForm:{show:false},
  210. from:{
  211. show:false,
  212. component: { placeholder: '请输入库存数量' },
  213. rules: [{ required: false, message: '请输入库存数量' }],
  214. }
  215. },
  216. warehouse:{
  217. title:'存放仓库',
  218. type:"dict-select",
  219. column: { minWidth: 150 },
  220. dict: dict({
  221. url: '/api/system/warehouse/',
  222. value: 'id',
  223. label: 'name'
  224. }),
  225. form: {
  226. component: { placeholder: '请选择存放仓库' },
  227. rules: [{ required: true, message: '请选择存放仓库' }],
  228. }
  229. },
  230. status:{
  231. title:'是否可见',
  232. type: 'dict-switch',
  233. column: {
  234. minWidth: 90,
  235. component: {
  236. name: 'fs-dict-switch',
  237. activeText: '',
  238. inactiveText: '',
  239. style: '--el-switch-on-color: var(--el-color-primary); --el-switch-off-color: #dcdfe6',
  240. onChange: compute((context) => {
  241. return () => {
  242. api.UpdateObjStatus(context.row).then((res:APIResponseData) => {
  243. successMessage(res.msg as string);
  244. });
  245. };
  246. }),
  247. },
  248. },
  249. dict: dict({
  250. data: dictionary('device_button_status_bool'),
  251. }),
  252. form: {
  253. rules: [{ required: true, message: '请选择是否可见' }],
  254. }
  255. },
  256. department: {
  257. title: '所属部门',
  258. search: { show: false },
  259. type: 'input',
  260. column: { minWidth: 120 },
  261. form: {
  262. component: { placeholder: '请输入所属部门' }
  263. }
  264. },
  265. purchase_date: {
  266. title: '采购时间',
  267. type: 'date',
  268. search: { show: false },
  269. column: {
  270. minWidth: 120,
  271. formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD') : ''),
  272. },
  273. form: {
  274. component: { placeholder: '请选择采购时间' },
  275. rules: [{ required: false, message: '请选择采购时间' }],
  276. },
  277. valueResolve({ form, value }) {
  278. form.purchase_date = value ? dayjs(value).format('YYYY-MM-DD') : null;
  279. },
  280. valueBuilder({ form, value }) {
  281. form.purchase_date = value;
  282. },
  283. },
  284. supplier: {
  285. title: '供应商',
  286. type: 'dict-select',
  287. search: { show: false },
  288. column: {
  289. minWidth: 120,
  290. },
  291. dict: dict({
  292. url: '/api/system/supplier/',
  293. value: 'id',
  294. label: 'name'
  295. }),
  296. form: {
  297. component: { placeholder: '请输入供应商' },
  298. },
  299. },
  300. price: {
  301. title: '价格',
  302. type: 'number',
  303. search: { show: false },
  304. column: { minWidth: 100 },
  305. form: {
  306. component: { placeholder: '请输入设备购入价格' },
  307. rules: [{ required: false, message: '请输入设备购入价格' }],
  308. },
  309. },
  310. warranty_expiration: {
  311. title: '质保期',
  312. type: 'date',
  313. search: { show: false },
  314. column: { minWidth: 120 ,
  315. formatter: ({ value }) => (value ? dayjs(value).format('YYYY-MM-DD') : ''),
  316. },
  317. viewForm: {
  318. title: '质保到期',
  319. component: {
  320. render({ value }) {
  321. // eslint-disable-next-line no-console
  322. console.log("valuevaluevalue:::",value);
  323. if (!value) return '未设置';
  324. const formatted = dayjs(value).format('YYYY-MM-DD');
  325. const isExpired = dayjs(value).isBefore(dayjs(), 'day');
  326. return h(
  327. 'span',
  328. {
  329. style: {
  330. color: isExpired ? 'red' : 'inherit',
  331. fontWeight: isExpired ? 'bold' : 'normal'
  332. }
  333. },
  334. isExpired ? `${formatted}(已过期)` : formatted
  335. );
  336. },
  337. showHtml: true,
  338. }
  339. },
  340. form: {
  341. component: { placeholder: '请选择质保期' },
  342. rules: [{ required: false, message: '请选择质保期' }],
  343. },
  344. valueResolve({ form, value }) {
  345. form.warranty_expiration = value ? dayjs(value).format('YYYY-MM-DD') : null;
  346. },
  347. valueBuilder({ form, value }) {
  348. form.warranty_expiration = value;
  349. },
  350. },
  351. tenant_id:{
  352. title: '租户id',
  353. type: 'input',
  354. value: 1,
  355. column: {
  356. show:false,
  357. minWidth: 120,
  358. },
  359. // dict: dict({
  360. // url: '/api/system/tenant/list/',
  361. // value: 'id',
  362. // label: 'name'
  363. // }),
  364. form: {
  365. value:1,
  366. show:false,
  367. component: { placeholder: '请填租户id' },
  368. rules: [{ required: false, message: '请填租户id' }],
  369. },
  370. },
  371. category:{
  372. show: false,
  373. title: '设备类别',
  374. search: { show: false },
  375. type: 'dict-select',
  376. column: { show: false,minWidth: 120 },
  377. dict: dict({
  378. url: '/api/system/device/category/',
  379. value: 'id',
  380. label: 'name'
  381. }),
  382. form: {
  383. component: { placeholder: '请输入设备类别' },
  384. rules: [{ required: true, message: '请输入设备类别' }],
  385. }
  386. },
  387. quantity:{
  388. show: false,
  389. title: '库存数量',
  390. search: { show: false },
  391. type: 'input',
  392. column: { show: false,minWidth: 120 },
  393. viewForm:{
  394. component:{placeholder:""}
  395. },
  396. form: {
  397. component: { placeholder: '请输入库存数量' },
  398. rules: [{ required: true, message: '请输入库存数量' }],
  399. valueResolve({ form, value }) {
  400. form.quantity = Number(value ?? 0);
  401. },
  402. valueBuilder({ form, value }) {
  403. // form.tags =Array.from(String(value), Number);
  404. form.quantity = form.total_quantity;
  405. }
  406. }
  407. },
  408. tags:{
  409. show: false,
  410. title: '设备标签',
  411. search: { show: false },
  412. type: 'dict-select',
  413. column: { show: false,minWidth: 120 },
  414. dict: dict({
  415. url: '/api/system/device_tags/',
  416. value: 'id',
  417. label: 'name'
  418. }),
  419. viewForm:{
  420. component:{placeholder:""}
  421. },
  422. form: {
  423. component: { placeholder: '请选择设备标签' },
  424. rules: [{ required: true, message: '请选择设备标签' }],
  425. valueResolve({ form, value }) {
  426. form.tags = Array.from(String(value), Number);
  427. },
  428. valueBuilder({ form, value }) {
  429. if(value){
  430. form.tags = Array.isArray(value)? value.length > 0 ? Number(value[0]) : '': Number(value);
  431. }
  432. },
  433. }
  434. },
  435. image:{
  436. title: '设备图片',
  437. type: 'image-uploader',
  438. column: {
  439. minWidth: 120,
  440. show:false,
  441. },
  442. form: {
  443. show: auth('image:upload'),
  444. component: {
  445. uploader: {
  446. type: 'form',
  447. limit: 1,
  448. action: '/api/system/device/upload-image/',
  449. accept: ".jpg,.png",
  450. uploadRequest: async ({ action, file, onProgress }) => {
  451. const token = Cookies.get('token');
  452. const data = new FormData();
  453. data.append("image", file);
  454. return await request({
  455. url: action,
  456. method: "post",
  457. timeout: 60000,
  458. headers: {
  459. "Content-Type": "multipart/form-data",
  460. "Authorization": token ? `JWT ${token}` : ''
  461. },
  462. data,
  463. onUploadProgress: (p) => {
  464. onProgress({ percent: Math.round((p.loaded / p.total) * 100) });
  465. }
  466. });
  467. },
  468. successHandle(ret) {
  469. // eslint-disable-next-line no-console
  470. // console.log("ret.data.image_url:::",ret.data.image_url);
  471. return {
  472. url: getBaseURL(ret.data.image_url),
  473. key: ret.data.id,
  474. ...ret.data
  475. };
  476. },
  477. }
  478. },
  479. },
  480. valueBuilder({ row, key }) {
  481. return row[key] ? [row[key]] : [];
  482. },
  483. valueResolve({ form, key }) {
  484. // eslint-disable-next-line no-console
  485. console.log("form[key]:::",form[key]);
  486. form[key] = Array.isArray(form[key]) ? form[key][0] : form[key];
  487. }
  488. // form: {
  489. // // component: { placeholder: '请上传图片' },
  490. // component: {
  491. // limit: 1,
  492. // uploader: {
  493. // type: "form"
  494. // }
  495. // },
  496. // rules: createUploaderRules([{ required: true, message: "此项必传", trigger: "change" }]),
  497. // change:()=>{
  498. // },
  499. // helper: "上传设备图片",
  500. // },
  501. },
  502. // maintenancename: {
  503. // title: '维修名称',
  504. // type: 'input',
  505. // search: { show: false },
  506. // column: { show:false,minWidth: 100 },
  507. // form: {
  508. // component: { placeholder: '请输入设备购入价格' },
  509. // rules: [{ required: true, message: '请输入设备购入价格' }],
  510. // },
  511. // },
  512. // ...commonCrudConfig({
  513. // create_datetime: { search: false },
  514. // update_datetime: { search: false },
  515. // }),
  516. },
  517. form: {
  518. row: { gutter: 20 },
  519. group: {
  520. groupType: "tabs", //collapse, tabs
  521. accordion: false,
  522. groups: {
  523. base: {
  524. slots: {
  525. label: (scope) => {
  526. return (
  527. <span style={{ color: scope.hasError ? "red" : "green" }}>
  528. <fs-icon icon={"ion:checkmark-circle"} />
  529. 基础信息
  530. </span>
  531. );
  532. }
  533. },
  534. icon: "el-icon-goods",
  535. 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"]
  536. },
  537. borrow: {
  538. label: "借用记录",
  539. icon: "el-icon-price-tag",
  540. columns: []
  541. },
  542. maintenance: {
  543. label: "维修记录",
  544. collapsed: true, //默认折叠
  545. icon: "el-icon-warning-outline",
  546. columns: [""]
  547. },
  548. maintain: {
  549. label: "保养记录",
  550. collapsed: true, //默认折叠
  551. icon: "el-icon-warning-outline",
  552. columns: []
  553. },
  554. datastatis: {
  555. label: "数据统计",
  556. collapsed: true, //默认折叠
  557. icon: "el-icon-warning-outline",
  558. columns: []
  559. },
  560. }
  561. }
  562. },
  563. },
  564. };
  565. };