index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React,{Component} from 'react';
  2. import {Icon, Spin, Upload} from 'antd';
  3. import './index.less';
  4. import Viewer from 'viewerjs';
  5. import 'viewerjs/dist/viewer.css';
  6. import PropTypes from 'prop-types';
  7. // const url1 = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1601012352754&di=d33308119328f5ed987bd90031f98cfa&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F27%2F81%2F01200000194677136358818023076.jpg';
  8. // const url2 = 'https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1601001245&di=60b5e1a42beb791524b46d7e3677f1f4&src=http://a2.att.hudong.com/13/86/01300001285722131043860050752.jpg';
  9. const uploadButton = (
  10. <div>
  11. <Icon type="plus" />
  12. <div className="ant-upload-text">点击上传</div>
  13. </div>
  14. );
  15. class ImgList extends Component{
  16. constructor(props) {
  17. super(props);
  18. this.state={
  19. seeMore: false,
  20. gallery: null,
  21. loading: true,
  22. imgLoadNum: 0,
  23. }
  24. this.imgLoading = this.imgLoading.bind(this);
  25. this.setTimeOutObj = null;
  26. }
  27. componentDidMount() {
  28. this.setTimeOutObj = setTimeout(()=>{
  29. this.setState({
  30. gallery: new Viewer(document.getElementById(this.props.domId))
  31. },()=>{
  32. this.setState({
  33. loading: false
  34. })
  35. })
  36. },10000)
  37. }
  38. imgLoading(){
  39. let num = this.state.imgLoadNum + 1;
  40. if(num >= this.props.fileList.length){
  41. this.state.gallery && this.state.gallery.destroy();
  42. this.setState({
  43. gallery: new Viewer(document.getElementById(this.props.domId))
  44. },()=>{
  45. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  46. this.setState({
  47. loading: false
  48. })
  49. })
  50. }
  51. this.setState({
  52. imgLoadNum: num
  53. })
  54. }
  55. componentWillUnmount() {
  56. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  57. }
  58. render() {
  59. const {fileList} = this.props;
  60. if(!fileList || !Array.isArray(fileList)){
  61. return null;
  62. }
  63. return (
  64. <div className='ImgListComponent'>
  65. <div className='ImgList'>
  66. <Spin spinning={fileList.length === 0 ? false : this.state.loading} tip="图片加载中...">
  67. <Upload
  68. {...this.props.uploadConfig}
  69. className="demandDetailShow-upload"
  70. listType="picture-card"
  71. fileList={
  72. Object.keys(this.props.uploadConfig).length !== 0 ?
  73. (
  74. fileList.map((v,i)=>{
  75. v.index = i;
  76. // v.url = i%2 ? url1 : url2;
  77. return v
  78. })
  79. ) : (
  80. this.state.seeMore?
  81. fileList.map((v,i)=>{
  82. v.index = i;
  83. // v.url = i%2 ? url1 : url2;
  84. return v
  85. }) :
  86. fileList.map((v,i)=>{
  87. v.index = i;
  88. // v.url = i%2 ? url1 : url2;
  89. return v
  90. }).slice(0,10)
  91. )
  92. }
  93. onPreview={(file) => {
  94. this.state.gallery.view(file.index).show();
  95. }}
  96. onChange={(infor)=>{
  97. this.state.gallery && this.state.gallery.destroy();
  98. this.props.onChange(infor)
  99. }}
  100. >
  101. {Object.keys(this.props.uploadConfig).length === 0 ? "" : uploadButton}
  102. </Upload>
  103. </Spin>
  104. </div>
  105. {fileList.length > 10 && Object.keys(this.props.uploadConfig).length === 0 ? <div className='seeMore' onClick={()=>{this.setState({seeMore:!this.state.seeMore})}}>
  106. {this.state.seeMore ? '收起' : '查看更多'}
  107. </div> : <div/>}
  108. <ul id={this.props.domId} style={{display:'none',zIndex:-1,}}>
  109. {
  110. fileList.map((value,index)=>(
  111. <li key={index}>
  112. <img src={value.url || value.thumbUrl} onLoad={()=>{
  113. this.imgLoading();
  114. }}/>
  115. </li>
  116. ))
  117. }
  118. </ul>
  119. </div>
  120. )
  121. }
  122. }
  123. ImgList.propTypes = {
  124. domId : PropTypes.string, //如果一个界面有多个该组件,需要传入自定义id名称,用于图片列表显示
  125. fileList: PropTypes.array.isRequired, //图片文件列表
  126. uploadConfig: PropTypes.object, //上传图片配置参数(需要上传图片时需要,存在与否,用于区分是否为上传或者为查看)
  127. onChange: PropTypes.func, //选择图片
  128. }
  129. ImgList.defaultProps = {
  130. domId: 'images',
  131. fileList: [],
  132. uploadConfig: {},
  133. onChange:()=>{}
  134. }
  135. export default ImgList;