index.jsx 5.86 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 149 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
import React, { useState, useEffect } from "react";
import {
  Table,
  Divider,
  Card,
  Descriptions,
  Typography,
  Spin,
  Row,
  Col,
  Tooltip,
  Rate,
  Drawer,
  Image,
} from "antd";
import { postFetch } from "@/utils/doFetch";
import AutoTable from "@/components/Tableform";
import styles from "./index.less";
const { Paragraph } = Typography;

const Details = (props) => {
  /**
   * 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,
  } = props;
  const [pageData, cp] = useState({});

  useEffect(() => {
    if (dataSource) {
      cp(dataSource);
    } else if (!dataSource && !Array.isArray(totalPath)) {
      postFetch({
        url: totalPath,
        params: totalParams ? { ...totalParams } : {},
      }).then((res) => {
        if (res.code == "0000") {
          if (!res.data) {
            return;
          }
          cp(res?.data?.data);
        }
      });
    }
  }, []);

  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,
      } = itemData,
      value = dataSource[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 && value?.length
            ? value?.map((el) => {
                return (
                  <Image
                    style={{ margin: "0 10px 0 0" }}
                    src={el?.url}
                    width={30}
                    height={30}
                    key={el?.uid}
                  ></Image>
                );
              })
            : "-"}
        </div>
      );
    } else if (type == "table") {
      return (
        <AutoTable
          columns={columns || []}
          path={path ? path : null}
          bordered={false}
          dataSource={path ? null : value}
          rowKey={rowKey ?? "id"}
          expandable={expandable ?? null}
        ></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}
        />
      );
    }
  };
  return (
    <Drawer
      maskClosable={false}
      placement="right"
      closable={true}
      getContainer={false}
      style={{ position: "absolute" }}
      {...props}
    >
      <div className={`${styles.bodyBox} detailCards`}>
        <Spin spinning={false}>
          {topNode && <div>{topNode}</div>}
          {totalCard?.map((item, i) => {
            return (
              <Card
                key={i}
                title={item.cardTitle}
                bordered={false}
                style={{ marginBottom: 16 }}
                bodyStyle={{ padding: "16px 10px" }}
              >
                <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>
                    );
                  })}
                </Row>
              </Card>
            );
          })}
          {bottomNode && <div>{bottomNode}</div>}
        </Spin>
      </div>
    </Drawer>
  );
};

export default Details;