bannerForm.jsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React from 'react';
  2. import {
  3. Icon,
  4. Modal,
  5. message,
  6. Spin,
  7. Upload,
  8. Input,
  9. Button,
  10. Radio,
  11. Form
  12. } from 'antd';
  13. import { splitUrl} from '@/tools.js';
  14. const PicturesWall = React.createClass({
  15. getInitialState() {
  16. return {
  17. previewVisible: false,
  18. previewImage: '',
  19. fileList: [],
  20. tags: []
  21. };
  22. },
  23. handleCancel() {
  24. this.setState({ previewVisible: false });
  25. },
  26. handlePreview(file) {
  27. this.setState({
  28. previewImage: file.url || file.thumbUrl,
  29. previewVisible: true
  30. });
  31. },
  32. handleChange(info) {
  33. let fileList = info.fileList;
  34. this.setState({ fileList });
  35. this.props.fileList(fileList);
  36. },
  37. componentWillReceiveProps(nextProps) {
  38. this.state.fileList = nextProps.pictureUrl;
  39. },
  40. render() {
  41. const { previewVisible, previewImage, fileList } = this.state;
  42. const uploadButton = (
  43. <div>
  44. <Icon type="plus" />
  45. <div className="ant-upload-text">点击上传</div>
  46. </div>
  47. );
  48. return (
  49. <div className="clearfix">
  50. <Upload
  51. action={globalConfig.context + '/api/admin/banners/uploadPicture'}
  52. data={{ sign: 'banners'}}
  53. listType="picture-card"
  54. fileList={fileList}
  55. onPreview={this.handlePreview}
  56. onChange={this.handleChange}
  57. >
  58. {fileList.length >= 1 ? null : uploadButton}
  59. </Upload>
  60. <Modal maskClosable={false} visible={previewVisible} footer={null} onCancel={this.handleCancel}>
  61. <img alt="example" style={{ width: '100%' }} src={previewImage} />
  62. </Modal>
  63. </div>
  64. );
  65. }
  66. });
  67. class BannerForm extends React.Component {
  68. constructor(props) {
  69. super(props);
  70. this.state = {
  71. loading: false,
  72. imgUrl: [],
  73. visible:false
  74. };
  75. }
  76. cancelFun(){
  77. this.props.closeDesc();
  78. }
  79. loadData(id) {
  80. this.setState({
  81. loading: true
  82. });
  83. $.ajax({
  84. method: "get",
  85. dataType: "json",
  86. crossDomain: false,
  87. url: globalConfig.context + '/api/admin/banners/details' ,
  88. data: {
  89. id: id
  90. },
  91. success: function (data) {
  92. let thisData = data.data;
  93. if (!thisData) {
  94. if (data.error && data.error.length) {
  95. message.warning(data.error[0].message);
  96. };
  97. thisData = {};
  98. };
  99. this.setState({
  100. id: thisData.id, //品类ID
  101. data:thisData,
  102. imgUrl: thisData.imgUrl ? splitUrl(thisData.imgUrl, ',', globalConfig.avatarHost + '/upload') : [],
  103. });
  104. }.bind(this),
  105. }).always(function () {
  106. this.setState({
  107. loading: false
  108. });
  109. }.bind(this));
  110. }
  111. handleSubmit(e){
  112. e.preventDefault();
  113. this.props.form.validateFields((err, values) => {
  114. //url转化
  115. let thePictureUrl = [];
  116. if (this.state.imgUrl.length) {
  117. let picArr = [];
  118. this.state.imgUrl.map(function (item) {
  119. if ( item.response && item.response.data && item.response.data.length ){
  120. picArr.push(item.response.data);
  121. }
  122. });
  123. thePictureUrl = picArr.join(",");
  124. }else{
  125. message.warning('请上传图片!');
  126. return false;
  127. };
  128. if (!err) {
  129. this.setState({
  130. loading: true
  131. });
  132. $.ajax({
  133. method: "post",
  134. dataType: "json",
  135. async:true,
  136. url: this.props.data.id ? globalConfig.context + '/api/admin/banners/update' : globalConfig.context + '/api/admin/banners/save',
  137. data: {
  138. id:this.props.data.id,
  139. text:values.text,
  140. forwardUrl:values.forwardUrl,
  141. apiUrl:values.apiUrl,
  142. imgUrl:thePictureUrl.length?thePictureUrl:'',
  143. deleteSign:false,
  144. client:values.client
  145. }
  146. }).done(function (data) {
  147. this.setState({
  148. loading: false
  149. });
  150. if (!data.error.length) {
  151. message.success('操作成功!');
  152. this.setState({
  153. visible: false
  154. });
  155. this.props.handOk();
  156. } else {
  157. message.warning(data.error[0].message);
  158. }
  159. }.bind(this));
  160. }
  161. })
  162. }
  163. componentWillMount() {
  164. if (this.props.data&&this.props.data.id) {
  165. this.loadData(this.props.data.id);
  166. } else {
  167. this.state.data = {};
  168. this.state.imgUrl = [];
  169. };
  170. }
  171. componentWillReceiveProps(nextProps) {
  172. if (!this.props.visible && nextProps.visible) {
  173. this.state.textFileList = [];
  174. this.state.videoFileList = [];
  175. if (nextProps.data.id) {
  176. this.loadData(nextProps.data.id);
  177. } else {
  178. this.state.data = {};
  179. this.state.imgUrl = [];
  180. };
  181. this.props.form.resetFields();
  182. };
  183. }
  184. render() {
  185. const { getFieldDecorator } = this.props.form;
  186. const FormItem = Form.Item;
  187. const formItemLayout = {
  188. labelCol: { span: 6 },
  189. wrapperCol: { span: 12 },
  190. };
  191. let theData = this.state.data||{};
  192. return (
  193. <div className="patent-desc">
  194. <Form layout="horizontal" onSubmit={this.handleSubmit.bind(this)} id="demand-form">
  195. <Spin spinning={this.state.loading}>
  196. <div className="clearfix">
  197. <FormItem {...formItemLayout} label="轮播图描述">
  198. {getFieldDecorator('text', {
  199. rules: [ { required: true, message: '此项为必填项!' } ],
  200. initialValue: theData.text
  201. })(<Input placeholder="请输入轮播图名称"/>)}
  202. </FormItem>
  203. </div>
  204. <div className="clearfix">
  205. <FormItem {...formItemLayout} label="跳转地址">
  206. {getFieldDecorator('forwardUrl', {
  207. initialValue: theData.forwardUrl
  208. })(<Input placeholder="请输入跳转地址"/>)}
  209. </FormItem>
  210. </div>
  211. <div className="clearfix">
  212. <FormItem {...formItemLayout} label="api">
  213. {getFieldDecorator('apiUrl', {
  214. rules: [ { required: true, message: '此项为必填项!' } ],
  215. initialValue: theData.apiUrl
  216. })(<Input placeholder="请输入api"/>)}
  217. </FormItem>
  218. </div>
  219. <div className="clearfix">
  220. <FormItem {...formItemLayout} label="位置">
  221. {getFieldDecorator('client', {
  222. rules: [ { required: true, message: '此项为必填项!' } ],
  223. initialValue: theData.client
  224. })(
  225. <Radio.Group>
  226. <Radio value={0}>网站</Radio>
  227. <Radio value={1}>App</Radio>
  228. </Radio.Group>
  229. )
  230. }
  231. </FormItem>
  232. </div>
  233. <div className="clearfix">
  234. <FormItem labelCol={{ span: 6 }} wrapperCol={{ span: 18 }} label={<span><strong style={{color:"#f00"}}> * </strong>轮播图</span>}>
  235. <PicturesWall
  236. fileList={(e)=>{this.setState({imgUrl:e})}}
  237. pictureUrl={this.state.imgUrl}
  238. visible={this.props.visible}
  239. />
  240. </FormItem>
  241. </div>
  242. <div className="clearfix">
  243. <FormItem wrapperCol={{ span: 12, offset: 6 }}>
  244. <Button className="set-submit" type="primary" htmlType="submit">
  245. 保存
  246. </Button>
  247. <Button className="set-submit" type="ghost" onClick={this.cancelFun.bind(this)}>
  248. 取消
  249. </Button>
  250. </FormItem>
  251. </div>
  252. </Spin>
  253. </Form>
  254. </div>
  255. );
  256. }
  257. }
  258. export default Form.create()(BannerForm);