newForm.jsx 11 KB

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