activityOperation.jsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import React,{Component} from 'react';
  2. import {Form, Input, Modal, Spin, Radio, Button, Icon, Upload, message, Checkbox} from "antd";
  3. import TextArea from "antd/es/input/TextArea";
  4. import {
  5. splitUrl,
  6. beforeUploadFile,
  7. } from "@/tools.js";
  8. import $ from "jquery/src/ajax";
  9. import {companyList, getCompanyValueArray} from "../../../../common/configure";
  10. const FormItem = Form.Item;
  11. const RadioGroup = Radio.Group;
  12. // 上传文件
  13. const PicturesWall = React.createClass({
  14. getInitialState() {
  15. return {
  16. previewVisible: false,
  17. previewImage: "",
  18. fileList: this.props.pictureUrl
  19. };
  20. },
  21. getDefaultProps() {
  22. return {
  23. changeClick: this.modifyChange
  24. };
  25. },
  26. handleCancel() {
  27. this.setState({ previewVisible: false });
  28. },
  29. handlePreview(file) {
  30. this.setState({
  31. previewImage: file.url || file.thumbUrl,
  32. previewVisible: true
  33. });
  34. },
  35. handleChange(info) {
  36. let fileList = info.fileList;
  37. this.setState({ fileList });
  38. this.props.fileList(fileList);
  39. },
  40. componentWillReceiveProps(nextProps) {
  41. this.state.fileList = nextProps.pictureUrl;
  42. },
  43. render() {
  44. const { previewVisible, previewImage, fileList } = this.state;
  45. // 点击上传
  46. const uploadButton = (
  47. <div>
  48. <Icon type="plus" />
  49. <div className="ant-upload-text">点击上传</div>
  50. </div>
  51. );
  52. return (
  53. //上传组件
  54. <div style={{ display: "inline-block" }}>
  55. <Upload
  56. beforeUpload={beforeUploadFile}
  57. action={globalConfig.context + "/api/admin/activity/upload"}
  58. listType="picture-card"
  59. fileList={fileList}
  60. onPreview={this.handlePreview}
  61. onChange={this.handleChange}
  62. >
  63. {fileList && fileList.length >= 1 ? null : uploadButton}
  64. </Upload>
  65. <Modal
  66. maskClosable={false}
  67. visible={previewVisible}
  68. footer={null}
  69. onCancel={e => {
  70. this.setState({ previewVisible: false });
  71. }}
  72. >
  73. <img alt="" style={{ width: "100%" }} src={previewImage} />
  74. </Modal>
  75. </div>
  76. );
  77. }
  78. });
  79. class ActivityOperation extends Component{
  80. constructor(props) {
  81. super(props);
  82. this.state={
  83. loading: false,
  84. pictureUrl: [],
  85. details: {},
  86. locations: [],
  87. }
  88. this.submit = this.submit.bind(this);
  89. this.getOrgCodeUrl = this.getOrgCodeUrl.bind(this);
  90. }
  91. submit(e){
  92. e.preventDefault();
  93. let _this = this;
  94. this.props.form.validateFields((err, values) => {
  95. if(values.title.length > 6){
  96. message.warning('标题最多六个字');
  97. return;
  98. }
  99. if(this.state.locations.length === 0){
  100. message.warning('请选择城市');
  101. return;
  102. }
  103. if (!err) {
  104. this.setState({
  105. loading: true
  106. })
  107. values.pictureUrl = values.pictureUrl ? values.pictureUrl[0].response.data : undefined;
  108. let url = this.props.id ? "/api/admin/activity/update" : "/api/admin/activity/add";
  109. if(this.props.id){
  110. values.id = this.props.id;
  111. }
  112. values.locations = this.state.locations.join(',')
  113. $.ajax({
  114. method: "post",
  115. dataType: "json",
  116. crossDomain: false,
  117. url: globalConfig.context + url,
  118. data: values,
  119. success: function(data) {
  120. if (data.error && data.error.length) {
  121. message.warning(data.error[0].message);
  122. } else {
  123. message.success('添加成功')
  124. _this.props.onCancel();
  125. }
  126. }.bind(this)
  127. }).always(
  128. function() {
  129. this.setState({
  130. loading: false
  131. });
  132. }.bind(this)
  133. );
  134. }
  135. });
  136. }
  137. getOrgCodeUrl(e) {
  138. this.setState({ pictureUrl: e });
  139. this.props.form.setFieldsValue({
  140. pictureUrl: e
  141. })
  142. }
  143. getSelect(){
  144. $.ajax({
  145. method: "get",
  146. dataType: "json",
  147. crossDomain: false,
  148. url: globalConfig.context + "/api/admin/activity/select",
  149. data: {
  150. id:this.props.id,
  151. },
  152. success: function(data) {
  153. if (data.error.length) {
  154. message.warning(data.error[0].message);
  155. } else {
  156. this.props.form.setFieldsValue({
  157. title:data.data.title,
  158. publisher:data.data.publisher,
  159. sort:data.data.sort,
  160. releaseStatus:data.data.releaseStatus,
  161. content:data.data.content,
  162. pictureUrl:data.data.pictureUrl
  163. ? splitUrl(
  164. data.data.pictureUrl,
  165. ",",
  166. globalConfig.avatarUploadHost
  167. )
  168. : []
  169. });
  170. this.setState({
  171. locations:data.data.locations,
  172. checked:data.data.locations.length === companyList.length,
  173. indeterminate: data.data.locations.length < companyList.length,
  174. pictureUrl:data.data.pictureUrl
  175. ? splitUrl(
  176. data.data.pictureUrl,
  177. ",",
  178. globalConfig.avatarUploadHost
  179. )
  180. : []
  181. })
  182. }
  183. }.bind(this)
  184. }).always(
  185. function() {
  186. this.setState({
  187. loading: false
  188. });
  189. }.bind(this)
  190. );
  191. }
  192. componentDidMount() {
  193. if(this.props.id){
  194. this.getSelect();
  195. }
  196. }
  197. render() {
  198. const { getFieldDecorator } = this.props.form;
  199. return (
  200. <Modal
  201. className="customeDetails"
  202. title={this.props.id?"修改活动":"新增活动"}
  203. width="700px"
  204. onCancel={this.props.onCancel}
  205. visible={this.props.visible}
  206. footer={false}
  207. >
  208. <Form
  209. layout="horizontal"
  210. onSubmit={this.submit}
  211. id="demand-form"
  212. >
  213. <Spin spinning={this.state.loading}>
  214. <FormItem
  215. labelCol={{ span: 8 }}
  216. wrapperCol={{ span: 8 }}
  217. label="活动标题"
  218. >
  219. {getFieldDecorator('title', {
  220. rules: [{ required: true, message: '请输入活动标题!' }],
  221. })(
  222. <Input placeholder="请输入活动标题" />
  223. )}
  224. </FormItem>
  225. <FormItem
  226. labelCol={{ span: 8 }}
  227. wrapperCol={{ span: 8 }}
  228. label="发布人"
  229. >
  230. {getFieldDecorator('publisher', {
  231. rules: [{ required: true, message: '请输入发布人名称!' }],
  232. })(
  233. <Input placeholder="请输入发布人名称" />
  234. )}
  235. </FormItem>
  236. <FormItem
  237. labelCol={{ span: 8 }}
  238. wrapperCol={{ span: 8 }}
  239. label="排序"
  240. >
  241. {getFieldDecorator('sort', {
  242. rules: [{ required: true, message: '请输入排序!' }],
  243. })(
  244. <Input placeholder="请输入排序" type={'number'}/>
  245. )}
  246. </FormItem>
  247. <FormItem
  248. labelCol={{ span: 8 }}
  249. wrapperCol={{ span: 8 }}
  250. label="是否发布"
  251. >
  252. {getFieldDecorator('releaseStatus',{
  253. rules: [{ required: true, message: '请选择是否显示!' }],
  254. })(
  255. <RadioGroup>
  256. <Radio value={0}>否</Radio>
  257. <Radio value={1}>是</Radio>
  258. </RadioGroup>
  259. )}
  260. </FormItem>
  261. <FormItem
  262. labelCol={{ span: 8 }}
  263. wrapperCol={{ span: 12 }}
  264. label="公司"
  265. >
  266. <Checkbox indeterminate={this.state.indeterminate} checked={this.state.checked} onChange={(e)=>{
  267. this.setState({
  268. locations: e.target.checked ? getCompanyValueArray():[],
  269. indeterminate: false,
  270. checked:e.target.checked
  271. })
  272. }}>全选</Checkbox>
  273. <Checkbox.Group
  274. options={companyList}
  275. value={this.state.locations}
  276. onChange={(e) => {
  277. this.setState({
  278. locations: e,
  279. indeterminate: companyList.length > e.length,
  280. checked: companyList.length === e.length
  281. });
  282. }}
  283. />
  284. </FormItem>
  285. <FormItem
  286. labelCol={{ span: 8 }}
  287. wrapperCol={{ span: 8 }}
  288. label="活动简介"
  289. >
  290. {getFieldDecorator('content', {
  291. rules: [{ required: true, message: '请输入活动简介!' }],
  292. })(
  293. <TextArea placeholder="请输入活动简介" type={'number'}/>
  294. )}
  295. </FormItem>
  296. <FormItem
  297. labelCol={{ span: 8 }}
  298. wrapperCol={{ span: 8 }}
  299. label="活动大图"
  300. extra="图片尺寸:1240X440"
  301. >
  302. {getFieldDecorator('pictureUrl', {
  303. rules: [{ required: true, message: '请选择活动大图!' }],
  304. })(
  305. <PicturesWall
  306. fileList={this.getOrgCodeUrl}
  307. pictureUrl={this.state.pictureUrl}
  308. />
  309. )}
  310. </FormItem>
  311. <FormItem
  312. style={{
  313. display: 'flex',
  314. justifyContent: 'center',
  315. }}
  316. >
  317. <Button
  318. loading={this.state.loading}
  319. type="primary"
  320. htmlType="submit"
  321. >
  322. {this.props.id ? '保存':'添加' }
  323. </Button>
  324. </FormItem>
  325. </Spin>
  326. </Form>
  327. </Modal>
  328. );
  329. }
  330. }
  331. const ActivityOperationForm = Form.create()(ActivityOperation);
  332. export default ActivityOperationForm;