ImgModalChoose.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { connect } from 'dva/index';
  2. import React, {Component, Fragment} from 'react';
  3. import {Modal,Empty} from 'antd';
  4. import global from "@/global.less";
  5. import {failTip, list_com_page_more} from "@/utils/utils";
  6. import product from "@/pages/goods/models/product";
  7. let sthis = '';
  8. @connect(({ store,product }) => ({
  9. store,product
  10. }))
  11. export default class SldPreviewImg extends Component{
  12. constructor(props){
  13. super(props);
  14. this.state = {
  15. chooseKey:[],
  16. }
  17. }
  18. componentDidMount(){
  19. }
  20. //图片选择
  21. chooseItem = (val,index) => {
  22. let { chooseKey } = this.state;
  23. //初次选择
  24. let a = chooseKey.find(item=>item==index)
  25. if(a){
  26. for(let i in chooseKey){
  27. if(chooseKey[i] == a){
  28. chooseKey.splice(i,1)
  29. }
  30. }
  31. }else{
  32. chooseKey.push(index)
  33. }
  34. this.setState({chooseKey: chooseKey});
  35. }
  36. //slodon_预览关闭modal
  37. imgModalhandleCancel = () => {
  38. this.setState({
  39. chooseKey:[]
  40. });
  41. this.props.imgModalhandleCancel();
  42. }
  43. imgModalhandleOk = () => {
  44. let {chooseKey} = this.state
  45. let {img_data,max_length,imgModalListProps} = this.props
  46. let data = []
  47. let img_data_back = Object.assign(img_data)
  48. for(let i in chooseKey){
  49. data.push(imgModalListProps[chooseKey[i] - 1] )
  50. }
  51. //初始化图片数据
  52. data.some(item => {
  53. let img_info = {};
  54. img_info.uid = item.id+new Date().getTime();
  55. img_info.thumbUrl = item.url;//图片的url地址
  56. img_info.name = item.name;
  57. img_info.status = 'done';
  58. img_info.response = {};
  59. img_info.response.state = 200;
  60. img_info.response.data = {
  61. path: item.path,
  62. url: item.url,//图片的url地址
  63. };
  64. if(img_data_back.fileList.length >= max_length){
  65. failTip('最多上传'+ max_length +'个资源!');
  66. return true
  67. }else{
  68. img_data_back.fileList.push(img_info);
  69. this.imgModalhandleCancel()
  70. this.props.imgModalhandleOk(img_data_back.fileList);
  71. }
  72. });
  73. }
  74. render(){
  75. const { show_choose_modal,imgModalListProps,modal_width} = this.props;
  76. const {chooseKey} = this.state
  77. return <Modal
  78. title="请选择资源"
  79. width={modal_width}
  80. visible={show_choose_modal}
  81. onOk={this.imgModalhandleOk}
  82. onCancel={this.imgModalhandleCancel}
  83. >
  84. {imgModalListProps.length > 0 && imgModalListProps.map((item, key) =>
  85. <div key={key} className={ chooseKey.find(item=>item==key+1) ? `${global.chooseWrapActive}` : `${global.chooseWrap}`} onClick={() => this.chooseItem(item,key+1)} >
  86. {item.fileType == '.jpg' &&
  87. <div className={global.item} >
  88. <img src={item.url} style={{width:'100%'}} />
  89. </div>
  90. }
  91. {item.fileType == 'video' &&
  92. <div className={global.file} >
  93. <a href={item.path} target={'_blank'}>{item.path}</a>
  94. </div>
  95. }
  96. {item.type == 'file' &&
  97. <div className={global.file} >
  98. <a>文件类型:{item.fileType}</a>
  99. </div>
  100. }
  101. <p className={global.file_name}>资源名称:{item.name}</p>
  102. </div>
  103. )}
  104. {imgModalListProps.length == 0 &&
  105. <Empty />
  106. }
  107. </Modal>;
  108. }
  109. }