models.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { uploadFile } from '@/api/system/oss';
  2. import { VXETable, VxeColumnPropTypes } from 'vxe-table';
  3. export const cmToPx = (cm: number, dpi = 96) => {
  4. return (cm * dpi) / 2.54;
  5. };
  6. export const importFileGetUrl = async (types: string[] = ['xlsx', 'xls']) => {
  7. const { file } = await VXETable.readFile({
  8. types
  9. });
  10. const curPath = URL.createObjectURL(file);
  11. const formData = new FormData();
  12. formData.append('file', file, file.name);
  13. const { data } = await uploadFile(formData);
  14. return data;
  15. };
  16. export const importFileGetUrls = async (types: string[] = ['png', 'jpg']) => {
  17. const { files } = await VXETable.readFile({
  18. types,
  19. multiple: true
  20. });
  21. const promises = Array.from(files).map((file: File) => importFileGetUrlByFile(file));
  22. return Promise.all(promises);
  23. };
  24. // 传入file, file.name 利用formData上传,用
  25. export const importFileGetUrlByFile = async (file: File) => {
  26. const formData = new FormData();
  27. formData.append('file', file, file.name);
  28. return uploadFile(formData);
  29. };
  30. export const filterBool = (val: string) => {
  31. if (val) {
  32. return +val ? '是' : '否';
  33. } else {
  34. return '-';
  35. }
  36. };
  37. export const colNoData: VxeColumnPropTypes.Formatter<any> = ({ cellValue }) => {
  38. if (cellValue) {
  39. return cellValue;
  40. } else {
  41. return '-';
  42. }
  43. };
  44. export const formatterfilterBool: VxeColumnPropTypes.Formatter<any> = ({ cellValue }) => {
  45. if (cellValue) {
  46. return +cellValue ? '是' : '否';
  47. } else {
  48. return '-';
  49. }
  50. };
  51. export const formatWarehouseRow = (warehouseInfo: any) => {
  52. if (warehouseInfo && warehouseInfo.length) {
  53. const strTem: string[] = [];
  54. for (let i = 0; i < warehouseInfo.length; i++) {
  55. const { warehouseName = '', shelvesName = '', warehouseSn = '', shelvesSn = '' } = warehouseInfo[i];
  56. const strKf = warehouseName ? `${warehouseName}${warehouseSn ? '(' + warehouseSn + ')' : ''}` : '';
  57. const strCk = shelvesName ? `${shelvesName}${shelvesSn ? '(' + shelvesSn + ')' : ''}` : '';
  58. strTem.push(strKf + (strCk ? '/' + strCk : ''));
  59. }
  60. return strTem.toString();
  61. } else {
  62. return '-';
  63. }
  64. };
  65. // 格式化产地
  66. export const formatOrigin = (adcdCodeDesc: any, nationTypeDesc: any) => {
  67. if (nationTypeDesc === '中国大陆' || !nationTypeDesc) {
  68. return adcdCodeDesc || '-';
  69. } else {
  70. return nationTypeDesc || '-';
  71. }
  72. };
  73. // 根据传入url获取文件后缀名,根据文件后缀名返回相应的图片icon
  74. export const getFileIconByUrl = (url: string) => {
  75. const fileExtension = url.substring(url.lastIndexOf('.') + 1).toLowerCase();
  76. const iconMap: Record<string, string> = {
  77. jpg: 'jpg',
  78. jpeg: 'jpg',
  79. png: 'png',
  80. gif: 'jpg',
  81. pdf: 'pdf',
  82. doc: 'doc',
  83. docx: 'doc',
  84. xls: 'xlsx',
  85. xlsx: 'xlsx',
  86. txt: 'txt',
  87. // 大写后缀映射
  88. JPG: 'jpg',
  89. JPEG: 'jpg',
  90. PNG: 'png',
  91. GIF: 'jpg',
  92. PDF: 'pdf',
  93. DOC: 'doc',
  94. DOCX: 'doc',
  95. XLS: 'xlsx',
  96. XLSX: 'xlsx',
  97. TXT: 'txt'
  98. };
  99. return `https://ta.zycpzs.cn/oss-file/smart-trace/szyy/images/file-type-sub/${iconMap?.[fileExtension] || 'def'}.png`; // 默认图标
  100. };