PictureSingleUpload.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { PureComponent, Fragment } from 'react';
  2. import { Form, Input, Button, Alert } from 'antd';
  3. import { connect } from 'dva';
  4. import { routerRedux } from 'dva/router';
  5. import Uploader from '../../../components/AXUpload';
  6. import { Hotax } from '../../../utils/config';
  7. const formItemLayout = {
  8. labelCol: {
  9. xs: { span: 24 },
  10. sm: { span: 6 },
  11. },
  12. wrapperCol: {
  13. xs: { span: 24 },
  14. sm: { span: 14 },
  15. md: { span: 12 },
  16. },
  17. };
  18. const submitFormLayout = {
  19. wrapperCol: {
  20. xs: { span: 24, offset: 0 },
  21. sm: { span: 12, offset: 6 },
  22. },
  23. };
  24. function picNameFormat(name) {
  25. if (!name) return name;
  26. const separatorIndex = name.lastIndexOf('.');
  27. if (separatorIndex !== -1) {
  28. return name.substring(0, separatorIndex);
  29. }
  30. return name;
  31. }
  32. @Form.create()
  33. @connect(({ loading }) => ({
  34. submitting: loading.models.resource,
  35. }))
  36. export default class PictureSingleUpload extends PureComponent {
  37. state = {
  38. fileList: [],
  39. code: null,
  40. };
  41. handleCodeInputChange = (e) => {
  42. this.setState({ code: e.target.value });
  43. };
  44. handleOnChangeEvent = (fileList) => {
  45. this.setState({ fileList });
  46. return fileList;
  47. };
  48. handlePageBack = () => {
  49. const { UIParams, Queryers, isCard } = this.props.location.state || {};
  50. this.props.dispatch(
  51. routerRedux.push({
  52. pathname: '/resource/picture',
  53. state: {
  54. isCard,
  55. UIParams,
  56. Queryers,
  57. },
  58. })
  59. );
  60. };
  61. handlePageSubmit = (e) => {
  62. e.preventDefault();
  63. this.props.form.validateFieldsAndScroll((err, values) => {
  64. if (!err) {
  65. const { fileList, ...params } = values;
  66. if (Array.isArray(fileList) && fileList.length) {
  67. params.url = fileList[0].url;
  68. }
  69. params.type = Hotax.RESOURCE_IMAGE;
  70. params.status = Hotax.STATUS_NORMAL;
  71. this.props.dispatch({
  72. type: 'resource/createImage',
  73. payload: params,
  74. });
  75. }
  76. });
  77. };
  78. render() {
  79. const { form, submitting } = this.props;
  80. const { getFieldDecorator } = form;
  81. const { fileList, code } = this.state;
  82. const firstPicture = fileList[0] || {};
  83. const { type, size, path, name } = firstPicture;
  84. return (
  85. <Form onSubmit={this.handlePageSubmit}>
  86. <Form.Item {...submitFormLayout}>
  87. <Alert
  88. showIcon
  89. type="warning"
  90. message={
  91. <Fragment>
  92. <p>1.上传图片前应先填写符合规范的图片编号,否则不能上传图片;</p>
  93. <p>2.上传成功后会自动生成图片名称、大小、格式、路径等信息,无需手动填写;</p>
  94. </Fragment>
  95. }
  96. />
  97. </Form.Item>
  98. <Form.Item label="图片编号" hasFeedback {...formItemLayout}>
  99. {getFieldDecorator('code', {
  100. rules: [{
  101. required: true, message: '编号不能为空!',
  102. }, {
  103. pattern: /^[a-zA-Z0-9|-]+$/ig, message: '编号格式错误!',
  104. }],
  105. })(
  106. <Input onChange={this.handleCodeInputChange} />
  107. )}
  108. </Form.Item>
  109. <Form.Item label="图片上传" {...formItemLayout}>
  110. {getFieldDecorator('fileList', {
  111. getValueFromEvent: this.handleOnChangeEvent,
  112. })(
  113. <Uploader
  114. forbidden={
  115. !((code && /^[a-zA-Z0-9|-]+$/ig.test(code)))
  116. }
  117. fileCode={code}
  118. fileList={fileList}
  119. accept="image"
  120. multiple={false}
  121. totalLimit={1}
  122. />
  123. )}
  124. </Form.Item>
  125. <Form.Item label="图片名称" hasFeedback {...formItemLayout}>
  126. {getFieldDecorator('name', {
  127. rules: [{ required: true, message: '名称不能为空!' }],
  128. initialValue: picNameFormat(name),
  129. })(
  130. <Input />
  131. )}
  132. </Form.Item>
  133. <Form.Item label="图片路径" {...formItemLayout}>
  134. {getFieldDecorator('path', {
  135. initialValue: path,
  136. })(
  137. <Input disabled />
  138. )}
  139. </Form.Item>
  140. <Form.Item label="图片格式" {...formItemLayout}>
  141. {getFieldDecorator('format', {
  142. initialValue: type ? type.split('/')[1] : '',
  143. })(
  144. <Input disabled />
  145. )}
  146. </Form.Item>
  147. <Form.Item label="图片大小" {...formItemLayout}>
  148. {getFieldDecorator('size', {
  149. initialValue: size,
  150. })(
  151. <Input disabled />
  152. )}
  153. </Form.Item>
  154. <Form.Item {...submitFormLayout} style={{ marginTop: 32 }}>
  155. <Button onClick={this.handlePageBack}>取消</Button>
  156. <Button
  157. type="primary"
  158. htmlType="submit"
  159. loading={submitting}
  160. style={{ marginLeft: 8 }}
  161. >提交
  162. </Button>
  163. </Form.Item>
  164. </Form>
  165. );
  166. }
  167. }