policyForm.jsx 11 KB

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