index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // fileListSize: 0,
  24. // doneFileSize: 0,
  25. }
  26. this.imgLoading = this.imgLoading.bind(this);
  27. this.setTimeOutObj = null;
  28. }
  29. componentDidMount() {
  30. this.setTimeOutObj = setTimeout(()=>{
  31. this.setState({
  32. gallery: new Viewer(document.getElementById(this.props.domId))
  33. },()=>{
  34. this.setState({
  35. loading: false
  36. })
  37. })
  38. },10000)
  39. }
  40. imgLoading(){
  41. if(Object.keys(this.props.uploadConfig).length !== 0){
  42. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  43. }else{
  44. let num = this.state.imgLoadNum + 1;
  45. if(num >= this.props.fileList.length){
  46. this.state.gallery && this.state.gallery.destroy();
  47. this.setState({
  48. gallery: new Viewer(document.getElementById(this.props.domId))
  49. },()=>{
  50. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  51. this.setState({
  52. loading: false
  53. })
  54. })
  55. }
  56. this.setState({
  57. imgLoadNum: num
  58. })
  59. }
  60. }
  61. componentWillUnmount() {
  62. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  63. }
  64. render() {
  65. const {fileList} = this.props;
  66. if(!fileList || !Array.isArray(fileList)){
  67. return null;
  68. }
  69. return (
  70. <div className='ImgListComponent'>
  71. <div className='ImgList'>
  72. <Spin spinning={Object.keys(this.props.uploadConfig).length !== 0 ? false : (fileList.length === 0 ? false : this.state.loading)} tip="图片加载中...">
  73. <Upload
  74. {...this.props.uploadConfig}
  75. className={Object.keys(this.props.uploadConfig).length !== 0 ? "" : "demandDetailShow-upload" }
  76. listType="picture-card"
  77. fileList={
  78. Object.keys(this.props.uploadConfig).length !== 0 ?
  79. (
  80. fileList.map((v,i)=>{
  81. v.index = i;
  82. // v.url = i%2 ? url1 : url2;
  83. return v
  84. })
  85. ) : (
  86. this.state.seeMore?
  87. fileList.map((v,i)=>{
  88. v.index = i;
  89. // v.url = i%2 ? url1 : url2;
  90. return v
  91. }) :
  92. fileList.map((v,i)=>{
  93. v.index = i;
  94. // v.url = i%2 ? url1 : url2;
  95. return v
  96. }).slice(0,10)
  97. )
  98. }
  99. onPreview={(file) => {
  100. if(Object.keys(this.props.uploadConfig).length !== 0){
  101. //TODO 上传组件,查看图片临时解决方法,无法查看下一张
  102. let image = new Image();
  103. image.src = file.url || file.thumbUrl;
  104. let viewer = new Viewer(image, {
  105. hidden: function () {
  106. viewer.destroy();
  107. },
  108. });
  109. viewer.show();
  110. }else{
  111. this.state.gallery.view(file.index).show();
  112. }
  113. }}
  114. onChange={(infor)=>{
  115. this.state.gallery && this.state.gallery.destroy();
  116. this.props.onChange(infor)
  117. // this.setState({
  118. // fileListSize: infor.fileList.length
  119. // })
  120. // if(infor.file && infor.file.status === 'done'){
  121. // let num = this.state.doneFileSize + 1;
  122. // if(num >= infor.fileList.length){
  123. // console.log('所有都上传成功')
  124. // }
  125. // this.setState({
  126. // doneFileSize: num
  127. // })
  128. // }
  129. // console.log(infor.event)
  130. }}
  131. >
  132. {Object.keys(this.props.uploadConfig).length === 0 ? "" : uploadButton}
  133. </Upload>
  134. </Spin>
  135. </div>
  136. {fileList.length > 10 && Object.keys(this.props.uploadConfig).length === 0 ? <div className='seeMore' onClick={()=>{this.setState({seeMore:!this.state.seeMore})}}>
  137. {this.state.seeMore ? '收起' : '查看更多'}
  138. </div> : <div/>}
  139. <ul id={this.props.domId} style={{display:'none',zIndex:-1,}}>
  140. {
  141. fileList.map((value,index)=>(
  142. <li key={index}>
  143. <img src={value.url || value.thumbUrl} onLoad={()=>{
  144. this.imgLoading();
  145. }}/>
  146. </li>
  147. ))
  148. }
  149. </ul>
  150. </div>
  151. )
  152. }
  153. }
  154. ImgList.propTypes = {
  155. domId : PropTypes.string, //如果一个界面有多个该组件,需要传入自定义id名称,用于图片列表显示
  156. fileList: PropTypes.array.isRequired, //图片文件列表
  157. uploadConfig: PropTypes.object, //上传图片配置参数(需要上传图片时需要,存在与否,用于区分是否为上传或者为查看)
  158. onChange: PropTypes.func, //选择图片
  159. }
  160. ImgList.defaultProps = {
  161. domId: 'images',
  162. fileList: [],
  163. uploadConfig: {},
  164. onChange:()=>{}
  165. }
  166. export default ImgList;