254 lines
6.7 KiB
Java
254 lines
6.7 KiB
Java
/*
|
|
* Copyright 2013-2017 vverp.com. All rights reserved.
|
|
* Support: http://www.vverp.com
|
|
* License: http://www.vverp.com/license
|
|
*/
|
|
package com.vverp.controller.admin;
|
|
|
|
import com.vverp.dto.LayuiPage;
|
|
import com.vverp.entity.Log;
|
|
import com.vverp.util.PageUtils;
|
|
import org.apache.commons.lang.exception.ExceptionUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.web.bind.WebDataBinder;
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
import org.springframework.web.bind.annotation.InitBinder;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import org.springframework.web.context.request.RequestAttributes;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.validation.ConstraintViolation;
|
|
import javax.validation.Validator;
|
|
import java.math.BigDecimal;
|
|
import java.util.Collection;
|
|
import java.util.Date;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import com.vverp.moli.util.*;
|
|
|
|
/**
|
|
* Controller - 基类
|
|
*
|
|
* @author
|
|
* @version 1.0
|
|
*/
|
|
public class BaseController {
|
|
|
|
/** Logger */
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
protected static final Message SUCCESS = Message.success("操作成功");
|
|
|
|
protected static final Message ERROR = Message.success("操作失败");
|
|
|
|
/** "验证结果"属性名称 */
|
|
private static final String CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME = "constraintViolations";
|
|
|
|
@Resource(name = "validator")
|
|
private Validator validator;
|
|
|
|
/**
|
|
* 数据绑定
|
|
*
|
|
* @param binder
|
|
* WebDataBinder
|
|
*/
|
|
@InitBinder
|
|
protected void initBinder(WebDataBinder binder) {
|
|
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
|
|
binder.registerCustomEditor(Date.class, new DateEditor(true));
|
|
binder.registerCustomEditor(String.class, "password", new StringEditor(true));
|
|
}
|
|
|
|
|
|
@ExceptionHandler
|
|
@ResponseBody
|
|
public Message exceptionHandler(Exception exception) {
|
|
exception.printStackTrace();
|
|
return Message.error("异常", exception.getMessage());
|
|
}
|
|
|
|
/**
|
|
* 数据验证
|
|
*
|
|
* @param target
|
|
* 验证对象
|
|
* @param groups
|
|
* 验证组
|
|
* @return 验证结果
|
|
*/
|
|
protected boolean isValid(Object target, Class<?>... groups) {
|
|
Assert.notNull(target);
|
|
|
|
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(target, groups);
|
|
if (constraintViolations.isEmpty()) {
|
|
return true;
|
|
}
|
|
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
|
requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations, RequestAttributes.SCOPE_REQUEST);
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 数据验证
|
|
*
|
|
* @param targets
|
|
* 验证对象
|
|
* @param groups
|
|
* 验证组
|
|
* @return 验证结果
|
|
*/
|
|
protected boolean isValid(Collection<Object> targets, Class<?>... groups) {
|
|
Assert.notEmpty(targets);
|
|
|
|
for (Object target : targets) {
|
|
if (!isValid(target, groups)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 数据验证
|
|
*
|
|
* @param type
|
|
* 类型
|
|
* @param property
|
|
* 属性
|
|
* @param value
|
|
* 值
|
|
* @param groups
|
|
* 验证组
|
|
* @return 验证结果
|
|
*/
|
|
protected boolean isValid(Class<?> type, String property, Object value, Class<?>... groups) {
|
|
Assert.notNull(type);
|
|
Assert.hasText(property);
|
|
|
|
Set<?> constraintViolations = validator.validateValue(type, property, value, groups);
|
|
if (constraintViolations.isEmpty()) {
|
|
return true;
|
|
}
|
|
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
|
requestAttributes.setAttribute(CONSTRAINT_VIOLATIONS_ATTRIBUTE_NAME, constraintViolations, RequestAttributes.SCOPE_REQUEST);
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 数据验证
|
|
*
|
|
* @param type
|
|
* 类型
|
|
* @param properties
|
|
* 属性
|
|
* @param groups
|
|
* 验证组
|
|
* @return 验证结果
|
|
*/
|
|
protected boolean isValid(Class<?> type, Map<String, Object> properties, Class<?>... groups) {
|
|
Assert.notNull(type);
|
|
Assert.notEmpty(properties);
|
|
|
|
for (Map.Entry<String, Object> entry : properties.entrySet()) {
|
|
if (!isValid(type, entry.getKey(), entry.getValue(), groups)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 货币格式化
|
|
*
|
|
* @param amount
|
|
* 金额
|
|
* @param showSign
|
|
* 显示标志
|
|
* @param showUnit
|
|
* 显示单位
|
|
* @return 货币格式化
|
|
*/
|
|
protected String currency(BigDecimal amount, boolean showSign, boolean showUnit) {
|
|
// Setting setting = SystemUtils.getSetting();
|
|
// String price = setting.setScale(amount).toString();
|
|
// if (showSign) {
|
|
// price = setting.getCurrencySign() + price;
|
|
// }
|
|
// if (showUnit) {
|
|
// price += setting.getCurrencyUnit();
|
|
// }
|
|
// return price;
|
|
return "";
|
|
}
|
|
|
|
/**
|
|
* 获取国际化消息
|
|
*
|
|
* @param code
|
|
* 代码
|
|
* @param args
|
|
* 参数
|
|
* @return 国际化消息
|
|
*/
|
|
protected String message(String code, Object... args) {
|
|
return SpringUtils.getMessage(code, args);
|
|
}
|
|
|
|
/**
|
|
* 添加日志
|
|
*
|
|
* @param content
|
|
* 内容
|
|
*/
|
|
protected void addLog(String content) {
|
|
if (content != null) {
|
|
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
|
requestAttributes.setAttribute(Log.LOG_CONTENT_ATTRIBUTE_NAME, content, RequestAttributes.SCOPE_REQUEST);
|
|
}
|
|
}
|
|
|
|
protected RespData respDataWithHandle(Runnable runnable) {
|
|
return respDataWithHandle(runnable, "操作失败");
|
|
}
|
|
|
|
protected RespData respDataWithHandle(Runnable runnable, String errorMsg) {
|
|
try {
|
|
runnable.run();
|
|
return RespData.success();
|
|
} catch (RuntimeException e) {
|
|
logger.error("controller error: {}", ExceptionUtils.getFullStackTrace(e));
|
|
return RespData.error(e.getMessage());
|
|
} catch (Exception e) {
|
|
logger.error("controller error: {}", ExceptionUtils.getFullStackTrace(e));
|
|
return RespData.error(errorMsg);
|
|
}
|
|
}
|
|
|
|
protected Message msgDataWithHandle(Runnable runnable) {
|
|
return msgDataWithHandle(runnable, "操作失败");
|
|
}
|
|
|
|
protected Message msgDataWithHandle(Runnable runnable, String errorMsg) {
|
|
try {
|
|
runnable.run();
|
|
return Message.success("success");
|
|
} catch (RuntimeException e) {
|
|
logger.error("controller error: {}", ExceptionUtils.getFullStackTrace(e));
|
|
return Message.error(e.getMessage());
|
|
} catch (Exception e) {
|
|
logger.error("controller error: {}", ExceptionUtils.getFullStackTrace(e));
|
|
return Message.error(errorMsg);
|
|
}
|
|
}
|
|
|
|
protected <T> LayuiPage<T> toLayuiPage(RespData respData) {
|
|
return PageUtils.toLayuiPage(respData);
|
|
}
|
|
|
|
} |