proForm.jsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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="请输入您的专栏标题" />)}
  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="请输入来源" />)}
  185. </FormItem>
  186. {/* <FormItem className="half-item" {...formItemLayout} label="来源链接">
  187. {getFieldDecorator('sourceUrl', {
  188. initialValue: theData.sourceUrl
  189. })(<Input placeholder="请输入来源链接" />)}
  190. </FormItem> */}
  191. </div>
  192. <div className="clearfix">
  193. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="关键字">
  194. {getFieldDecorator('keyword', {
  195. initialValue: theData.keyword
  196. })(<KeyWordTagGroup
  197. keyWordArr={this.state.tags}
  198. tagsArr={(e)=>{this.setState({tags:e})}}
  199. />)}
  200. </FormItem>
  201. </div>
  202. <div className="clearfix">
  203. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="专栏图片">
  204. <Crop
  205. getAllImg={(e)=>{this.setState({pictureUrl:e})}}
  206. number = {1}
  207. aspectRatio = {2/1}
  208. url ={globalConfig.context + '/api/admin/community/upload'}
  209. uploadData = {{sign:'knowledge_base'}}
  210. urlArr = {this.state.pictureUrl}
  211. />
  212. </FormItem>
  213. </div>
  214. <div className="clearfix">
  215. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="简介">
  216. {getFieldDecorator('summary', {
  217. rules: [ { required: true, message: '此项为必填项!' } ],
  218. initialValue: theData.summary
  219. })(<Input type="textarea" rows={4} placeholder="输入简介" />)}
  220. </FormItem>
  221. </div>
  222. <div className="clearfix">
  223. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>专栏正文</span>}>
  224. <Editors textContent={this.state.edit}
  225. uploadUrl={'/api/user/demand/uploadPicture'}
  226. uploadSign={'demand_picture'}
  227. handleRichText={(value) => { this.state.edit = value; }}
  228. visible={this.state.visible} />
  229. </FormItem>
  230. </div>
  231. <div className="clearfix">
  232. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发布状态">
  233. <span>{getProStatus(theData.status)}</span>
  234. </FormItem>
  235. </div>
  236. <div className="clearfix">
  237. <FormItem className="half-item" {...formItemLayout} label="置顶">
  238. {getFieldDecorator('top', {
  239. initialValue: theData.top
  240. })(
  241. <Radio.Group>
  242. <Radio value={0}>否</Radio>
  243. <Radio value={1}>是</Radio>
  244. </Radio.Group>
  245. )}
  246. </FormItem>
  247. <FormItem className="half-item" {...formItemLayout} label="推荐">
  248. {getFieldDecorator('recommend', {
  249. initialValue: theData.recommend
  250. })(
  251. <Radio.Group>
  252. <Radio value={0}>否</Radio>
  253. <Radio value={1}>是</Radio>
  254. </Radio.Group>
  255. )}
  256. </FormItem>
  257. <FormItem className="half-item" {...formItemLayout} label="阅读量">
  258. <span>{theData.readNumber||0}</span>
  259. </FormItem>
  260. <FormItem className="half-item" {...formItemLayout} label="评论量">
  261. <span>{theData.commentNumber||0}</span>
  262. </FormItem>
  263. <FormItem className="half-item" {...formItemLayout} label="点赞量">
  264. <span>{theData.starNumber||0}</span>
  265. </FormItem>
  266. </div>
  267. <div className="clearfix">
  268. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  269. <Button className="set-submit" type="primary" htmlType="submit">
  270. 保存草稿
  271. </Button>
  272. <Button
  273. className="set-submit"
  274. type="ghost"
  275. onClick={(e) => {
  276. this.handleSubmit(e,1)
  277. }}
  278. >
  279. 发布新闻
  280. </Button>
  281. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  282. 取消
  283. </Button>
  284. </FormItem>
  285. </div>
  286. </Spin>
  287. </Form>
  288. </div>
  289. )
  290. }
  291. }));
  292. export default NewDetailForm;