From: Tang Cheng Date: Sat, 13 Jul 2019 12:49:09 +0000 (+0800) Subject: 优化用户管理模块,增加 jpa beanvalidation 错误信息的处理 X-Git-Tag: 1.0.0^2~54 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=9364142321419bf43f8dccc1fed84607b02a2e41;p=epayment%2Ffood_payapi.git 优化用户管理模块,增加 jpa beanvalidation 错误信息的处理 --- diff --git a/payapi-common/src/main/java/com/supwisdom/dlpay/api/annotation/IDNoCheck.java b/payapi-common/src/main/java/com/supwisdom/dlpay/api/annotation/IDNoCheck.java index 1760452c..16061340 100644 --- a/payapi-common/src/main/java/com/supwisdom/dlpay/api/annotation/IDNoCheck.java +++ b/payapi-common/src/main/java/com/supwisdom/dlpay/api/annotation/IDNoCheck.java @@ -33,5 +33,5 @@ public @interface IDNoCheck { * * @return */ - IDTypes value(); + IDTypes value() default IDTypes.IDTYPE_LIST; } \ No newline at end of file diff --git a/payapi/src/main/java/com/supwisdom/dlpay/api/domain/TPerson.java b/payapi/src/main/java/com/supwisdom/dlpay/api/domain/TPerson.java index 9e31db8c..a8cfbd6a 100644 --- a/payapi/src/main/java/com/supwisdom/dlpay/api/domain/TPerson.java +++ b/payapi/src/main/java/com/supwisdom/dlpay/api/domain/TPerson.java @@ -1,14 +1,18 @@ package com.supwisdom.dlpay.api.domain; +import com.supwisdom.dlpay.api.annotation.IDNoCheck; +import com.supwisdom.dlpay.api.annotation.MobileNumber; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; +import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; @Entity @Table(name = "TB_PERSON", indexes = {@Index(name = "person_name_idx", columnList = "name"), @Index(name = "person_idno_uk", unique = true, columnList = "idtype,idno")}) +@IDNoCheck(message = "证件信息错误", idno = "idno", idtype = "idtype") public class TPerson { @Id @GenericGenerator(name = "idGenerator", strategy = "uuid") @@ -39,12 +43,14 @@ public class TPerson { private String nation; @Column(name = "EMAIL", length = 60) + @Email(message = "电子邮件格式错误") private String email; @Column(name = "TEL", length = 20) private String tel; @Column(name = "MOBILE", length = 30) + @MobileNumber(message = "手机号格式错误") private String mobile; @Column(name = "ADDR", length = 240) diff --git a/payapi/src/main/java/com/supwisdom/dlpay/system/controller/DictPoolAction.java b/payapi/src/main/java/com/supwisdom/dlpay/system/controller/DictPoolAction.java index 9c78b008..16e68da1 100644 --- a/payapi/src/main/java/com/supwisdom/dlpay/system/controller/DictPoolAction.java +++ b/payapi/src/main/java/com/supwisdom/dlpay/system/controller/DictPoolAction.java @@ -5,6 +5,7 @@ import com.supwisdom.dlpay.system.service.DictionaryDataService; import com.supwisdom.dlpay.system.service.DictionaryProxy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; @@ -20,7 +21,7 @@ public class DictPoolAction { } @GetMapping("/dictpool") - public Map getDictDataByDicttype(@FormParam("dicttype") String dictType, HttpServletRequest request) { + public Map getDictDataByDicttype(@RequestParam("dicttype") String dictType, HttpServletRequest request) { dictType = request.getParameter("dicttype"); return dictionaryProxy.getDictionaryAsMap(dictType); } diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt index 9fab5f0d..874a5396 100644 --- a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt +++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/advices.kt @@ -1,5 +1,6 @@ package com.supwisdom.dlpay.api +import com.supwisdom.dlpay.api.bean.JsonResult import com.supwisdom.dlpay.api.exception.RequestParamCheckException import com.supwisdom.dlpay.exception.TransactionCheckException import com.supwisdom.dlpay.exception.TransactionException @@ -18,17 +19,16 @@ import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.security.core.context.SecurityContextHolder import org.springframework.stereotype.Component +import org.springframework.transaction.TransactionSystemException +import org.springframework.web.bind.MethodArgumentNotValidException +import org.springframework.web.bind.annotation.ControllerAdvice import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice -import java.lang.reflect.UndeclaredThrowableException -import javax.servlet.http.HttpServletRequest -import java.util.stream.Collectors -import org.springframework.validation.BindingResultUtils.getBindingResult -import java.util.LinkedHashMap import org.springframework.web.context.request.WebRequest -import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler -import org.springframework.web.bind.annotation.ControllerAdvice +import java.lang.reflect.UndeclaredThrowableException +import javax.servlet.http.HttpServletRequest +import javax.validation.ConstraintViolationException @RestControllerAdvice("com.supwisdom.dlpay.api") @@ -52,9 +52,18 @@ class RestControllerAdvice { } } +@ControllerAdvice("com.supwisdom.dlpay") +class MvcControllerAdvice { + private val logger = KotlinLogging.logger { } + @ExceptionHandler(ConstraintViolationException::class) + fun handleException(e: ConstraintViolationException): ResponseEntity { + logger.error { "unhandle exception : $e" } + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e) + } +} + @ControllerAdvice class CustomGlobalExceptionHandler : ResponseEntityExceptionHandler() { - // error handle for @Valid override fun handleMethodArgumentNotValid(ex: MethodArgumentNotValidException, headers: HttpHeaders, @@ -66,6 +75,22 @@ class CustomGlobalExceptionHandler : ResponseEntityExceptionHandler() { .fail(300001, msg)) } + + @ExceptionHandler(TransactionSystemException::class) + fun handleContraintViolationException(e: TransactionSystemException): ResponseEntity { + logger.error { "unhandle exception : $e" } + var cause = e.cause + while (cause != null) { + if (cause is ConstraintViolationException) { + val msg = cause.constraintViolations.joinToString { + it.message + } + return ResponseEntity.status(HttpStatus.OK).body(JsonResult.error(msg)) + } + cause = cause.cause + } + return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(e) + } } @Component diff --git a/payapi/src/main/resources/data.sql b/payapi/src/main/resources/data.sql index fdeebca8..0f44e62f 100644 --- a/payapi/src/main/resources/data.sql +++ b/payapi/src/main/resources/data.sql @@ -505,60 +505,60 @@ VALUES (3010, '市民卡代扣', '{tenantid}'); INSERT INTO "tb_transcode" ("transcode", "transname", "tenantid") VALUES (3500, '账户充值', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('cancel', 'reverseFlagList', '冲正', '冲正状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('reverse', 'reverseFlagList', '手工撤销', '冲正状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('none', 'reverseFlagList', '-', '冲正状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('init', 'dtlStatusList', '初始化', '流水状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('success', 'dtlStatusList', '交易成功', '流水状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('fail', 'dtlStatusList', '交易失败', '流水状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('wip', 'dtlStatusList','待支付', '流水状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('cancel','dtlStatusList', '交易取消', '流水状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (1, 'cancel', 'reverseFlagList', '冲正', '冲正状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (2, 'reverse', 'reverseFlagList', '手工撤销', '冲正状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (3, 'none', 'reverseFlagList', '-', '冲正状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (4, 'init', 'dtlStatusList', '初始化', '流水状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (5, 'success', 'dtlStatusList', '交易成功', '流水状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (6, 'fail', 'dtlStatusList', '交易失败', '流水状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (7, 'wip', 'dtlStatusList','待支付', '流水状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (8, 'cancel','dtlStatusList', '交易取消', '流水状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('idcard', 'idtypeList', '身份证', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('residence_booklet', 'idtypeList', '户口簿', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('passport', 'idtypeList', '护照', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('hk_macau_pass', 'idtypeList', '港澳居民来往内地通行证', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('taiwan_pass', 'idtypeList', '港澳通行证', '台湾同胞来往内地通行证', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('foreigner_residence_permit', 'idtypeList', '港澳通行证', '外国人居留证', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('military_idcard', 'idtypeList', '港澳通行证', '军官证', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('soldier_idcard', 'idtypeList', '港澳通行证', '士兵证', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('driving_license', 'idtypeList', '驾照', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('student_idcard', 'idtypeList', '学工号', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('unknown', 'idtypeList', '其他', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (9, 'idcard', 'idtypeList', '身份证', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (10, 'residence_booklet', 'idtypeList', '户口簿', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (11, 'passport', 'idtypeList', '护照', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (12, 'hk_macau_pass', 'idtypeList', '港澳居民来往内地通行证', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (13, 'taiwan_pass', 'idtypeList', '港澳通行证', '台湾同胞来往内地通行证', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (14, 'foreigner_residence_permit', 'idtypeList', '港澳通行证', '外国人居留证', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (15, 'military_idcard', 'idtypeList', '港澳通行证', '军官证', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (16, 'soldier_idcard', 'idtypeList', '港澳通行证', '士兵证', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (17, 'driving_license', 'idtypeList', '驾照', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (18, 'student_idcard', 'idtypeList', '学工号', '证件类型', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (19, 'unknown', 'idtypeList', '其他', '证件类型', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('male', 'sexList', '男', '性别', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('female', 'sexList', '女', '性别', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('unknown', 'sexList', '未知', '性别', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (20, 'male', 'sexList', '男', '性别', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (21, 'female', 'sexList', '女', '性别', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (22, 'unknown', 'sexList', '未知', '性别', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('normal', 'accountStatusList', '正常', '账户状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('closed', 'accountStatusList', '注销', '账户状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('locked', 'accountStatusList', '锁定', '账户状态', '{tenantid}'); -INSERT INTO "tb_dictionary" ("dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") -VALUES ('unknown', 'accountStatusList', '异常', '账户状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (23, 'normal', 'accountStatusList', '正常', '账户状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (24, 'closed', 'accountStatusList', '注销', '账户状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (25, 'locked', 'accountStatusList', '锁定', '账户状态', '{tenantid}'); +INSERT INTO "tb_dictionary" ("id", "dictval", "dicttype", "dictcaption", "dicttypename", "tenantid") +VALUES (26, 'unknown', 'accountStatusList', '异常', '账户状态', '{tenantid}'); ---------------------------------------------------- commit; \ No newline at end of file diff --git a/payapi/src/main/resources/templates/system/user/add.html b/payapi/src/main/resources/templates/system/user/add.html index f886acae..f33976e3 100755 --- a/payapi/src/main/resources/templates/system/user/add.html +++ b/payapi/src/main/resources/templates/system/user/add.html @@ -86,14 +86,13 @@ data : JSON.stringify(data.field), success : function(result) { layer.closeAll('loading'); - if (result.code == 200) { + if (result.code === 200) { layer.msg(result.msg, {icon: 1}); admin.finishPopupCenter(); - } else if (result.code == 401) { + } else if (result.code === 401) { layer.msg(result.msg, {icon: 2, time: 1500}, function () { location.replace('[[@{/login}]]'); }, 1000); - return; } else { console.log('err:' + result.code); layer.msg(result.msg, {icon: 2});