policyForm.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. };
  86. },
  87. loadData(id) {
  88. this.setState({
  89. loading: true
  90. });
  91. $.ajax({
  92. method: "get",
  93. dataType: "json",
  94. crossDomain: false,
  95. url: globalConfig.context + '/api/admin/policy/detail' ,
  96. data: {
  97. id: id
  98. },
  99. success: function (data) {
  100. let thisData = data.data,provice=[];
  101. if (!thisData) {
  102. if (data.error && data.error.length) {
  103. message.warning(data.error[0].message);
  104. };
  105. thisData = {};
  106. };
  107. let proArr=[];
  108. proArr.push(thisData.province);
  109. this.setState({
  110. id:thisData.id,
  111. data: thisData,
  112. Province:proArr,
  113. publishPages:thisData.publishPages?thisData.publishPages:['web_policy_main','app_policy_main'],
  114. auditStatus:thisData.auditStatus,
  115. edit:{content:thisData.content},
  116. pictureUrl: thisData.titleImg ? splitUrl(thisData.titleImg, ',', globalConfig.avatarHost + '/upload') : []
  117. });
  118. }.bind(this),
  119. }).always(function () {
  120. this.setState({
  121. loading: false
  122. });
  123. }.bind(this));
  124. },
  125. getPictureUrl(e) {
  126. this.setState({ pictureUrl: e });
  127. },
  128. handleSubmit(e,index) {
  129. e.preventDefault();
  130. this.props.form.validateFields((err, values) => {
  131. //url转化
  132. if((this.state.edit).content==undefined||!(this.state.edit).content||(this.state.edit).content=='<p><br></p>'){
  133. message.warning('请填写政策正文');
  134. return false;
  135. }
  136. let thePictureUrl = [];
  137. if (this.state.pictureUrl.length) {
  138. let picArr = [];
  139. this.state.pictureUrl.map(function (item) {
  140. if ( item.response && item.response.data && item.response.data.length ){
  141. picArr.push(item.response.data);
  142. }
  143. });
  144. thePictureUrl = picArr.join(",");
  145. };
  146. if (!err) {
  147. let publishPages =(values.publishPages).join(',')
  148. this.setState({
  149. loading: true
  150. });
  151. $.ajax({
  152. method: "post",
  153. dataType: "json",
  154. async:true,
  155. url: this.props.data.id ? globalConfig.context + '/api/admin/policy/update' : globalConfig.context + '/api/admin/policy/apply',
  156. data: {
  157. id: this.props.data.id,
  158. type:0,
  159. title: values.title,
  160. author: values.author,
  161. titleImg: thePictureUrl.length?thePictureUrl:'',
  162. content:this.state.edit.content,
  163. hot:values.hot,
  164. source:values.source,
  165. sourceUrl:values.sourceUrl,
  166. keyword:values.keyword,
  167. auditStatus:index,
  168. summary: values.summary,
  169. province: (values.ProvinceCity)[0],
  170. publishPages:publishPages
  171. }
  172. }).done(function (data) {
  173. this.setState({
  174. loading: false
  175. });
  176. if (!data.error.length) {
  177. message.success('操作成功!');
  178. this.setState({
  179. visible: false
  180. });
  181. this.props.handOk();
  182. } else {
  183. message.warning(data.error[0].message);
  184. }
  185. }.bind(this));
  186. }
  187. });
  188. },
  189. cancelFun(){
  190. this.state.edit={};
  191. this.props.closeDesc();
  192. },
  193. componentWillMount() {
  194. if (this.props.data.id) {
  195. this.loadData(this.props.data.id);
  196. } else {
  197. this.state.data = {};
  198. this.state.pictureUrl = [];
  199. this.state.Province=undefined;
  200. this.setState({
  201. edit:{content:'<p><br></p>'},
  202. publishPages:['web_policy_main','app_policy_main']
  203. })
  204. };
  205. },
  206. componentWillReceiveProps(nextProps) {
  207. if (!this.props.visible && nextProps.visible) {
  208. this.state.textFileList = [];
  209. this.state.videoFileList = [];
  210. if (nextProps.data.id) {
  211. this.loadData(nextProps.data.id);
  212. } else {
  213. this.state.data = {};
  214. this.state.pictureUrl = [];
  215. this.state.theData={};
  216. this.state.Province=undefined;
  217. this.setState({
  218. edit:{content:'<p><br></p>'},
  219. publishPages:['web_policy_main','app_policy_main']
  220. })
  221. };
  222. this.props.form.resetFields();
  223. };
  224. },
  225. render() {
  226. const theData = this.state.data || {};
  227. const { getFieldDecorator } = this.props.form;
  228. const FormItem = Form.Item
  229. const formItemLayout = {
  230. labelCol: { span: 6 },
  231. wrapperCol: { span: 12 },
  232. };
  233. const formItemLayouts = {
  234. labelCol: { span: 3 },
  235. wrapperCol: { span: 18 },
  236. };
  237. return (
  238. <div className='login'>
  239. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  240. <Spin spinning={this.state.loading}>
  241. <div className="clearfix">
  242. <FormItem className="half-item" {...formItemLayout} label="标题">
  243. {getFieldDecorator('title', {
  244. rules: [ { required: true, message: '此项为必填项!' } ],
  245. initialValue: theData.title
  246. })(<Input placeholder="请输入您的政策政策标题" />)}
  247. </FormItem>
  248. </div>
  249. <div className="clearfix">
  250. <FormItem className="half-item" {...formItemLayout} label="热门">
  251. {getFieldDecorator('hot', {
  252. rules: [ { required: true, message: '此项为必填项!' } ],
  253. initialValue: theData.hot
  254. })(
  255. <Radio.Group>
  256. <Radio value={0}>否</Radio>
  257. <Radio value={1}>是</Radio>
  258. </Radio.Group>
  259. )}
  260. </FormItem>
  261. <FormItem className="half-item" {...formItemLayout} label="政策来源">
  262. {getFieldDecorator('source', {
  263. rules: [ { required: true, message: '此项为必填项!' } ],
  264. initialValue: theData.source
  265. })(<Input placeholder="请输政策来源" />)}
  266. </FormItem>
  267. <FormItem className="half-item" {...formItemLayout} label="来源链接">
  268. {getFieldDecorator('sourceUrl', {
  269. initialValue: theData.sourceUrl
  270. })(<Input placeholder="请输入政策来源链接" />)}
  271. </FormItem>
  272. <FormItem className="half-item"
  273. {...formItemLayout}
  274. label="省-市-区"
  275. >
  276. {getFieldDecorator('ProvinceCity', {
  277. rules: [ { required: true, message: '此项为必填项!' } ],
  278. initialValue: this.state.Province
  279. })(
  280. <Cascader options={provinceSelect()} placeholder="选择城市" />
  281. )}
  282. </FormItem>
  283. </div>
  284. <div className="clearfix">
  285. <FormItem {...formItemLayouts} label="关键字">
  286. {getFieldDecorator('keyword', {
  287. initialValue: theData.keyword
  288. })(<Input placeholder="请输入关键字" />)}
  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('summary', {
  303. rules: [ { required: true, message: '此项为必填项!' } ],
  304. initialValue: theData.summary
  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/user/demand/uploadPicture'}
  312. uploadSign={'demand_picture'}
  313. handleRichText={(value) => { this.state.edit.content = value; }}
  314. visible={this.state.visible} />
  315. </FormItem>
  316. </div>
  317. {theData.createTime&&<div className="clearfix">
  318. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="是否发布">
  319. <span>{theData.releaseStatus?'已发布':'未发布'}</span>
  320. </FormItem>
  321. </div>}
  322. <div className="clearfix">
  323. <FormItem className="full-item"
  324. labelCol={{ span: 3 }}
  325. wrapperCol={{ span: 18 }} label="发布位置">
  326. {getFieldDecorator('publishPages', {
  327. rules: [ { required: false } ],
  328. initialValue: this.state.publishPages
  329. })(
  330. <Checkbox.Group>
  331. <Row>
  332. <Col span={6}><Checkbox value="web_index" >网站首页</Checkbox></Col>
  333. <Col span={6}><Checkbox value="web_policy_main" disabled>网站政策列表首页</Checkbox></Col>
  334. <Col span={6}><Checkbox value="web_tech_service_index">网站交易首页</Checkbox></Col>
  335. <Col span={6}><Checkbox value="app_index">APP首页</Checkbox></Col>
  336. <Col span={6}><Checkbox value="app_policy_main" disabled>app政策首页</Checkbox></Col>
  337. </Row>
  338. </Checkbox.Group>
  339. )}
  340. </FormItem>
  341. </div>
  342. <div className="clearfix">
  343. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  344. <Button className="set-submit" type="primary" htmlType="submit">
  345. 保存草稿
  346. </Button>
  347. <Button
  348. className="set-submit"
  349. type="ghost"
  350. onClick={(e) => {
  351. this.handleSubmit(e,2)
  352. }}
  353. >
  354. 发布政策
  355. </Button>
  356. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  357. 取消
  358. </Button>
  359. </FormItem>
  360. </div>
  361. </Spin>
  362. </Form>
  363. </div>
  364. )
  365. }
  366. }));
  367. export default NewDetailForm;