FormEnclosure.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Created by RaoMeng on 2020/2/19
  3. * Desc: 通用表单附件类型
  4. */
  5. import React, { Component } from 'react'
  6. import './formCommon.css'
  7. import BillModel from '../../model/BillModel'
  8. import { Upload, Button, Icon } from 'antd'
  9. import { isObjNull } from '../../utils/common'
  10. import { Toast } from 'antd-mobile'
  11. let uploadFail = false
  12. export default class FormEnclosure extends Component {
  13. constructor () {
  14. super()
  15. this.state = {
  16. billModel: new BillModel(),
  17. }
  18. }
  19. componentDidMount () {
  20. let { billModel } = this.props
  21. this.setState({
  22. billModel: billModel,
  23. })
  24. }
  25. componentWillUnmount () {
  26. }
  27. componentWillReceiveProps () {
  28. this.componentDidMount()
  29. }
  30. render () {
  31. const { billModel } = this.state
  32. let valueItem = this.getEnclosureCom(billModel)
  33. return (
  34. this.renderTwoLines(
  35. billModel,
  36. valueItem))
  37. }
  38. /**
  39. * 加载标题和内容单独分行
  40. * @param billModel
  41. * @param valueItem
  42. * @returns {*}
  43. */
  44. renderTwoLines (billModel, valueItem) {
  45. return <div className={'form-textarea-layout'}>
  46. <div className='form-common-layout'
  47. style={{ borderBottom: 'none' }}>
  48. <div style={{
  49. minWidth: '90px',
  50. fontSize: '14px',
  51. color: '#333333',
  52. }}>{billModel.caption}</div>
  53. <div className={billModel.allowBlank == 'F'
  54. ? 'form-input-fill'
  55. : 'visibleHidden'}>*
  56. </div>
  57. </div>
  58. {valueItem}
  59. </div>
  60. }
  61. /**
  62. * 附件类型
  63. * @param billModel
  64. */
  65. getEnclosureCom = (billModel) => {
  66. return <div style={{
  67. margin: '4px 10px 10px',
  68. }}>
  69. <Upload
  70. action={this.props.baseUrl + '/mobile/uploadAttachs.action'}
  71. listType={'picture'}
  72. multiple={true}
  73. fileList={billModel.fileList ? billModel.fileList : []}
  74. showUploadList={true}
  75. withCredentials={true}
  76. beforeUpload={this.beforeUpload}
  77. onChange={this.handleChange}
  78. // onPreview={this.handlePreview}
  79. // onRemove={this.handleRemove}
  80. // onDownload={() => {}}
  81. // data={''}
  82. method={'post'}
  83. className={'upload-list-inline'}
  84. >
  85. <div style={{ display: 'flex', alignItems: 'center' }}>
  86. <div
  87. className={'uploadBtn'}>
  88. <Icon type="upload"
  89. style={{ color: 'white' }}/>
  90. <span style={{ fontSize: '12px', marginLeft: '6px' }}>选择文件</span>
  91. </div>
  92. <span className='promptText'>(不能超过100MB)</span>
  93. </div>
  94. </Upload>
  95. </div>
  96. }
  97. handleChange = ({ fileList }) => {
  98. console.log('filelist', fileList)
  99. if (uploadFail) {
  100. return
  101. }
  102. const { count } = this.props
  103. const { billModel } = this.state
  104. if (isObjNull(count) || fileList.length <= count) {
  105. //{"success":true,"id":"[52650]"}
  106. /*let value = ''
  107. if (fileList) {
  108. fileList.forEach((file, index) => {
  109. file.enclosureId = (file.response && file.response.id)
  110. ? file.response.id
  111. : ''
  112. })
  113. }
  114. billModel.fileList = fileList
  115. this.setState({ billModel })
  116. this.props.onTextChange &&
  117. this.props.onTextChange(this.props.groupIndex, this.props.childIndex,
  118. value)*/
  119. }
  120. }
  121. handleRemove = (file) => {
  122. if (this.props.handleRemove) {
  123. return this.props.handleRemove(file)
  124. }
  125. }
  126. beforeUpload = (file, fileList) => {
  127. uploadFail = false
  128. if (file.size && file.size > 100 * 1024 * 1024) {
  129. uploadFail = false
  130. Toast.fail('文件大小不能超过100M')
  131. return false
  132. }
  133. const { count } = this.props
  134. const { billModel } = this.state
  135. if (count && billModel.fileList &&
  136. ((billModel.fileList.length + fileList.length) > count)) {
  137. Toast.fail(`上传失败,附件数量不能超过${count}`)
  138. uploadFail = true
  139. return false
  140. } else {
  141. return this.props.beforeUpload
  142. ? this.props.beforeUpload(file, fileList)
  143. : true
  144. }
  145. }
  146. handlePreview = (file) => {
  147. }
  148. }