proForm.jsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React from 'react';
  2. import $ from 'jquery/src/ajax';
  3. import {
  4. message,
  5. Spin,
  6. Input,
  7. Button,
  8. Radio,
  9. Form,
  10. Select,
  11. } from 'antd';
  12. import {productType} from '@/dataDic.js';
  13. import {getProStatus} from '@/tools.js'
  14. import Editors from '@/richTextEditors';
  15. import Crop from '@/crop/cropBlock';
  16. import KeyWordTagGroup from '@/crop/keyword';
  17. const NewDetailForm = Form.create()(React.createClass({
  18. getInitialState() {
  19. return {
  20. visible: false,
  21. loading: false,
  22. auditStatus: 0,
  23. textFileList: [],
  24. videoFileList: [],
  25. pictureUrl: [],
  26. tags:[],
  27. edit:{},
  28. edits:{}
  29. };
  30. },
  31. loadData(id) {
  32. this.setState({
  33. loading: true
  34. });
  35. $.ajax({
  36. method: "get",
  37. dataType: "json",
  38. crossDomain: false,
  39. url: globalConfig.context + '/api/admin/community/getCommunintyById' ,
  40. data: {
  41. id: id
  42. },
  43. success: function (data) {
  44. let thisData = data.data;
  45. if (!thisData) {
  46. if (data.error && data.error.length) {
  47. message.warning(data.error[0].message);
  48. };
  49. thisData = {};
  50. };
  51. this.setState({
  52. id:thisData.id,
  53. data: thisData,
  54. tags:(thisData.keyword).split(',')||[],
  55. status:thisData.status,
  56. edit:thisData.content,
  57. pictureUrl: thisData.picUrl ? thisData.picUrl.split(',') : []
  58. });
  59. }.bind(this),
  60. }).always(function () {
  61. this.setState({
  62. loading: false
  63. });
  64. }.bind(this));
  65. },
  66. handleSubmit(e,index) {
  67. e.preventDefault();
  68. this.props.form.validateFields((err, values) => {
  69. if (!this.state.pictureUrl.length) {
  70. message.warning('请上传专栏图片!');
  71. return false;
  72. };
  73. if((this.state.edit)==undefined||!this.state.edit||(this.state.edit)=='<p><br></p>'){
  74. message.warning('请填写专栏正文');
  75. return false;
  76. }
  77. if (!err) {
  78. this.setState({
  79. loading: true
  80. });
  81. $.ajax({
  82. method: "post",
  83. dataType: "json",
  84. async:true,
  85. url: this.props.data.id ? globalConfig.context + '/api/admin/community/updateCommunity' : globalConfig.context + '/api/admin/community/insertCommunity',
  86. data: {
  87. id: this.props.data.id,
  88. title: values.title,
  89. picUrl: this.state.pictureUrl.join(','),
  90. content:this.state.edit,
  91. category:values.category,
  92. recommend:values.recommend,
  93. top:values.top,
  94. source:values.source,
  95. status:index,
  96. keyword: this.state.tags ? this.state.tags.join(",") : '',
  97. summary: values.summary,
  98. }
  99. }).done(function (data) {
  100. this.setState({
  101. loading: false
  102. });
  103. if (!data.error.length) {
  104. message.success('操作成功!');
  105. this.setState({
  106. visible: false
  107. });
  108. this.props.handOk();
  109. } else {
  110. message.warning(data.error[0].message);
  111. }
  112. }.bind(this));
  113. }
  114. });
  115. },
  116. cancelFun(){
  117. this.props.closeDesc();
  118. },
  119. componentWillMount() {
  120. if (this.props.data.id) {
  121. this.loadData(this.props.data.id);
  122. } else {
  123. this.state.visible=true;
  124. this.state.data = {};
  125. this.state.tags=[];
  126. this.state.edit='<p><br></p>';
  127. this.state.pictureUrl = [];
  128. };
  129. },
  130. componentWillReceiveProps(nextProps) {
  131. if (!this.props.visible && nextProps.visible) {
  132. this.state.textFileList = [];
  133. this.state.videoFileList = [];
  134. if (nextProps.data.id) {
  135. this.loadData(nextProps.data.id);
  136. } else {
  137. this.state.visible=true;
  138. this.state.data = {};
  139. this.state.tags=[];
  140. this.state.edit='<p><br></p>';
  141. this.state.pictureUrl = [];
  142. this.state.theData={};
  143. };
  144. this.props.form.resetFields();
  145. };
  146. },
  147. render() {
  148. const theData = this.state.data || {};
  149. const { getFieldDecorator } = this.props.form;
  150. const FormItem = Form.Item
  151. const formItemLayout = {
  152. labelCol: { span: 6 },
  153. wrapperCol: { span: 12 },
  154. };
  155. return (
  156. <div className='login'>
  157. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  158. <Spin spinning={this.state.loading}>
  159. <div className="clearfix">
  160. <FormItem className="half-item" {...formItemLayout} label="标题">
  161. {getFieldDecorator('title', {
  162. rules: [ { required: true, message: '此项为必填项!' } ],
  163. initialValue: theData.title
  164. })(<Input placeholder="请输入您的专栏标题" maxLength={40}/>)}
  165. </FormItem>
  166. <FormItem className="half-item" {...formItemLayout} label="产品类型">
  167. {getFieldDecorator('category', {
  168. rules: [ { required: true, message: '此项为必填项!' } ],
  169. initialValue: theData.category!=undefined?String(theData.category):theData.category
  170. })(
  171. <Select placeholder="产品类型">
  172. {
  173. productType.map(item=>{
  174. return <Select.Option value={item.value}>{item.key}</Select.Option>
  175. })
  176. }
  177. </Select>
  178. )}
  179. </FormItem>
  180. <FormItem className="half-item" {...formItemLayout} label="专栏来源">
  181. {getFieldDecorator('source', {
  182. rules: [ { required: true, message: '此项为必填项!' } ],
  183. initialValue: theData.source
  184. })(<Input placeholder="请输入来源" maxLength={45} />)}
  185. </FormItem>
  186. </div>
  187. <div className="clearfix">
  188. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="关键字">
  189. {getFieldDecorator('keyword', {
  190. initialValue: theData.keyword
  191. })(<KeyWordTagGroup
  192. keyWordArr={this.state.tags}
  193. tagsArr={(e)=>{this.setState({tags:e})}}
  194. />)}
  195. </FormItem>
  196. </div>
  197. <div className="clearfix">
  198. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span>
  199. <strong style={{ color: '#f00' }}> * </strong>
  200. <span>专栏图片</span>
  201. </span>}>
  202. <Crop
  203. getAllImg={(e)=>{this.setState({pictureUrl:e})}}
  204. number = {1}
  205. aspectRatio = {2/1}
  206. url ={globalConfig.context + '/api/admin/community/upload'}
  207. uploadData = {{sign:'knowledge_base'}}
  208. urlArr = {this.state.pictureUrl}
  209. />
  210. </FormItem>
  211. </div>
  212. <div className="clearfix">
  213. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="简介">
  214. {getFieldDecorator('summary', {
  215. rules: [ { required: true, message: '此项为必填项!' } ],
  216. initialValue: theData.summary
  217. })(<Input type="textarea" rows={4} maxLength={144} placeholder="输入简介" />)}
  218. </FormItem>
  219. </div>
  220. <div className="clearfix">
  221. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>专栏正文</span>}>
  222. <Editors textContent={this.state.edit}
  223. uploadUrl={'/api/user/demand/uploadPicture'}
  224. uploadSign={'demand_picture'}
  225. handleRichText={(value) => { this.state.edit = value; }}
  226. visible={this.state.visible} />
  227. </FormItem>
  228. </div>
  229. <div className="clearfix">
  230. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发布状态">
  231. <span>{getProStatus(theData.status)}</span>
  232. </FormItem>
  233. </div>
  234. <div className="clearfix">
  235. <FormItem className="half-item" {...formItemLayout} label="置顶">
  236. {getFieldDecorator('top', {
  237. initialValue: theData.top
  238. })(
  239. <Radio.Group>
  240. <Radio value={0}>否</Radio>
  241. <Radio value={1}>是</Radio>
  242. </Radio.Group>
  243. )}
  244. </FormItem>
  245. <FormItem className="half-item" {...formItemLayout} label="推荐">
  246. {getFieldDecorator('recommend', {
  247. initialValue: theData.recommend
  248. })(
  249. <Radio.Group>
  250. <Radio value={0}>否</Radio>
  251. <Radio value={1}>是</Radio>
  252. </Radio.Group>
  253. )}
  254. </FormItem>
  255. <FormItem className="half-item" {...formItemLayout} label="阅读量">
  256. <span>{theData.readNumber||0}</span>
  257. </FormItem>
  258. <FormItem className="half-item" {...formItemLayout} label="评论量">
  259. <span>{theData.commentNumber||0}</span>
  260. </FormItem>
  261. <FormItem className="half-item" {...formItemLayout} label="点赞量">
  262. <span>{theData.starNumber||0}</span>
  263. </FormItem>
  264. </div>
  265. <div className="clearfix">
  266. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  267. <Button className="set-submit" type="primary" htmlType="submit">
  268. 保存草稿
  269. </Button>
  270. <Button
  271. className="set-submit"
  272. type="ghost"
  273. onClick={(e) => {
  274. this.handleSubmit(e,1)
  275. }}
  276. >
  277. 发布新闻
  278. </Button>
  279. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  280. 取消
  281. </Button>
  282. </FormItem>
  283. </div>
  284. </Spin>
  285. </Form>
  286. </div>
  287. )
  288. }
  289. }));
  290. export default NewDetailForm;