newForm.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. auditStatus:index,
  174. summary: values.summary,
  175. provinceId: (values.ProvinceCity)[0],
  176. publishPages:publishPages
  177. }
  178. }).done(function (data) {
  179. this.setState({
  180. loading: false
  181. });
  182. if (!data.error.length) {
  183. message.success('操作成功!');
  184. this.setState({
  185. visible: false
  186. });
  187. this.props.handOk();
  188. } else {
  189. message.warning(data.error[0].message);
  190. }
  191. }.bind(this));
  192. }
  193. });
  194. },
  195. cancelFun(){
  196. this.props.closeDesc();
  197. },
  198. componentWillMount() {
  199. if (this.props.data.id) {
  200. this.loadData(this.props.data.id);
  201. } else {
  202. this.state.visible=true;
  203. this.state.data = {};
  204. this.state.edit='<p><br></p>';
  205. this.state.pictureUrl = [];
  206. this.state.Province=undefined;
  207. this.setState({
  208. publishPages:['web_index','app_policy_index']
  209. })
  210. };
  211. },
  212. componentWillReceiveProps(nextProps) {
  213. if (!this.props.visible && nextProps.visible) {
  214. this.state.textFileList = [];
  215. this.state.videoFileList = [];
  216. if (nextProps.data.id) {
  217. this.loadData(nextProps.data.id);
  218. } else {
  219. this.state.visible=true;
  220. this.state.data = {};
  221. this.state.Province=undefined;
  222. this.state.edit='<p><br></p>';
  223. this.state.pictureUrl = [];
  224. this.state.theData={};
  225. this.setState({
  226. publishPages:['web_index','app_policy_index']
  227. })
  228. };
  229. this.props.form.resetFields();
  230. };
  231. },
  232. render() {
  233. const theData = this.state.data || {};
  234. const { getFieldDecorator } = this.props.form;
  235. const FormItem = Form.Item
  236. const formItemLayout = {
  237. labelCol: { span: 6 },
  238. wrapperCol: { span: 12 },
  239. };
  240. return (
  241. <div className='login'>
  242. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  243. <Spin spinning={this.state.loading}>
  244. <div className="clearfix">
  245. <FormItem className="half-item" {...formItemLayout} label="标题">
  246. {getFieldDecorator('title', {
  247. rules: [ { required: true, message: '此项为必填项!' } ],
  248. initialValue: theData.title
  249. })(<Input placeholder="请输入您的新闻标题" />)}
  250. </FormItem>
  251. <FormItem className="half-item" {...formItemLayout} label="新闻类型">
  252. {getFieldDecorator('type', {
  253. rules: [ { required: true, message: '此项为必填项!' } ],
  254. initialValue: theData.type!=undefined?String(theData.type):theData.type
  255. })(
  256. <Select placeholder="新闻类型">
  257. <Select.Option value="0" >普通新闻</Select.Option>
  258. <Select.Option value="98" >知产百科</Select.Option>
  259. <Select.Option value="99" >技淘观点</Select.Option>
  260. </Select>
  261. )}
  262. </FormItem>
  263. </div>
  264. <div className="clearfix">
  265. <FormItem className="half-item" {...formItemLayout} label="热门">
  266. {getFieldDecorator('hot', {
  267. rules: [ { required: true, message: '此项为必填项!' } ],
  268. initialValue: theData.hot
  269. })(
  270. <Radio.Group>
  271. <Radio value={0}>否</Radio>
  272. <Radio value={1}>是</Radio>
  273. </Radio.Group>
  274. )}
  275. </FormItem>
  276. <FormItem className="half-item" {...formItemLayout} label="新闻来源">
  277. {getFieldDecorator('source', {
  278. rules: [ { required: true, message: '此项为必填项!' } ],
  279. initialValue: theData.source
  280. })(<Input placeholder="请输入新闻来源" />)}
  281. </FormItem>
  282. <FormItem className="half-item" {...formItemLayout} label="来源链接">
  283. {getFieldDecorator('sourceUrl', {
  284. initialValue: theData.sourceUrl
  285. })(<Input placeholder="请输入新闻来源链接" />)}
  286. </FormItem>
  287. <FormItem className="half-item"
  288. {...formItemLayout}
  289. label="省-市-区"
  290. >
  291. {getFieldDecorator('ProvinceCity', {
  292. rules: [ { required: true, message: '此项为必填项!' } ],
  293. initialValue: this.state.Province
  294. })(
  295. <Cascader options={provinceSelect()} placeholder="选择城市" />
  296. )}
  297. </FormItem>
  298. </div>
  299. <div className="clearfix">
  300. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻图片">
  301. <PicturesWall
  302. fileList={this.getPictureUrl}
  303. pictureUrl={this.state.pictureUrl}
  304. visible={this.props.visible}
  305. />
  306. </FormItem>
  307. </div>
  308. <div className="clearfix">
  309. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻简介">
  310. {getFieldDecorator('summary', {
  311. rules: [ { required: true, message: '此项为必填项!' } ],
  312. initialValue: theData.summary
  313. })(<Input type="textarea" rows={4} placeholder="输入新闻简介" />)}
  314. </FormItem>
  315. </div>
  316. <div className="clearfix">
  317. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>新闻正文</span>}>
  318. <Editors textContent={this.state.edit}
  319. uploadUrl={'/api/user/demand/uploadPicture'}
  320. uploadSign={'demand_picture'}
  321. handleRichText={(value) => { this.state.edit = value; }}
  322. visible={this.state.visible} />
  323. </FormItem>
  324. </div>
  325. <div className="clearfix">
  326. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="是否发布">
  327. <span>{theData.releaseStatus?'已发布':'未发布'}</span>
  328. </FormItem>
  329. </div>
  330. <div className="clearfix">
  331. <FormItem className="full-item"
  332. labelCol={{ span: 3 }}
  333. wrapperCol={{ span: 18 }} label="发布位置">
  334. {getFieldDecorator('publishPages', {
  335. rules: [ { required: false } ],
  336. initialValue: this.state.publishPages
  337. })(
  338. <Checkbox.Group>
  339. <Row>
  340. <Col span={6}><Checkbox value="web_index" disabled>网站新闻首页</Checkbox></Col>
  341. <Col span={6}><Checkbox value="web_tech_service_index">网站科技服务首页</Checkbox></Col>
  342. <Col span={6}><Checkbox value="app_policy_index" disabled>app政策首页</Checkbox></Col>
  343. <Col span={6}><Checkbox value="app_tech_service_index">APP科技服务首页</Checkbox></Col>
  344. </Row>
  345. </Checkbox.Group>
  346. )}
  347. </FormItem>
  348. </div>
  349. <div className="clearfix">
  350. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  351. <Button className="set-submit" type="primary" htmlType="submit">
  352. 保存草稿
  353. </Button>
  354. <Button
  355. className="set-submit"
  356. type="ghost"
  357. onClick={(e) => {
  358. this.handleSubmit(e,2)
  359. }}
  360. >
  361. 发布新闻
  362. </Button>
  363. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  364. 取消
  365. </Button>
  366. </FormItem>
  367. </div>
  368. </Spin>
  369. </Form>
  370. </div>
  371. )
  372. }
  373. }));
  374. export default NewDetailForm;