SDImpDialog.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Upload } from 'antd';
  2. import { useRef, useState } from 'react';
  3. import { baseFun, SDButton, SDForm, SDFormText, SDLayout, SDModal, SDOperate } from '@sundata/ui-frame';
  4. import { getDetail, ImpmTypeConfigModel, uploadAction } from '../services/pubMng/excelMng';
  5. import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
  6. import { UploadOutlined } from '@ant-design/icons';
  7. export type SDImpDialogProps = {
  8. /**组件标识,组件内唯一 */
  9. key: string;
  10. /** 导入类型,格式:模板代码&导入序号&导入期次;导入序号和导入期次可为空 */
  11. type: string;
  12. /**上传按钮调用的方法 */
  13. onclick?: () => any;
  14. /** */
  15. //disTemplate?: boolean;
  16. /**窗口大小 */
  17. impDialogSize?: 'l' | 'm' | 's' | 'large' | 'medium' | 'small';
  18. /** */
  19. visible: boolean;
  20. //onVisibleChange: React.Dispatch<React.SetStateAction<boolean>>; /** 弹出窗口显示状态发生改变后的处理方法 */
  21. /** 关联表格的引用对象,应与对应ProTable的actionRef一致,用于刷新表格 */
  22. tableRef?: React.MutableRefObject<ActionType | undefined>;
  23. /**回调函数,上传后返回数据 */
  24. callback?: (data: any) => any;
  25. /**用于关闭弹窗 例:onCancel={()=>setVisible(false)} */
  26. onCancel?: () => any;
  27. /**展示期次,true-展示 false/undefined-不展示 */
  28. showTerm?: boolean;
  29. /** moment规则的日期格式,可使用中文 */
  30. dateFmt?: string;
  31. };
  32. const SDImpDialog: React.FC<SDImpDialogProps> = (props) => {
  33. const formRef = useRef<ProFormInstance>();
  34. console.log(props.type);
  35. const [disModal, setDisModal] = useState<boolean>(props.visible);
  36. const mType: string[] = props.type.split('@');
  37. /**上传文件 */
  38. const [fileState, setFileState] = useState<File>();
  39. /**上传属性 */
  40. const fileProps = {
  41. onRemove: () => {
  42. setFileState(undefined);
  43. },
  44. beforeUpload(info: any) {
  45. setFileState(info);
  46. },
  47. customRequest: (options: { onSuccess: () => void; }) => {
  48. options?.onSuccess?.();
  49. },
  50. };
  51. /**上传提交 */
  52. const uploadSubmit = async () => {
  53. let type = props.type;
  54. if(props.showTerm){
  55. let term = formRef.current?.getFieldValue("term");
  56. if(!term){
  57. baseFun.warning('未选择期次');
  58. return;
  59. }
  60. if(typeof term === 'string'){//传参转换
  61. const date = new Date(term);
  62. const year = date.getFullYear();
  63. const month = (date.getMonth() < 10 ? '0' + (date.getMonth() + 1) : date.getMonth());
  64. term =`${year}${month}`;
  65. }else{//日期控件切换转换
  66. term = term.format("YYYYMM");
  67. }
  68. if(mType.length>1){
  69. type = `${mType[0]}@${mType[1]}@${term}`
  70. }
  71. }
  72. if (!fileState) {
  73. baseFun.warning('未上传文件');
  74. return;
  75. }
  76. const res = await uploadAction(fileState, type);
  77. // if (res.success) {
  78. // baseFun.info('导入成功');
  79. // } else {
  80. // baseFun.error(res.message || '导入失败');
  81. // }
  82. baseFun.info(res._msg)
  83. if (props.callback) props.callback(res);
  84. };
  85. const download = () => {
  86. baseFun.download('/api/admin/excelmanage/importAndAuditp/importExcelModel.do', {
  87. mType: mType[0],
  88. });
  89. };
  90. const getDetailReq = async () => {
  91. const data: ImpmTypeConfigModel = await getDetail({ mtype: mType[0] });
  92. if(mType.length==3 && mType[2]){
  93. return {mtypename: data.mtypename,term: mType[2]}
  94. }else{
  95. return {mtypename: data.mtypename,term: null}
  96. }
  97. };
  98. return (
  99. <SDModal
  100. title=""
  101. visible={disModal}
  102. footer={[ <SDButton successMessage='' autoLoading primary onClick={uploadSubmit}>保存</SDButton>,
  103. <SDButton onClick={download}>下载模板文件</SDButton>,
  104. ]}
  105. size={props.impDialogSize ? props.impDialogSize : 's'}
  106. onCancel={() => {
  107. setDisModal(false);
  108. //props.onVisibleChange(false);
  109. props.tableRef?.current?.reload();
  110. props.tableRef?.current?.clearSelected?.();
  111. if(props.onCancel)props.onCancel();
  112. }}
  113. >
  114. <SDLayout >
  115. <SDForm
  116. editType={'create'}
  117. request={getDetailReq}
  118. formRef={formRef}
  119. >
  120. {props.showTerm &&
  121. <SDFormText name="term" required type='prodmonth'label="期次" dateFmt={props.dateFmt?props.dateFmt:'YYYYMM'}/>
  122. }
  123. <SDFormText name="mtypename" readonlyCond="both" label="模板名称" />
  124. <Upload maxCount={1} {...fileProps} name="file">
  125. <div style={{paddingLeft:'3.4vw'}}>
  126. 上传文件: &nbsp;&nbsp;<SDOperate icon={<UploadOutlined />}>上传</SDOperate>
  127. </div>
  128. </Upload>
  129. </SDForm>
  130. </SDLayout>
  131. </SDModal>
  132. );
  133. };
  134. export default SDImpDialog;