ModalConfirm.js 2.8 KB
Newer Older
王绍森's avatar
王绍森 committed
1 2 3 4 5 6 7 8 9
/**
 * 钟是志
 * 2019年2月26日
 * 用于 弹出提示信息 确认用户是否执行XXX操作
 * 一个按钮 + 点击按钮显示的提示
 * 点确定 调接口->关闭弹窗 刷新页面
 * */
import ButtonDiy from '@/baseComponent/ButtonDiy';
import React, { Component, Fragment } from 'react';
10
import { message, Modal } from 'antd';
王绍森's avatar
王绍森 committed
11 12 13 14
import PropTypes from 'prop-types';
import * as service from '@/highOrderComponent/Service';

export default class ModalConfirm extends Component {
15 16 17 18 19 20 21 22 23 24 25 26 27 28
	handleConfirm = () => {
		const {
			selectRows,
			name,
			contentSentence,
			onlyOne,
			checkSelectRow,
			noNeedCheckSelect,
			listData,
		} = this.props;
		if (typeof noNeedCheckSelect === 'undefined' && !selectRows.length) {
			message.warning('请至少选择一条数据');
			return false;
		}
王绍森's avatar
王绍森 committed
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
		if (onlyOne && selectRows.length > 1) {
			message.warning('只能选择一条数据进行操作');
			return false;
		}
		if (checkSelectRow) {
			let res = checkSelectRow(selectRows, listData);
			if (res && res.result === false) {
				message.warning(res.info);
				return false;
			}
		}
		Modal.confirm({
			title: name,
			content: contentSentence,
			onOk: this.handleOk,
			okText: '确定',
			cancelText: '取消',
		});
	};
王绍森's avatar
王绍森 committed
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	handleOk = () => {
		const {
			selectRows,
			postKey,
			sourceKey,
			url,
			responseCallBack,
			getPage,
			fields,
			formValues,
			beforeUpdate,
			search,
		} = this.props;
		let data = {};
		if (fields) {
			data = { ...fields };
		}
		data[postKey] = selectRows.map((item) => {
			return item[sourceKey];
		});
		data[postKey] = data[postKey].join(',');
		if (beforeUpdate) {
			data = beforeUpdate(data, selectRows, formValues, search);
		}
		service.addOrUpdate(data, url).then((response) => {
			if (responseCallBack && !responseCallBack(response)) {
				return false;
			}
			if (!response) {
				return false;
			} else {
				message.success('操作成功');
				getPage();
			}
		});
	};
王绍森's avatar
王绍森 committed
86

87 88 89 90 91 92 93 94
	render() {
		const { name, className } = this.props;
		return (
			<Fragment>
				<ButtonDiy name={name} className={className} handleClick={this.handleConfirm} />
			</Fragment>
		);
	}
王绍森's avatar
王绍森 committed
95 96 97
}

ModalConfirm.propTypes = {
98 99 100 101 102 103 104 105
	name: PropTypes.string, // 按钮名称和 弹窗的标题
	selectRows: PropTypes.array.isRequired, // 选择的数据.数组类型
	className: PropTypes.string, // 按钮样式
	sourceKey: PropTypes.string.isRequired, // 通过什么字段删除
	postKey: PropTypes.string, // 提交的字段名.
	url: PropTypes.string.isRequired, // 接口url
	responseCallBack: PropTypes.func, // 接口返回数据检查
	getPage: PropTypes.func.isRequired, // 刷新页面的方法
王绍森's avatar
王绍森 committed
106 107 108
};

ModalConfirm.defaultProps = {
109 110 111 112 113 114 115 116 117 118
	name: '一键审核通过',
	className: 'defaultBlue',
	url: 'asdasd/asdasd',
	contentSentence: '您确认xxxxx吗?',
	sourceKey: 'id',
	postKey: 'ids',
	selectRows: [],
	responseCallBack: (response) => {
		return !!response;
	},
王绍森's avatar
王绍森 committed
119
};