proForm.jsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. status:index,
  95. keyword: this.state.tags ? this.state.tags.join(",") : '',
  96. summary: values.summary,
  97. }
  98. }).done(function (data) {
  99. this.setState({
  100. loading: false
  101. });
  102. if (!data.error.length) {
  103. message.success('操作成功!');
  104. this.setState({
  105. visible: false
  106. });
  107. this.props.handOk();
  108. } else {
  109. message.warning(data.error[0].message);
  110. }
  111. }.bind(this));
  112. }
  113. });
  114. },
  115. cancelFun(){
  116. this.props.closeDesc();
  117. },
  118. componentWillMount() {
  119. if (this.props.data.id) {
  120. this.loadData(this.props.data.id);
  121. } else {
  122. this.state.visible=true;
  123. this.state.data = {};
  124. this.state.tags=[];
  125. this.state.edit='<p><br></p>';
  126. this.state.pictureUrl = [];
  127. };
  128. },
  129. componentWillReceiveProps(nextProps) {
  130. if (!this.props.visible && nextProps.visible) {
  131. this.state.textFileList = [];
  132. this.state.videoFileList = [];
  133. if (nextProps.data.id) {
  134. this.loadData(nextProps.data.id);
  135. } else {
  136. this.state.visible=true;
  137. this.state.data = {};
  138. this.state.tags=[];
  139. this.state.edit='<p><br></p>';
  140. this.state.pictureUrl = [];
  141. this.state.theData={};
  142. };
  143. this.props.form.resetFields();
  144. };
  145. },
  146. render() {
  147. const theData = this.state.data || {};
  148. const { getFieldDecorator } = this.props.form;
  149. const FormItem = Form.Item
  150. const formItemLayout = {
  151. labelCol: { span: 6 },
  152. wrapperCol: { span: 12 },
  153. };
  154. return (
  155. <div className='login'>
  156. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  157. <Spin spinning={this.state.loading}>
  158. <div className="clearfix">
  159. <FormItem className="half-item" {...formItemLayout} label="标题">
  160. {getFieldDecorator('title', {
  161. rules: [ { required: true, message: '此项为必填项!' } ],
  162. initialValue: theData.title
  163. })(<Input placeholder="请输入您的专栏标题" maxLength={40}/>)}
  164. </FormItem>
  165. <FormItem className="half-item" {...formItemLayout} label="产品类型">
  166. {getFieldDecorator('category', {
  167. rules: [ { required: true, message: '此项为必填项!' } ],
  168. initialValue: theData.category!=undefined?String(theData.category):theData.category
  169. })(
  170. <Select placeholder="产品类型">
  171. {
  172. productType.map(item=>{
  173. return <Select.Option value={item.value}>{item.key}</Select.Option>
  174. })
  175. }
  176. </Select>
  177. )}
  178. </FormItem>
  179. </div>
  180. <div className="clearfix">
  181. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="关键字">
  182. {getFieldDecorator('keyword', {
  183. initialValue: theData.keyword
  184. })(<KeyWordTagGroup
  185. keyWordArr={this.state.tags}
  186. tagsArr={(e)=>{this.setState({tags:e})}}
  187. />)}
  188. </FormItem>
  189. </div>
  190. <div className="clearfix">
  191. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="专栏图片">
  192. <Crop
  193. getAllImg={(e)=>{this.setState({pictureUrl:e})}}
  194. number = {1}
  195. aspectRatio = {2/1}
  196. url ={globalConfig.context + '/api/admin/community/upload'}
  197. uploadData = {{sign:'knowledge_base'}}
  198. urlArr = {this.state.pictureUrl}
  199. />
  200. </FormItem>
  201. </div>
  202. <div className="clearfix">
  203. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="简介">
  204. {getFieldDecorator('summary', {
  205. rules: [ { required: true, message: '此项为必填项!' } ],
  206. initialValue: theData.summary
  207. })(<Input type="textarea" rows={4} maxLength={144} placeholder="输入简介" />)}
  208. </FormItem>
  209. </div>
  210. <div className="clearfix">
  211. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>专栏正文</span>}>
  212. <Editors textContent={this.state.edit}
  213. uploadUrl={'/api/user/demand/uploadPicture'}
  214. uploadSign={'demand_picture'}
  215. handleRichText={(value) => { this.state.edit = value; }}
  216. visible={this.state.visible} />
  217. </FormItem>
  218. </div>
  219. <div className="clearfix">
  220. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发布状态">
  221. <span>{getProStatus(theData.status)}</span>
  222. </FormItem>
  223. </div>
  224. <div className="clearfix">
  225. <FormItem className="half-item" {...formItemLayout} label="热门">
  226. {getFieldDecorator('top', {
  227. initialValue: theData.top
  228. })(
  229. <Radio.Group>
  230. <Radio value={0}>否</Radio>
  231. <Radio value={1}>是</Radio>
  232. </Radio.Group>
  233. )}
  234. </FormItem>
  235. <FormItem className="half-item" {...formItemLayout} label="推荐">
  236. {getFieldDecorator('recommend', {
  237. initialValue: theData.recommend
  238. })(
  239. <Radio.Group>
  240. <Radio value={0}>否</Radio>
  241. <Radio value={1}>是</Radio>
  242. </Radio.Group>
  243. )}
  244. </FormItem>
  245. {this.props.data.id&&<div className="clearfix">
  246. <FormItem className="half-item" {...formItemLayout} label="阅读量">
  247. <span>{theData.readNumber||0}</span>
  248. </FormItem>
  249. <FormItem className="half-item" {...formItemLayout} label="评论量">
  250. <span>{theData.commentNumber||0}</span>
  251. </FormItem>
  252. <FormItem className="half-item" {...formItemLayout} label="点赞量">
  253. <span>{theData.starNumber||0}</span>
  254. </FormItem>
  255. </div>}
  256. </div>
  257. <div className="clearfix">
  258. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  259. <Button className="set-submit" type="primary" htmlType="submit">
  260. 保存草稿
  261. </Button>
  262. <Button
  263. className="set-submit"
  264. type="ghost"
  265. onClick={(e) => {
  266. this.handleSubmit(e,1)
  267. }}
  268. >
  269. 发布新闻
  270. </Button>
  271. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  272. 取消
  273. </Button>
  274. </FormItem>
  275. </div>
  276. </Spin>
  277. </Form>
  278. </div>
  279. )
  280. }
  281. }));
  282. export default NewDetailForm;