newForm.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 {areaSelect } 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 >= 3 ? 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. };
  85. },
  86. loadData(id) {
  87. this.setState({
  88. loading: true
  89. });
  90. $.ajax({
  91. method: "get",
  92. dataType: "json",
  93. crossDomain: false,
  94. url: globalConfig.context + '/api/user/achievement/achievementDetail' ,
  95. data: {
  96. id: id
  97. },
  98. success: function (data) {
  99. let thisData = data.data;
  100. if (!thisData) {
  101. if (data.error && data.error.length) {
  102. message.warning(data.error[0].message);
  103. };
  104. thisData = {};
  105. };
  106. this.setState({
  107. id:thisData.id,
  108. data: thisData,
  109. publishPages:thisData.publishPages.length?thisData.publishPages:['app_achievement_list','web_achievement_list'],
  110. auditStatus:thisData.auditStatus,
  111. pictureUrl: thisData.technicalPictureUrl ? splitUrl(thisData.technicalPictureUrl, ',', globalConfig.avatarHost + '/upload') : []
  112. });
  113. }.bind(this),
  114. }).always(function () {
  115. this.setState({
  116. loading: false
  117. });
  118. }.bind(this));
  119. },
  120. getPictureUrl(e) {
  121. this.setState({ pictureUrl: e });
  122. },
  123. handleSubmit(e,index) {
  124. e.preventDefault();
  125. this.props.form.validateFields((err, values) => {
  126. //url转化
  127. let thePictureUrl = [];
  128. if (this.state.pictureUrl.length) {
  129. let picArr = [];
  130. this.state.pictureUrl.map(function (item) {
  131. if ( item.response && item.response.data && item.response.data.length ){
  132. picArr.push(item.response.data);
  133. }
  134. });
  135. thePictureUrl = picArr.join(",");
  136. };
  137. if (!err) {
  138. this.setState({
  139. loading: true
  140. });
  141. $.ajax({
  142. method: "post",
  143. dataType: "json",
  144. async:true,
  145. url: this.props.data.id ? globalConfig.context + '/api/user/achievement/update' : globalConfig.context + '/api/user/achievement/apply',
  146. data: {
  147. id: this.props.data.id,
  148. name: values.name,
  149. fieldA: values.industryCategory ? values.industryCategory[0] : undefined,
  150. fieldB: values.industryCategory ? values.industryCategory[1] : undefined,
  151. dataCategory: values.dataCategory,
  152. category: values.category,
  153. technicalPictureUrl: thePictureUrl,
  154. introduction:values.introduction,
  155. maturity:values.maturity,
  156. innovation:values.innovation,
  157. transferMode:values.transferMode,
  158. auditStatus:index,
  159. patentCase: values.patentCase,
  160. teamDes: values.teamDes,
  161. parameter: values.parameter,
  162. boutique: values.boutique,
  163. isHot:values.isHot,
  164. publishPages:values.publishPages,
  165. }
  166. }).done(function (data) {
  167. this.setState({
  168. loading: false
  169. });
  170. if (!data.error.length) {
  171. message.success('操作成功!');
  172. this.setState({
  173. visible: false
  174. });
  175. this.props.handOk();
  176. } else {
  177. message.warning(data.error[0].message);
  178. }
  179. }.bind(this));
  180. }
  181. });
  182. },
  183. submission(e,index){
  184. this.handleSubmit(e,index);
  185. $.ajax({
  186. method: "POST",
  187. dataType: "json",
  188. crossDomain: false,
  189. async:true,
  190. url: globalConfig.context + "/api/user/achievement/commit",
  191. data: {
  192. id: this.state.id,
  193. }
  194. }).done(function (data) {
  195. if (!data.error.length) {
  196. this.setState({
  197. loading: false,
  198. });
  199. } else {
  200. message.warning(data.error[0].message);
  201. };
  202. }.bind(this));
  203. },
  204. cancelFun(){
  205. this.props.closeDesc();
  206. },
  207. componentWillMount() {
  208. if (this.props.data.id) {
  209. this.loadData(this.props.data.id);
  210. } else {
  211. this.state.data = {};
  212. this.state.tags = [];
  213. this.setState({
  214. publishPages:['app_achievement_list','web_achievement_list']
  215. })
  216. };
  217. },
  218. componentWillReceiveProps(nextProps) {
  219. if (!this.props.visible && nextProps.visible) {
  220. this.state.textFileList = [];
  221. this.state.videoFileList = [];
  222. if (nextProps.data.id) {
  223. this.loadData(nextProps.data.id);
  224. } else {
  225. this.state.data = {};
  226. this.state.pictureUrl = [];
  227. this.state.theData={};
  228. this.setState({
  229. publishPages:['app_achievement_list','web_achievement_list']
  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. this.state.edit ={};
  244. return (
  245. <div className='login'>
  246. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  247. <Spin spinning={this.state.loading}>
  248. <div className="clearfix">
  249. <FormItem className="half-item" {...formItemLayout} label="标题">
  250. {getFieldDecorator('name', {
  251. rules: [ { required: true, message: '此项为必填项!' } ],
  252. initialValue: theData.name
  253. })(<Input placeholder="请输入您的新闻标题" />)}
  254. </FormItem>
  255. </div>
  256. <div className="clearfix">
  257. <FormItem className="half-item" {...formItemLayout} label="热门">
  258. {getFieldDecorator('hot', {
  259. rules: [ { required: true, message: '此项为必填项!' } ],
  260. initialValue: theData.hot
  261. })(
  262. <Radio.Group>
  263. <Radio value={0}>否</Radio>
  264. <Radio value={1}>是</Radio>
  265. </Radio.Group>
  266. )}
  267. </FormItem>
  268. <FormItem className="half-item" {...formItemLayout} label="新闻来源">
  269. {getFieldDecorator('name', {
  270. rules: [ { required: true, message: '此项为必填项!' } ],
  271. initialValue: theData.name
  272. })(<Input placeholder="请输入新闻来源" />)}
  273. </FormItem>
  274. <FormItem className="half-item" {...formItemLayout} label="来源链接">
  275. {getFieldDecorator('url', {
  276. initialValue: theData.url
  277. })(<Input placeholder="请输入新闻来源链接" />)}
  278. </FormItem>
  279. <FormItem className="half-item"
  280. {...formItemLayout}
  281. label="省-市-区"
  282. >
  283. {getFieldDecorator('ProvinceCity', {
  284. rules: [ { required: true, message: '此项为必填项!' } ],
  285. initialValue: theData.ProvinceCity
  286. })(
  287. <Cascader options={areaSelect()} placeholder="选择城市" />
  288. )}
  289. </FormItem>
  290. </div>
  291. <div className="clearfix">
  292. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻图片">
  293. <PicturesWall
  294. fileList={this.getPictureUrl}
  295. pictureUrl={this.state.pictureUrl}
  296. visible={this.props.visible}
  297. />
  298. </FormItem>
  299. </div>
  300. <div className="clearfix">
  301. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻简介">
  302. {getFieldDecorator('introduction', {
  303. rules: [ { required: true, message: '此项为必填项!' } ],
  304. initialValue: theData.introduction
  305. })(<Input type="textarea" rows={4} placeholder="输入新闻简介" />)}
  306. </FormItem>
  307. </div>
  308. <div className="clearfix">
  309. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>新闻正文</span>}>
  310. <Editors textContent={this.state.edit.content}
  311. uploadUrl={'/api/admin/news/upload'}
  312. uploadSign={'news_content_picture'}
  313. handleRichText={(value) => { this.state.edit.content = value; }}
  314. visible={this.state.visible} />
  315. </FormItem>
  316. </div>
  317. {theData.auditInfo&&<div className="clearfix">
  318. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="审核信息">
  319. <span>{theData.auditInfo}</span>
  320. </FormItem>
  321. </div>}
  322. {theData.createTime&&<div className="clearfix">
  323. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发布时间">
  324. <span>{theData.createTime}</span>
  325. </FormItem>
  326. </div>}
  327. <div className="clearfix">
  328. <FormItem className="full-item"
  329. labelCol={{ span: 3 }}
  330. wrapperCol={{ span: 18 }} label="发布位置">
  331. {getFieldDecorator('publishPages', {
  332. rules: [ { required: false } ],
  333. initialValue: this.state.publishPages
  334. })(
  335. <Checkbox.Group>
  336. <Row>
  337. <Col span={6}><Checkbox value="web_index">网站新闻首页</Checkbox></Col>
  338. <Col span={6}><Checkbox value="web_achievement_main">网站科技服务主页</Checkbox></Col>
  339. <Col span={6}><Checkbox value="web_achievement_list" disabled>app政策主页</Checkbox></Col>
  340. <Col span={6}><Checkbox value="app_index">APP科技服务首页</Checkbox></Col>
  341. </Row>
  342. </Checkbox.Group>
  343. )}
  344. </FormItem>
  345. </div>
  346. <div className="clearfix">
  347. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  348. {this.props.data&&!this.props.data.id&&<Button className="set-submit" type="primary" htmlType="submit">
  349. 保存草稿
  350. </Button>}
  351. <Button
  352. className="set-submit"
  353. type="ghost"
  354. onClick={(e) => {
  355. if(this.props.data.id){
  356. this.submission(e,1)
  357. }else{
  358. this.handleSubmit(e,1)
  359. }
  360. }}
  361. >
  362. 发布新闻
  363. </Button>
  364. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  365. 取消
  366. </Button>
  367. </FormItem>
  368. </div>
  369. </Spin>
  370. </Form>
  371. </div>
  372. )
  373. }
  374. }));
  375. export default NewDetailForm;