newForm.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import React from 'react';
  2. import $ from 'jquery/src/ajax';
  3. import {
  4. Icon,
  5. Modal,
  6. message,
  7. Spin,
  8. Upload,
  9. Input,
  10. Button,
  11. Radio,
  12. Form,
  13. Checkbox,
  14. Cascader,
  15. Row,
  16. Select,
  17. Col, DatePicker
  18. } from 'antd';
  19. import { splitUrl} from '@/tools.js';
  20. import {provinceSelect } from '@/NewDicProvinceList';
  21. import Editors from '@/richTextEditors';
  22. import moment from "moment";
  23. const PicturesWall = React.createClass({
  24. getInitialState() {
  25. return {
  26. previewVisible: false,
  27. previewImage: '',
  28. fileList: [],
  29. tags: []
  30. };
  31. },
  32. handleCancel() {
  33. this.setState({ previewVisible: false });
  34. },
  35. handlePreview(file) {
  36. this.setState({
  37. previewImage: file.url || file.thumbUrl,
  38. previewVisible: true
  39. });
  40. },
  41. handleChange(info) {
  42. let fileList = info.fileList;
  43. this.setState({ fileList });
  44. this.props.fileList(fileList);
  45. },
  46. componentWillReceiveProps(nextProps) {
  47. this.state.fileList = nextProps.pictureUrl;
  48. },
  49. render() {
  50. const { previewVisible, previewImage, fileList } = this.state;
  51. const uploadButton = (
  52. <div>
  53. <Icon type="plus" />
  54. <div className="ant-upload-text">点击上传</div>
  55. </div>
  56. );
  57. return (
  58. <div className="clearfix">
  59. <Upload
  60. action={globalConfig.context + '/api/user/demand/uploadPicture'}
  61. data={{ sign: 'demand_picture'}}
  62. listType="picture-card"
  63. fileList={fileList}
  64. onPreview={this.handlePreview}
  65. onChange={this.handleChange}
  66. >
  67. {fileList.length >= 2 ? null : uploadButton}
  68. </Upload>
  69. <Modal maskClosable={false} visible={previewVisible} footer={null} onCancel={this.handleCancel}>
  70. <img alt="example" style={{ width: '100%' }} src={previewImage} />
  71. </Modal>
  72. </div>
  73. );
  74. }
  75. });
  76. const NewDetailForm = Form.create()(React.createClass({
  77. getInitialState() {
  78. return {
  79. visible: false,
  80. loading: false,
  81. auditStatus: 0,
  82. textFileList: [],
  83. videoFileList: [],
  84. pictureUrl: [],
  85. tags:[],
  86. edit:{},
  87. edits:{}
  88. };
  89. },
  90. loadData(id) {
  91. this.setState({
  92. loading: true
  93. });
  94. $.ajax({
  95. method: "get",
  96. dataType: "json",
  97. crossDomain: false,
  98. url: globalConfig.context + '/api/admin/news/detail' ,
  99. data: {
  100. id: id
  101. },
  102. success: function (data) {
  103. let thisData = data.data,provice=[];
  104. if (!thisData) {
  105. if (data.error && data.error.length) {
  106. message.warning(data.error[0].message);
  107. };
  108. thisData = {};
  109. };
  110. let publishPagesArr = [];
  111. let pageList = thisData.newsPublishs
  112. pageList.map(item=>{
  113. publishPagesArr.push(item.publishPage)
  114. });
  115. let pageK = publishPagesArr.length?publishPagesArr:['web_index','app_policy_index']
  116. provice.push(thisData.provinceId);
  117. this.setState({
  118. id:thisData.id,
  119. data: thisData,
  120. Province:provice,
  121. publishPages:pageK,
  122. auditStatus:thisData.auditStatus,
  123. edit:thisData.content,
  124. pictureUrl: thisData.titleImg ? splitUrl(thisData.titleImg, ',', globalConfig.avatarHost + '/upload') : []
  125. });
  126. }.bind(this),
  127. }).always(function () {
  128. this.setState({
  129. loading: false
  130. });
  131. }.bind(this));
  132. },
  133. getPictureUrl(e) {
  134. this.setState({ pictureUrl: e });
  135. },
  136. handleSubmit(e,index) {
  137. e.preventDefault();
  138. this.props.form.validateFields((err, values) => {
  139. if((this.state.edit)==undefined||!this.state.edit||(this.state.edit)=='<p><br></p>'){
  140. message.warning('请填写政策正文');
  141. return false;
  142. }
  143. //url转化
  144. let thePictureUrl = [];
  145. if (this.state.pictureUrl.length) {
  146. let picArr = [];
  147. this.state.pictureUrl.map(function (item) {
  148. if ( item.response && item.response.data && item.response.data.length ){
  149. picArr.push(item.response.data);
  150. }
  151. });
  152. thePictureUrl = picArr.join(",");
  153. };
  154. if (!err) {
  155. // let publishPages =(values.publishPages).join(',')
  156. this.setState({
  157. loading: true
  158. });
  159. $.ajax({
  160. method: "post",
  161. dataType: "json",
  162. async:true,
  163. url: this.props.data.id ? globalConfig.context + '/api/admin/news/updateNews' : globalConfig.context + '/api/admin/news/apply',
  164. data: {
  165. id: this.props.data.id,
  166. type:values.type,
  167. title: values.title,
  168. author: values.author,
  169. titleImg: thePictureUrl.length?thePictureUrl:'',
  170. content:this.state.edit,
  171. hot:values.hot,
  172. source:values.source,
  173. sourceUrl:values.sourceUrl,
  174. keyword:values.keyword,
  175. auditStatus:index,
  176. summary: values.summary,
  177. provinceId: (values.ProvinceCity)[0],
  178. releaseDateStr:moment(values.releaseDateStr).format('YYYY-MM-DD HH:mm:ss'),
  179. // publishPages:publishPages
  180. }
  181. }).done(function (data) {
  182. this.setState({
  183. loading: false
  184. });
  185. if (!data.error.length) {
  186. message.success('操作成功!');
  187. this.setState({
  188. visible: false
  189. });
  190. this.props.handOk();
  191. } else {
  192. message.warning(data.error[0].message);
  193. }
  194. }.bind(this));
  195. }
  196. });
  197. },
  198. cancelFun(){
  199. this.props.closeDesc();
  200. },
  201. componentWillMount() {
  202. if (this.props.data.id) {
  203. this.loadData(this.props.data.id);
  204. } else {
  205. this.state.visible=true;
  206. this.state.data = {};
  207. this.state.edit='<p><br></p>';
  208. this.state.pictureUrl = [];
  209. this.state.Province=undefined;
  210. this.setState({
  211. publishPages:['web_index','app_policy_index']
  212. })
  213. };
  214. },
  215. componentWillReceiveProps(nextProps) {
  216. if (!this.props.visible && nextProps.visible) {
  217. this.state.textFileList = [];
  218. this.state.videoFileList = [];
  219. if (nextProps.data.id) {
  220. this.loadData(nextProps.data.id);
  221. } else {
  222. this.state.visible=true;
  223. this.state.data = {};
  224. this.state.Province=undefined;
  225. this.state.edit='<p><br></p>';
  226. this.state.pictureUrl = [];
  227. this.state.theData={};
  228. this.setState({
  229. publishPages:['web_index','app_policy_index']
  230. })
  231. };
  232. this.props.form.resetFields();
  233. };
  234. },
  235. render() {
  236. const theData = this.state.data || {};
  237. const { getFieldDecorator } = this.props.form;
  238. const FormItem = Form.Item
  239. const formItemLayout = {
  240. labelCol: { span: 6 },
  241. wrapperCol: { span: 12 },
  242. };
  243. const formItemLayouts = {
  244. labelCol: { span: 3 },
  245. wrapperCol: { span: 18 },
  246. };
  247. return (
  248. <div className='login'>
  249. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  250. <Spin spinning={this.state.loading}>
  251. <div className="clearfix">
  252. <FormItem className="half-item" {...formItemLayout} label="标题">
  253. {getFieldDecorator('title', {
  254. rules: [ { required: true, message: '此项为必填项!' } ],
  255. initialValue: theData.title
  256. })(<Input placeholder="请输入您的新闻标题" maxLength={40} />)}
  257. </FormItem>
  258. <FormItem className="half-item" {...formItemLayout} label="新闻类型">
  259. {getFieldDecorator('type', {
  260. rules: [ { required: true, message: '此项为必填项!' } ],
  261. initialValue: theData.type!=undefined?String(theData.type):theData.type
  262. })(
  263. <Select placeholder="新闻类型">
  264. <Select.Option value="0" >普通新闻</Select.Option>
  265. <Select.Option value="98" >知产百科</Select.Option>
  266. <Select.Option value="99" >技淘观点</Select.Option>
  267. </Select>
  268. )}
  269. </FormItem>
  270. </div>
  271. <div className="clearfix">
  272. <FormItem className="half-item" {...formItemLayout} label="热门">
  273. {getFieldDecorator('hot', {
  274. rules: [ { required: true, message: '此项为必填项!' } ],
  275. initialValue: theData.hot
  276. })(
  277. <Radio.Group>
  278. <Radio value={0}>否</Radio>
  279. <Radio value={1}>是</Radio>
  280. </Radio.Group>
  281. )}
  282. </FormItem>
  283. <FormItem className="half-item" {...formItemLayout} label="新闻来源">
  284. {getFieldDecorator('source', {
  285. rules: [ { required: true, message: '此项为必填项!' } ],
  286. initialValue: theData.source
  287. })(<Input placeholder="请输入新闻来源" maxLength={45} />)}
  288. </FormItem>
  289. <FormItem className="half-item" {...formItemLayout} label="来源链接">
  290. {getFieldDecorator('sourceUrl', {
  291. initialValue: theData.sourceUrl
  292. })(<Input placeholder="请输入新闻来源链接" />)}
  293. </FormItem>
  294. <FormItem className="half-item"
  295. {...formItemLayout}
  296. label="省-市-区"
  297. >
  298. {getFieldDecorator('ProvinceCity', {
  299. rules: [ { required: true, message: '此项为必填项!' } ],
  300. initialValue: this.state.Province
  301. })(
  302. <Cascader options={provinceSelect()} placeholder="选择城市" />
  303. )}
  304. </FormItem>
  305. </div>
  306. <div className="clearfix">
  307. <FormItem {...formItemLayouts} label="关键字">
  308. {getFieldDecorator('keyword', {
  309. initialValue: theData.keyword
  310. })(<Input placeholder="请输入关键字" />)}
  311. </FormItem>
  312. </div>
  313. <div className="clearfix">
  314. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻图片">
  315. <PicturesWall
  316. fileList={this.getPictureUrl}
  317. pictureUrl={this.state.pictureUrl}
  318. visible={this.props.visible}
  319. />
  320. </FormItem>
  321. </div>
  322. <div className="clearfix">
  323. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻简介">
  324. {getFieldDecorator('summary', {
  325. rules: [ { required: true, message: '此项为必填项!' } ],
  326. initialValue: theData.summary
  327. })(<Input type="textarea" rows={4} maxLength={144} placeholder="输入新闻简介" />)}
  328. </FormItem>
  329. </div>
  330. <div className="clearfix">
  331. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>新闻正文</span>}>
  332. <Editors textContent={this.state.edit}
  333. uploadUrl={'/api/user/demand/uploadPicture'}
  334. uploadSign={'demand_picture'}
  335. handleRichText={(value) => { this.state.edit = value; }}
  336. visible={this.state.visible} />
  337. </FormItem>
  338. </div>
  339. <div className="clearfix">
  340. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="是否发布">
  341. <span>{theData.releaseStatus?'已发布':'未发布'}</span>
  342. </FormItem>
  343. <FormItem className="half-item" {...formItemLayout} label="发布日期">
  344. {getFieldDecorator('releaseDateStr',{
  345. rules:[{required:true,message:"发布时间不能为空"}],
  346. initialValue: moment()
  347. })(<DatePicker showTime
  348. format="YYYY-MM-DD HH:mm:ss" />)}
  349. </FormItem>
  350. </div>
  351. {/* <div className="clearfix">
  352. <FormItem className="full-item"
  353. labelCol={{ span: 3 }}
  354. wrapperCol={{ span: 18 }} label="发布位置">
  355. {getFieldDecorator('publishPages', {
  356. rules: [ { required: false } ],
  357. initialValue: this.state.publishPages
  358. })(
  359. <Checkbox.Group>
  360. <Row>
  361. <Col span={6}><Checkbox value="web_index" disabled>网站新闻首页</Checkbox></Col>
  362. <Col span={6}><Checkbox value="web_tech_service_index">网站申请首页</Checkbox></Col>
  363. <Col span={6}><Checkbox value="app_policy_index" disabled>app新闻首页</Checkbox></Col>
  364. <Col span={6}><Checkbox value="app_tech_service_index">APP科技服务首页</Checkbox></Col>
  365. </Row>
  366. </Checkbox.Group>
  367. )}
  368. </FormItem>
  369. </div> */}
  370. <div className="clearfix">
  371. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  372. <Button className="set-submit" type="primary" htmlType="submit">
  373. 保存草稿
  374. </Button>
  375. <Button
  376. className="set-submit"
  377. type="ghost"
  378. onClick={(e) => {
  379. this.handleSubmit(e,2)
  380. }}
  381. >
  382. 发布新闻
  383. </Button>
  384. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  385. 取消
  386. </Button>
  387. </FormItem>
  388. </div>
  389. </Spin>
  390. </Form>
  391. </div>
  392. )
  393. }
  394. }));
  395. export default NewDetailForm;