|
@@ -25,19 +25,18 @@ import java.io.PrintWriter;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* controller基础类
|
|
* controller基础类
|
|
|
- *
|
|
|
|
|
- * @author yingp
|
|
|
|
|
*
|
|
*
|
|
|
|
|
+ * @author yingp
|
|
|
*/
|
|
*/
|
|
|
public class BaseController {
|
|
public class BaseController {
|
|
|
|
|
|
|
|
- protected static final String defultCharset = "UTF-8";
|
|
|
|
|
|
|
+ protected static final String defultCharset = "UTF-8";
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- protected HttpServletRequest request;
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected HttpServletRequest request;
|
|
|
|
|
|
|
|
- @Autowired
|
|
|
|
|
- protected HttpServletResponse response;
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected HttpServletResponse response;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
protected TokenService tokenService;
|
|
protected TokenService tokenService;
|
|
@@ -48,79 +47,77 @@ public class BaseController {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
protected SettingService settingService;
|
|
protected SettingService settingService;
|
|
|
|
|
|
|
|
- protected static boolean isSuccess(ModelMap map) {
|
|
|
|
|
- return Boolean.TRUE.equals(map.get("success"));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static Object getContent(ModelMap map) {
|
|
|
|
|
- return map.get("content");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static ModelMap success() {
|
|
|
|
|
- return new ModelMap("success", true);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static ModelMap success(Object data) {
|
|
|
|
|
- return new ModelMap("success", true).addAttribute("content", data);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static ModelMap error(String errMsg) {
|
|
|
|
|
- return new ModelMap("error", true).addAttribute("errMsg", errMsg);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static ModelMap error(Object detail) {
|
|
|
|
|
- return new ModelMap("error", true).addAttribute("errDetail", detail);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected static ModelMap error(String errCode, String errMsg) {
|
|
|
|
|
- return new ModelMap("error", true).addAttribute("errCode", errCode).addAttribute("errMsg", errMsg);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 输出json格式
|
|
|
|
|
- *
|
|
|
|
|
- * @param obj
|
|
|
|
|
- * @throws IOException
|
|
|
|
|
- */
|
|
|
|
|
- protected void printJson(Object obj) throws IOException {
|
|
|
|
|
- response.addHeader("Content-Type", "application/json; charset=" + defultCharset);
|
|
|
|
|
- PrintWriter printWriter = response.getWriter();
|
|
|
|
|
- printWriter.append(JSON.toJSONString(obj));
|
|
|
|
|
- printWriter.flush();
|
|
|
|
|
- printWriter.close();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 响应Ajax请求
|
|
|
|
|
- *
|
|
|
|
|
- * @param content
|
|
|
|
|
- * 响应内容
|
|
|
|
|
- * @throws IOException
|
|
|
|
|
- */
|
|
|
|
|
- protected void printJsonP(String callback, Object content) throws IOException {
|
|
|
|
|
- if (!content.getClass().isAssignableFrom(String.class)) {
|
|
|
|
|
- content = JSON.toJSON(content);
|
|
|
|
|
- }
|
|
|
|
|
- response.setContentType("text/html;charset=" + defultCharset);
|
|
|
|
|
- PrintWriter out = response.getWriter();
|
|
|
|
|
- out.print(callback + "(" + content + ")");
|
|
|
|
|
- out.flush();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 输出流
|
|
|
|
|
- *
|
|
|
|
|
- * @param fileName
|
|
|
|
|
- * 文件名
|
|
|
|
|
- * @param bytes
|
|
|
|
|
- * @throws IOException
|
|
|
|
|
- */
|
|
|
|
|
- protected ResponseEntity<byte[]> outputStream(String fileName, byte[] bytes) {
|
|
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
- headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
|
- headers.setContentDispositionFormData("attachment", fileName);
|
|
|
|
|
- return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ protected static boolean isSuccess(ModelMap map) {
|
|
|
|
|
+ return Boolean.TRUE.equals(map.get("success"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static Object getContent(ModelMap map) {
|
|
|
|
|
+ return map.get("content");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static ModelMap success() {
|
|
|
|
|
+ return new ModelMap("success", true);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static ModelMap success(Object data) {
|
|
|
|
|
+ return new ModelMap("success", true).addAttribute("content", data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static ModelMap error(String errMsg) {
|
|
|
|
|
+ return new ModelMap("error", true).addAttribute("errMsg", errMsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static ModelMap error(Object detail) {
|
|
|
|
|
+ return new ModelMap("error", true).addAttribute("errDetail", detail);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected static ModelMap error(String errCode, String errMsg) {
|
|
|
|
|
+ return new ModelMap("error", true).addAttribute("errCode", errCode).addAttribute("errMsg", errMsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 输出json格式
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param obj
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ protected void printJson(Object obj) throws IOException {
|
|
|
|
|
+ response.addHeader("Content-Type", "application/json; charset=" + defultCharset);
|
|
|
|
|
+ PrintWriter printWriter = response.getWriter();
|
|
|
|
|
+ printWriter.append(JSON.toJSONString(obj));
|
|
|
|
|
+ printWriter.flush();
|
|
|
|
|
+ printWriter.close();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 响应Ajax请求
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param content 响应内容
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ protected void printJsonP(String callback, Object content) throws IOException {
|
|
|
|
|
+ if (!content.getClass().isAssignableFrom(String.class)) {
|
|
|
|
|
+ content = JSON.toJSON(content);
|
|
|
|
|
+ }
|
|
|
|
|
+ response.setContentType("text/html;charset=" + defultCharset);
|
|
|
|
|
+ PrintWriter out = response.getWriter();
|
|
|
|
|
+ out.print(callback + "(" + content + ")");
|
|
|
|
|
+ out.flush();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 输出流
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param fileName 文件名
|
|
|
|
|
+ * @param bytes
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ protected ResponseEntity<byte[]> outputStream(String fileName, byte[] bytes) {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
|
|
+ headers.setContentDispositionFormData("attachment", fileName);
|
|
|
|
|
+ return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 校验密码强度
|
|
* 校验密码强度
|
|
@@ -129,30 +126,30 @@ public class BaseController {
|
|
|
* @return PasswordStrength枚举
|
|
* @return PasswordStrength枚举
|
|
|
* @throws VisibleError 用户可见异常
|
|
* @throws VisibleError 用户可见异常
|
|
|
*/
|
|
*/
|
|
|
- protected PasswordStrength checkPasswordLevel(String password) throws VisibleError {
|
|
|
|
|
- String strongRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$";
|
|
|
|
|
- String mediumRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$";
|
|
|
|
|
- if (password == null) {
|
|
|
|
|
- throw new VisibleError("密码不能为空");
|
|
|
|
|
- }
|
|
|
|
|
- if (password.matches(strongRegex)) {
|
|
|
|
|
- return PasswordStrength.STRONG;
|
|
|
|
|
- } else if (password.matches(mediumRegex)) {
|
|
|
|
|
- return PasswordStrength.MEDIUM;
|
|
|
|
|
- } else {
|
|
|
|
|
- return PasswordStrength.WEAK;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ protected PasswordStrength checkPasswordLevel(String password) throws VisibleError {
|
|
|
|
|
+ String strongRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]))|((?=.*[0-9])((?=.*[a-zA-Z]))(?=.*[^a-zA-Z0-9]))).*$";
|
|
|
|
|
+ String mediumRegex = "^(?=.{8,20})(((?=.*[0-9])(?=.*[a-z]))|((?=.*[0-9])(?=.*[A-Z]))).*$";
|
|
|
|
|
+ if (password == null) {
|
|
|
|
|
+ throw new VisibleError("密码不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (password.matches(strongRegex)) {
|
|
|
|
|
+ return PasswordStrength.STRONG;
|
|
|
|
|
+ } else if (password.matches(mediumRegex)) {
|
|
|
|
|
+ return PasswordStrength.MEDIUM;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return PasswordStrength.WEAK;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取手机号验证码
|
|
* 获取手机号验证码
|
|
|
*
|
|
*
|
|
|
* @return tokenId
|
|
* @return tokenId
|
|
|
*/
|
|
*/
|
|
|
- protected String getMobileCode(String mobile) {
|
|
|
|
|
|
|
+ protected String getMobileCode(String mobile) {
|
|
|
// 随机获得验证码
|
|
// 随机获得验证码
|
|
|
String code = StringUtil.getRandomNumber(6);
|
|
String code = StringUtil.getRandomNumber(6);
|
|
|
- Token token = new Token(code, 10*60);
|
|
|
|
|
|
|
+ Token token = new Token(code, 10 * 60);
|
|
|
|
|
|
|
|
// 设置绑定手机,防止获取验证码之后修改手机号
|
|
// 设置绑定手机,防止获取验证码之后修改手机号
|
|
|
token.setMobile(mobile);
|
|
token.setMobile(mobile);
|
|
@@ -182,12 +179,12 @@ public class BaseController {
|
|
|
/**
|
|
/**
|
|
|
* 校验手机号
|
|
* 校验手机号
|
|
|
*
|
|
*
|
|
|
- * @param token 验证码tokenID
|
|
|
|
|
|
|
+ * @param token 验证码tokenID
|
|
|
* @param mobile 手机号
|
|
* @param mobile 手机号
|
|
|
- * @param code 验证码
|
|
|
|
|
|
|
+ * @param code 验证码
|
|
|
* @return
|
|
* @return
|
|
|
* @throws VisibleError 校验失败则抛异常
|
|
* @throws VisibleError 校验失败则抛异常
|
|
|
- * 当参数异常,token过期或者token绑定的手机号不对时抛出此异常
|
|
|
|
|
|
|
+ * 当参数异常,token过期或者token绑定的手机号不对时抛出此异常
|
|
|
*/
|
|
*/
|
|
|
protected void checkMobileCode(String token, String mobile, String code) {
|
|
protected void checkMobileCode(String token, String mobile, String code) {
|
|
|
// 校验参数
|
|
// 校验参数
|
|
@@ -209,27 +206,27 @@ public class BaseController {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 校验手机号格式
|
|
|
|
|
- *
|
|
|
|
|
- * @param mobile 手机号
|
|
|
|
|
- * @param mobileArea 手机号所属区域
|
|
|
|
|
- */
|
|
|
|
|
- protected void checkMobile(String mobile, String mobileArea) {
|
|
|
|
|
- // 由于现在不考虑手机号所属区域,默认为中国大陆
|
|
|
|
|
- mobileArea = mobileArea == null ? Const.CONTINENT : mobileArea;
|
|
|
|
|
-
|
|
|
|
|
- // 校验手机号
|
|
|
|
|
- if (Const.CONTINENT.equals(mobileArea)) {
|
|
|
|
|
- if (!mobile.matches(Const.REGEXP_MOBILE_CONTINENT)) {
|
|
|
|
|
- throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
- }
|
|
|
|
|
- } else if (Const.HONGKONG.equals(mobileArea)) {
|
|
|
|
|
- if (!mobile.matches(Const.REGEXP_MOBILE_HONGKONG)) {
|
|
|
|
|
- throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new VisibleError("未找到所选地区");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 校验手机号格式
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mobile 手机号
|
|
|
|
|
+ * @param mobileArea 手机号所属区域
|
|
|
|
|
+ */
|
|
|
|
|
+ protected void checkMobile(String mobile, String mobileArea) {
|
|
|
|
|
+ // 由于现在不考虑手机号所属区域,默认为中国大陆
|
|
|
|
|
+ mobileArea = mobileArea == null ? Const.CONTINENT : mobileArea;
|
|
|
|
|
+
|
|
|
|
|
+ // 校验手机号
|
|
|
|
|
+ if (Const.CONTINENT.equals(mobileArea)) {
|
|
|
|
|
+ if (!mobile.matches(Const.REGEXP_MOBILE_CONTINENT)) {
|
|
|
|
|
+ throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (Const.HONGKONG.equals(mobileArea)) {
|
|
|
|
|
+ if (!mobile.matches(Const.REGEXP_MOBILE_HONGKONG)) {
|
|
|
|
|
+ throw new VisibleError("请输入正确的手机号格式");
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ throw new VisibleError("未找到所选地区");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|