policyForm.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. 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. };
  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/policy/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 proArr=[];
  110. proArr.push(thisData.province);
  111. this.setState({
  112. id:thisData.id,
  113. data: thisData,
  114. Province:proArr,
  115. publishPages:thisData.publishPages?thisData.publishPages:['web_policy_main','app_policy_main'],
  116. auditStatus:thisData.auditStatus,
  117. edit:{content:thisData.content},
  118. pictureUrl: thisData.titleImg ? splitUrl(thisData.titleImg, ',', globalConfig.avatarHost + '/upload') : [],
  119. releaseData: thisData.releaseDate,
  120. });
  121. console.log(this.state)
  122. }.bind(this),
  123. }).always(function () {
  124. this.setState({
  125. loading: false
  126. });
  127. }.bind(this));
  128. },
  129. getPictureUrl(e) {
  130. this.setState({ pictureUrl: e });
  131. },
  132. handleSubmit(e,index) {
  133. e.preventDefault();
  134. this.props.form.validateFields((err, values) => {
  135. //url转化
  136. if((this.state.edit).content==undefined||!(this.state.edit).content||(this.state.edit).content=='<p><br></p>'){
  137. message.warning('请填写政策正文');
  138. return false;
  139. }
  140. let thePictureUrl = [];
  141. if (this.state.pictureUrl.length) {
  142. let picArr = [];
  143. this.state.pictureUrl.map(function (item) {
  144. if ( item.response && item.response.data && item.response.data.length ){
  145. picArr.push(item.response.data);
  146. }
  147. });
  148. thePictureUrl = picArr.join(",");
  149. };
  150. if (!err) {
  151. let publishPages =(values.publishPages).join(',')
  152. this.setState({
  153. loading: true
  154. });
  155. $.ajax({
  156. method: "post",
  157. dataType: "json",
  158. async:true,
  159. url: this.props.data.id ? globalConfig.context + '/api/admin/policy/update' : globalConfig.context + '/api/admin/policy/apply',
  160. data: {
  161. id: this.props.data.id,
  162. type:0,
  163. title: values.title,
  164. author: values.author,
  165. titleImg: thePictureUrl.length?thePictureUrl:'',
  166. content:this.state.edit.content,
  167. hot:values.hot,
  168. source:values.source,
  169. sourceUrl:values.sourceUrl,
  170. keyword:values.keyword,
  171. auditStatus:index,
  172. summary: values.summary,
  173. province: (values.ProvinceCity)[0],
  174. publishPages:publishPages,
  175. releaseDateStr:moment(values.releaseDateStr).format('YYYY-MM-DD HH:mm:ss'),
  176. }
  177. }).done(function (data) {
  178. this.setState({
  179. loading: false
  180. });
  181. if (!data.error.length) {
  182. message.success('操作成功!');
  183. this.setState({
  184. visible: false
  185. });
  186. this.props.handOk();
  187. } else {
  188. message.warning(data.error[0].message);
  189. }
  190. }.bind(this));
  191. }
  192. });
  193. },
  194. cancelFun(){
  195. this.state.edit={};
  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.data = {};
  203. this.state.pictureUrl = [];
  204. this.state.Province=undefined;
  205. this.setState({
  206. edit:{content:'<p><br></p>'},
  207. publishPages:['web_policy_main','app_policy_main']
  208. })
  209. };
  210. },
  211. componentWillReceiveProps(nextProps) {
  212. if (!this.props.visible && nextProps.visible) {
  213. this.state.textFileList = [];
  214. this.state.videoFileList = [];
  215. if (nextProps.data.id) {
  216. this.loadData(nextProps.data.id);
  217. } else {
  218. this.state.data = {};
  219. this.state.pictureUrl = [];
  220. this.state.theData={};
  221. this.state.Province=undefined;
  222. this.setState({
  223. edit:{content:'<p><br></p>'},
  224. publishPages:['web_policy_main','app_policy_main']
  225. })
  226. };
  227. this.props.form.resetFields();
  228. };
  229. },
  230. render() {
  231. const theData = this.state.data || {};
  232. const { getFieldDecorator } = this.props.form;
  233. const FormItem = Form.Item
  234. const formItemLayout = {
  235. labelCol: { span: 6 },
  236. wrapperCol: { span: 12 },
  237. };
  238. const formItemLayouts = {
  239. labelCol: { span: 3 },
  240. wrapperCol: { span: 18 },
  241. };
  242. return (
  243. <div className='login'>
  244. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  245. <Spin spinning={this.state.loading}>
  246. <div className="clearfix">
  247. <FormItem className="half-item" {...formItemLayout} label="标题">
  248. {getFieldDecorator('title', {
  249. rules: [ { required: true, message: '此项为必填项!' } ],
  250. initialValue: theData.title
  251. })(<Input placeholder="请输入您的政策政策标题" maxLength={40}/>)}
  252. </FormItem>
  253. <FormItem className="half-item" {...formItemLayout} label="发布日期">
  254. {getFieldDecorator('releaseDateStr',{
  255. rules:[{required:true,message:"发布时间不能为空"}],
  256. initialValue: moment()
  257. })(<DatePicker showTime
  258. format="YYYY-MM-DD HH:mm:ss" />)}
  259. </FormItem>
  260. </div>
  261. <div className="clearfix">
  262. <FormItem className="half-item" {...formItemLayout} label="热门">
  263. {getFieldDecorator('hot', {
  264. rules: [ { required: true, message: '此项为必填项!' } ],
  265. initialValue: theData.hot
  266. })(
  267. <Radio.Group>
  268. <Radio value={0}>否</Radio>
  269. <Radio value={1}>是</Radio>
  270. </Radio.Group>
  271. )}
  272. </FormItem>
  273. <FormItem className="half-item" {...formItemLayout} label="政策来源">
  274. {getFieldDecorator('source', {
  275. rules: [ { required: true, message: '此项为必填项!' } ],
  276. initialValue: theData.source
  277. })(<Input placeholder="请输政策来源" maxLength={45} />)}
  278. </FormItem>
  279. <FormItem className="half-item" {...formItemLayout} label="来源链接">
  280. {getFieldDecorator('sourceUrl', {
  281. initialValue: theData.sourceUrl
  282. })(<Input placeholder="请输入政策来源链接" />)}
  283. </FormItem>
  284. <FormItem className="half-item"
  285. {...formItemLayout}
  286. label="省-市-区"
  287. >
  288. {getFieldDecorator('ProvinceCity', {
  289. rules: [ { required: true, message: '此项为必填项!' } ],
  290. initialValue: this.state.Province
  291. })(
  292. <Cascader options={provinceSelect()} placeholder="选择城市" />
  293. )}
  294. </FormItem>
  295. </div>
  296. <div className="clearfix">
  297. <FormItem {...formItemLayouts} label="关键字">
  298. {getFieldDecorator('keyword', {
  299. initialValue: theData.keyword
  300. })(<Input placeholder="请输入关键字" />)}
  301. </FormItem>
  302. </div>
  303. <div className="clearfix">
  304. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="政策图片">
  305. <PicturesWall
  306. fileList={this.getPictureUrl}
  307. pictureUrl={this.state.pictureUrl}
  308. visible={this.props.visible}
  309. />
  310. </FormItem>
  311. </div>
  312. <div className="clearfix">
  313. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="政策简介">
  314. {getFieldDecorator('summary', {
  315. rules: [ { required: true, message: '此项为必填项!' } ],
  316. initialValue: theData.summary
  317. })(<Input type="textarea" rows={4} maxLength={144} placeholder="输入政策简介" />)}
  318. </FormItem>
  319. </div>
  320. <div className="clearfix">
  321. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label={<span><i style={{color:"red",fontSize:14}}> * </i>政策正文</span>}>
  322. <Editors textContent={this.state.edit.content}
  323. uploadUrl={'/api/user/demand/uploadPicture'}
  324. uploadSign={'demand_picture'}
  325. handleRichText={(value) => { this.state.edit.content = value; }}
  326. visible={this.state.visible} />
  327. </FormItem>
  328. </div>
  329. {theData.createTime&&<div className="clearfix">
  330. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="是否发布">
  331. <span>{theData.releaseStatus?'已发布':'未发布'}</span>
  332. </FormItem>
  333. </div>}
  334. <div className="clearfix">
  335. <FormItem className="full-item"
  336. labelCol={{ span: 3 }}
  337. wrapperCol={{ span: 18 }} label="发布位置">
  338. {getFieldDecorator('publishPages', {
  339. rules: [ { required: false } ],
  340. initialValue: this.state.publishPages
  341. })(
  342. <Checkbox.Group>
  343. <Row>
  344. <Col span={6}><Checkbox value="web_index" >网站首页</Checkbox></Col>
  345. <Col span={6}><Checkbox value="web_policy_main" disabled>网站政策列表首页</Checkbox></Col>
  346. <Col span={6}><Checkbox value="web_tech_service_index">网站交易首页</Checkbox></Col>
  347. <Col span={6}><Checkbox value="app_index">APP首页</Checkbox></Col>
  348. <Col span={6}><Checkbox value="web_community">社区</Checkbox></Col>
  349. <Col span={6}><Checkbox value="app_policy_main" disabled>app政策首页</Checkbox></Col>
  350. </Row>
  351. </Checkbox.Group>
  352. )}
  353. </FormItem>
  354. </div>
  355. <div className="clearfix">
  356. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  357. <Button className="set-submit" type="primary" htmlType="submit">
  358. 保存草稿
  359. </Button>
  360. <Button
  361. className="set-submit"
  362. type="ghost"
  363. onClick={(e) => {
  364. this.handleSubmit(e,2)
  365. }}
  366. >
  367. 发布政策
  368. </Button>
  369. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  370. 取消
  371. </Button>
  372. </FormItem>
  373. </div>
  374. </Spin>
  375. </Form>
  376. </div>
  377. )
  378. }
  379. }));
  380. export default NewDetailForm;