index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import React, { Component } from 'react';
  2. import { Icon, message, Spin, Upload, Progress, Modal } from 'antd';
  3. import $ from 'jquery/src/ajax';
  4. import './index.less';
  5. import Viewer from 'viewerjs';
  6. import 'viewerjs/dist/viewer.css';
  7. import PropTypes from 'prop-types';
  8. import pdfimg from '../../../../image/pdf_logo.png';
  9. const uploadButton = (
  10. <div>
  11. <Icon type="plus" />
  12. <div className="ant-upload-text" style={{ cursor: 'pointer' }}>点击上传</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. uploadLoading: false,
  24. percent: 0,
  25. newUploadArr: [],
  26. newUploadInforArr: {},
  27. isPDF: false,
  28. file: {},
  29. }
  30. this.imgLoading = this.imgLoading.bind(this);
  31. this.beforeUpload = this.beforeUpload.bind(this);
  32. this.onRemove = this.onRemove.bind(this);
  33. this.setTimeOutObj = null;
  34. }
  35. componentDidMount() {
  36. this.setTimeOutObj = setTimeout(() => {
  37. this.setState({
  38. gallery: new Viewer(document.getElementById(this.props.domId))
  39. }, () => {
  40. this.setState({
  41. loading: false
  42. })
  43. })
  44. }, 10000)
  45. }
  46. imgLoading() {
  47. if (Object.keys(this.props.uploadConfig).length !== 0) {
  48. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  49. } else {
  50. let num = this.state.imgLoadNum + 1;
  51. if (num >= this.props.fileList.length) {
  52. this.state.gallery && this.state.gallery.destroy();
  53. this.setState({
  54. gallery: new Viewer(document.getElementById(this.props.domId))
  55. }, () => {
  56. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  57. this.setState({
  58. loading: false
  59. })
  60. })
  61. }
  62. this.setState({
  63. imgLoadNum: num
  64. })
  65. }
  66. }
  67. beforeUpload(file) {
  68. const { isSupportPDF = false } = this.props
  69. const isLt2M = file.size / 1024 / 1024 < 20;
  70. if (!isLt2M) {
  71. message.error('文件大小不能超过 20MB!');
  72. return isLt2M;
  73. }
  74. if (this.props.accept) {
  75. if (isLt2M) {
  76. this.state.newUploadArr.push(file)
  77. this.setState({
  78. newUploadArr: this.state.newUploadArr
  79. })
  80. }
  81. return isLt2M;
  82. } else {
  83. const isJPG = file.type.split('/');
  84. if (isSupportPDF) {
  85. if (isJPG[0] !== 'image' && isJPG[0] !== "application") {
  86. message.error('只能上传图片或pdf文件!');
  87. return false;
  88. }
  89. } else {
  90. if (isJPG[0] !== 'image') {
  91. message.error('只能上传图片!');
  92. return false;
  93. }
  94. }
  95. if (isJPG && isLt2M) {
  96. // 用于计算当次选择的图片数量
  97. this.state.newUploadArr.push(file)
  98. this.setState({
  99. newUploadArr: this.state.newUploadArr
  100. })
  101. }
  102. return isJPG && isLt2M;
  103. }
  104. }
  105. // 删除绑定的图片
  106. async onRemove(info) {
  107. const { orderNo, bindId, sign, deleteApi } = this.props
  108. if (!deleteApi) {
  109. return
  110. }
  111. let name = info.response.data
  112. let remove = new Promise(function (resolve, reject) {
  113. Modal.confirm({
  114. title: '操作确认',
  115. content: '是否确认删除?',
  116. okText: '确定',
  117. cancelText: '取消',
  118. onOk() {
  119. $.ajax({
  120. method: "post",
  121. dataType: "json",
  122. crossDomain: false,
  123. url: globalConfig.context + deleteApi,
  124. data: {
  125. id: bindId, // 编号 (没有不用传)
  126. orderNo, // 绑定的订单号
  127. sign,
  128. fileName: name
  129. }
  130. }).done(function (data) {
  131. if (!data.error.length) {
  132. resolve(true);
  133. } else {
  134. };
  135. }.bind(this));
  136. },
  137. onCancel() {
  138. resolve(false)
  139. },
  140. })
  141. });
  142. return await remove;
  143. }
  144. componentWillUnmount() {
  145. this.setTimeOutObj && clearTimeout(this.setTimeOutObj);
  146. }
  147. render() {
  148. const { fileList } = this.props;
  149. if (!fileList || !Array.isArray(fileList)) {
  150. return null;
  151. }
  152. return (
  153. <div className='ImgListComponent'>
  154. <div className='ImgList'>
  155. <Spin spinning={Object.keys(this.props.uploadConfig).length !== 0 ? false : (fileList.length === 0 ? false : this.state.loading)} tip="文件加载中...">
  156. <Upload
  157. {...this.props.uploadConfig}
  158. accept={this.props.accept || "image/*,application/pdf"}
  159. className={Object.keys(this.props.uploadConfig).length !== 0 ? "" : "demandDetailShow-upload"}
  160. listType={this.props.accept ? this.props.uploadConfig.listType : "picture-card"}
  161. fileList={
  162. Object.keys(this.props.uploadConfig).length !== 0 ?
  163. (
  164. fileList.map((v, i) => {
  165. v.index = i;
  166. v.url = v.url;
  167. v.thumbUrl = v.name.substr(v.name.lastIndexOf(".") + 1) == "pdf" ? pdfimg : v.url
  168. return v
  169. })
  170. ) : (
  171. this.state.seeMore ?
  172. fileList.map((v, i) => {
  173. v.index = i;
  174. v.url = v.url;
  175. v.thumbUrl = v.name.substr(v.name.lastIndexOf(".") + 1) == "pdf" ? pdfimg : v.url
  176. return v
  177. }) :
  178. fileList.map((v, i) => {
  179. v.index = i;
  180. v.url = v.url;
  181. v.thumbUrl = v.name.substr(v.name.lastIndexOf(".") + 1) == "pdf" ? pdfimg : v.url
  182. return v
  183. }).slice(0, 10)
  184. )
  185. }
  186. onPreview={(file) => {
  187. let types = file.name.substr(file.name.lastIndexOf(".") + 1)
  188. if (!!file.type && file.type.split('/')[0] == "application" || types == "pdf") {
  189. let url = file.response.data.indexOf("/upload") == -1
  190. ? globalConfig.avatarHost + "/upload" + file.response.data
  191. : file.response.data
  192. // this.setState({
  193. // isPDF: true,
  194. // file: url,
  195. // })
  196. window.open(url)
  197. } else {
  198. if (Object.keys(this.props.uploadConfig).length !== 0) {
  199. //TODO 上传组件,查看图片临时解决方法,无法查看下一张
  200. let image = new Image();
  201. image.src = file.url || file.thumbUrl;
  202. let viewer = new Viewer(image, {
  203. hidden: function () {
  204. viewer.destroy();
  205. },
  206. });
  207. viewer.show();
  208. } else {
  209. this.state.gallery.view(file.index).show();
  210. }
  211. }
  212. }}
  213. beforeUpload={this.beforeUpload}
  214. onChange={(infor) => {
  215. if (infor.file.status) {
  216. let newUploadNum = this.state.newUploadArr.length
  217. this.state.gallery && this.state.gallery.destroy();
  218. this.props.onChange(infor);
  219. if (infor.file && infor.file.status === 'error') {
  220. message.error('上传失败,请重新上传!');
  221. }
  222. //用于控制进度条
  223. if (infor.file && infor.file.status !== 'removed' && infor.event) {
  224. let percent = 0;
  225. this.state.newUploadInforArr[infor.file.uid] = infor.event.percent;
  226. this.setState({
  227. newUploadInforArr: this.state.newUploadInforArr
  228. })
  229. for (let i of Object.values(this.state.newUploadInforArr)) {
  230. percent += i
  231. }
  232. let percentNum = percent / newUploadNum / 100;
  233. this.setState({
  234. percent: percentNum
  235. })
  236. if (percentNum <= 1) {
  237. this.setState({
  238. uploadLoading: true,
  239. })
  240. }
  241. if (percentNum >= 1) {
  242. this.setState({
  243. percent: 1,
  244. }, () => {
  245. setTimeout(() => {
  246. this.setState({
  247. uploadLoading: false,
  248. }, () => {
  249. this.setState({
  250. percent: 0,
  251. newUploadArr: [],
  252. newUploadInforArr: {}
  253. })
  254. })
  255. }, 1500)
  256. })
  257. }
  258. } else if (infor.file && infor.file.status === 'removed') {
  259. this.setState({
  260. percent: 0,
  261. newUploadArr: [],
  262. newUploadInforArr: {}
  263. })
  264. }
  265. }
  266. }}
  267. onRemove={this.onRemove}
  268. >
  269. {Object.keys(this.props.uploadConfig).length === 0 ? "" : uploadButton}
  270. </Upload>
  271. </Spin>
  272. </div>
  273. {fileList.length > 10 && Object.keys(this.props.uploadConfig).length === 0
  274. ? <div className='seeMore' onClick={() => { this.setState({ seeMore: !this.state.seeMore }) }}>
  275. {this.state.seeMore ? '收起' : '查看更多'}
  276. </div>
  277. : <div />}
  278. <ul id={this.props.domId} style={{ display: 'none', zIndex: -1, }}>
  279. {
  280. fileList.map((value, index) => (
  281. <li key={index}>
  282. <img src={value.name.substr(value.name.lastIndexOf(".") + 1) == "pdf" ? pdfimg : (value.url || value.thumbUrl)} onLoad={() => {
  283. this.imgLoading();
  284. }} />
  285. </li>
  286. ))
  287. }
  288. </ul>
  289. {this.state.uploadLoading ? <div className='mask'>
  290. <Progress
  291. className='progress'
  292. type="circle"
  293. status="active"
  294. percent={parseFloat(this.state.percent * 100).toFixed(0)}
  295. />
  296. <div className='promptText'>文件上传中...</div>
  297. </div> : <div />}
  298. {
  299. // PDF文件预览弹窗
  300. this.state.isPDF &&
  301. <Modal
  302. // maskClosable={false}
  303. wrapClassName={"pdfmodal"}
  304. visible={this.state.isPDF}
  305. className="admin-desc-content"
  306. footer=""
  307. title=""
  308. width="1200px"
  309. onCancel={() => {
  310. this.setState({
  311. isPDF: false,
  312. file: "",
  313. })
  314. }}
  315. >
  316. <embed src={this.state.file} type="application/pdf" style={{ width: "100%", height: 700 }} />
  317. <div className='no'></div>
  318. </Modal>
  319. }
  320. </div>
  321. )
  322. }
  323. }
  324. ImgList.propTypes = {
  325. domId: PropTypes.string, //如果一个界面有多个该组件,需要传入自定义id名称,用于图片列表显示
  326. fileList: PropTypes.array.isRequired, //图片文件列表
  327. uploadConfig: PropTypes.object, //上传图片配置参数(需要上传图片时需要,存在与否,用于区分是否为上传或者为查看)
  328. onChange: PropTypes.func, //选择图片
  329. }
  330. ImgList.defaultProps = {
  331. domId: 'images',
  332. fileList: [],
  333. uploadConfig: {},
  334. onChange: () => { }
  335. }
  336. export default ImgList;