detail.jsx 8.45 KB
Newer Older
wuhao's avatar
wuhao committed
1 2
import React, { useState, useEffect, memo } from "react";
import {
wuhao's avatar
wuhao committed
3 4 5 6 7 8 9 10 11 12 13 14 15
  Table,
  Divider,
  Card,
  Button,
  Typography,
  Spin,
  Row,
  Col,
  Tooltip,
  Rate,
  Image,
  message,
  Section,
wuhao's avatar
wuhao committed
16 17 18
} from "antd";
import { doFetch, postFetch } from "@/utils/doFetch";
import AutoTable from "@/components/Tableform";
wuhao's avatar
wuhao committed
19
import { useModel } from "umi";
wuhao's avatar
wuhao committed
20
import styles from "./index.less";
wuhao's avatar
wuhao committed
21
import PrintProvider, { Print, NoPrint } from "react-easy-print";
wuhao's avatar
wuhao committed
22 23

const Details = (props) => {
wuhao's avatar
wuhao committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * dataSource: 整个页面的数据,Object
   * totalPath: 整个详情页的接口,String
   * totalParams: 整个详情页接口的参数
   * totalCard: 整个页面的模块,Array
   * col: 列数
   *
   * totalCard中的每一项变量:cardTitle每一个模块的标题;itemData每一个模块的数据信息(Array)
   * itemData中的每一项变量:key所对应的字段;title每一项label标题;type每一项的类型(file,img,textarea,table,不填默认就是普通的展示类型);col每一项的占比;columns表格类型所用;path当类型为表格时,若该表格是单独查询出来的可以填入path;urlName 当传入文件为url为字符串时
   *
   */
  const {
    totalPath = "",
    dataSource,
    totalCard,
    totalParams,
    col,
    bottomNode,
    topNode,
    printRef,
    onloadeddata,
    reload,
  } = props;
  const [pageData, cp] = useState({});
wuhao's avatar
wuhao committed
48

wuhao's avatar
wuhao committed
49
  const { initialState, setInitialState } = useModel("@@initialState");
wuhao's avatar
wuhao committed
50

wuhao's avatar
wuhao committed
51 52 53 54 55 56 57 58 59 60 61 62 63
  useEffect(() => {
    if (dataSource) {
      cp(dataSource);
    } else if (!dataSource && !Array.isArray(totalPath)) {
      doFetch({
        url: totalPath,
        params: totalParams ? { ...totalParams } : {},
      }).then((res) => {
        if (res.code == "0000") {
          if (!res.data) {
            return;
          }
          cp(res?.data?.data);
wuhao's avatar
wuhao committed
64
        }
wuhao's avatar
wuhao committed
65 66 67
      });
    }
  }, []);
wuhao's avatar
wuhao committed
68

wuhao's avatar
wuhao committed
69 70 71
  useEffect(() => {
    onloadeddata?.(pageData);
  }, [pageData]);
wuhao's avatar
wuhao committed
72

wuhao's avatar
wuhao committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  const getCol = (itemcol) => {
    if (itemcol) {
      return itemcol;
    } else {
      return col ? col : { xs: 24, sm: 24, md: 12, lg: 8, xl: 8, xxl: 6 };
    }
  };
  //获取每一项
  const getItem = (itemData) => {
    const {
        type,
        key,
        title,
        columns,
        path,
        urlName,
        col,
        rowKey,
        expandable,
        pagination,
        rowSelection,
      } = itemData,
      value = pageData[key];
    if (!type || type == "input") {
      return value == 0 || value ? (
        <Tooltip title={`${title}:${value}`}>
          <pre className={col ? "" : styles.one}>{value}</pre>
        </Tooltip>
      ) : (
        <div>-</div>
      );
    } else if (type == "file") {
      return (
        <div style={{ display: "flex", flexFlow: "row wrap", width: "100%" }}>
          {value && typeof value == "string" ? (
            <a
              href={value}
              download={value}
              style={{ margin: "0 10px 0 0", display: "block" }}
            >
              {urlName ? urlName : "下载"}
            </a>
          ) : value && Array.isArray(value) && value?.length ? (
            value?.map((el) => {
              return (
                <a
                  key={el.url}
                  href={el.url}
                  download={el.name}
                  style={{ margin: "0 10px 0 0", display: "block" }}
                >
                  {el.name}
                </a>
              );
            })
          ) : (
            "-"
          )}
        </div>
      );
    } else if (type == "img") {
      return (
        <div style={{ display: "flex", flexFlow: "row wrap" }}>
          {value ? (
            Array.isArray(value) && value.length > 0 ? (
              value?.map((el) => {
                return (
                  <Image
                    style={{ margin: "0 10px 0 0" }}
                    src={el?.url}
                    width={30}
                    height={30}
                    key={el?.uid}
                  ></Image>
                );
              })
wuhao's avatar
wuhao committed
149
            ) : (
wuhao's avatar
wuhao committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
              <Image
                style={{ margin: "0 10px 0 0" }}
                src={value}
                width={itemData.width ?? 30}
                height={itemData.height ?? 30}
              ></Image>
            )
          ) : (
            "-"
          )}
        </div>
      );
    } else if (type == "table") {
      return (
        value && (
          <AutoTable
            columns={columns || []}
            path={path ? path : null}
            bordered={false}
            dataSource={path ? null : value}
            rowKey={rowKey ?? "id"}
            expandable={
              expandable
                ? {
                    ...expandable,
                    defaultExpandedRowKeys: value?.map((it) => it.id),
                  }
                : null
            }
            pagination={pagination}
            rowSelection={rowSelection}
          ></AutoTable>
        )
      );
    } else if (type == "rate") {
      return (
        <Rate
          bordered={false}
          className={"simple"}
          style={{ width: "100%" }}
          disabled={itemData.disabled}
          allowHalf={itemData.allowHalf ? itemData.allowHalf : false}
          value={value || 0}
        />
      );
    }
  };
wuhao's avatar
wuhao committed
197

wuhao's avatar
wuhao committed
198
  const pageStyle = `
wuhao's avatar
wuhao committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        @media all {
            .page-break {
            display: none;
            }
        }
        
        @media print {
            html, body {
            height: initial !important;
            overflow: initial !important;
            -webkit-print-color-adjust: exact;
            }
        }
        
        @media print {
            .page-break {
            margin-top: 2rem;
            display: block;
            page-break-before: auto;
            }
        }
        
        @page {
            size: auto;
            margin: 20mm;
        }
    `;
wuhao's avatar
wuhao committed
226

wuhao's avatar
wuhao committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  return (
    <PrintProvider>
      <div className={`${styles.bodyBox} detailCards`} ref={printRef ?? null}>
        <style type="text/css" media="print">
          {pageStyle}
        </style>
        <Spin spinning={false}>
          {topNode && <div>{topNode}</div>}
          {totalCard?.map((item, i) => {
            let Container = item?.noPrint ? NoPrint : Print;
            return (
              <Container force={true}>
                <Card
                  key={i}
                  title={item.cardTitle}
                  bordered={false}
                  style={{ marginBottom: 16 }}
                  bodyStyle={{ padding: "16px 10px" }}
                  extra={
                    item.extraContent ? (
                      item.extraContent
                    ) : item?.extra ? (
                      <Button
                        type="primary"
                        onClick={() => {
                          doFetch({
                            url: item.extrapath,
                            params: initialState[item.extrakey],
                          }).then((res) => {
                            if (res.code == "0000") {
                              if (reload) {
                                reload && reload();
                              } else {
                                message.success("操作成功");
                              }
                            }
                          });
                        }}
                      >
                        保存
                      </Button>
                    ) : null
                  }
                >
                  <Row gutter={[12, 12]}>
                    {item?.itemData?.map((it, j) => {
                      return (
                        <Col key={j} {...getCol(it.col)}>
                          <div style={{ width: "100%", display: "flex" }}>
                            <div
                              style={{
                                flexShrink: 0,
                                display: `${it.title ? "block" : "none"}`,
                                textAlign: "right",
                                width: 126,
                              }}
                            >
                              {it.title}
                            </div>
                            {getItem(it)}
                          </div>
                        </Col>
                      );
wuhao's avatar
wuhao committed
290
                    })}
wuhao's avatar
wuhao committed
291 292 293 294 295 296 297 298 299 300
                  </Row>
                </Card>
              </Container>
            );
          })}
          {bottomNode && <div>{bottomNode}</div>}
        </Spin>
      </div>
    </PrintProvider>
  );
wuhao's avatar
wuhao committed
301 302 303
};

export default memo(Details);