newForm.jsx 12 KB

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