Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
customer-service
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QG
customer-service
Commits
9b836f9c
Commit
9b836f9c
authored
Nov 16, 2022
by
李健华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改手机号逻辑
parent
3574c68a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
172 additions
and
0 deletions
+172
-0
ExceptionHandlers.java
...a/cn/quantgroup/customer/exception/ExceptionHandlers.java
+104
-0
UserModifyPhoneController.java
...n/quantgroup/customer/rest/UserModifyPhoneController.java
+33
-0
ModifyPhoneRecord.java
...antgroup/customer/rest/param/phone/ModifyPhoneRecord.java
+34
-0
ValidationUtil.java
...main/java/cn/quantgroup/customer/util/ValidationUtil.java
+1
-0
No files found.
src/main/java/cn/quantgroup/customer/exception/ExceptionHandlers.java
0 → 100644
View file @
9b836f9c
package
cn
.
quantgroup
.
customer
.
exception
;
import
cn.quantgroup.customer.rest.vo.JsonResult
;
import
lombok.extern.slf4j.Slf4j
;
import
org.hibernate.exception.DataException
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.jdbc.BadSqlGrammarException
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.ObjectError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.MissingServletRequestParameterException
;
import
org.springframework.web.bind.annotation.*
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolationException
;
import
javax.validation.ValidationException
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Set
;
/**
* Created by Miraculous on 15/7/6.
* 出现异常,进入这个handler。
*/
@Slf4j
@ControllerAdvice
@RestController
public
class
ExceptionHandlers
{
private
static
final
JsonResult
EXCEPTION_RESULT
=
new
JsonResult
(
"服务出错。"
,
(
long
)
HttpStatus
.
INTERNAL_SERVER_ERROR
.
value
(),
null
);
@ExceptionHandler
(
ValidationException
.
class
)
public
JsonResult
validException
(
ValidationException
exception
)
{
ConstraintViolationException
exs
=
(
ConstraintViolationException
)
exception
;
Set
<
ConstraintViolation
<?>>
constraintViolations
=
exs
.
getConstraintViolations
();
if
(
constraintViolations
.
isEmpty
())
{
log
.
error
(
"这里出了个错...."
,
exception
.
getMessage
());
return
null
;
}
String
message
=
constraintViolations
.
iterator
().
next
().
getMessage
();
return
JsonResult
.
buildErrorStateResult
(
message
,
null
);
}
@ExceptionHandler
({
MethodArgumentNotValidException
.
class
,
BindException
.
class
})
@ResponseBody
@ResponseStatus
(
HttpStatus
.
OK
)
public
JsonResult
handleBindException
(
Exception
e
)
{
BindingResult
result
=
null
;
if
(
e
instanceof
MethodArgumentNotValidException
)
{
result
=
((
MethodArgumentNotValidException
)
e
).
getBindingResult
();
}
else
if
(
e
instanceof
BindException
)
{
result
=
((
BindException
)
e
).
getBindingResult
();
}
if
(
result
!=
null
)
{
List
<
ObjectError
>
errors
=
result
.
getAllErrors
();
StringBuilder
sb
=
new
StringBuilder
();
if
(
errors
!=
null
&&
errors
.
size
()
>
0
)
{
Iterator
<
ObjectError
>
iterator
=
errors
.
iterator
();
ObjectError
err
=
null
;
while
((
err
=
iterator
.
next
())
!=
null
)
{
sb
.
append
(
err
.
getDefaultMessage
());
if
(
iterator
.
hasNext
())
{
sb
.
append
(
"; "
);
}
else
{
sb
.
append
(
"。"
);
break
;
}
}
}
return
JsonResult
.
buildErrorStateResult
(
sb
.
toString
(),
null
);
}
return
null
;
}
@ExceptionHandler
({
DataException
.
class
})
@ResponseBody
@ResponseStatus
(
HttpStatus
.
OK
)
public
JsonResult
handleDataException
(
DataException
e
)
{
return
JsonResult
.
buildErrorStateResult
(
e
.
getMessage
(),
null
);
}
@ExceptionHandler
(
BadSqlGrammarException
.
class
)
@ResponseBody
@ResponseStatus
(
HttpStatus
.
OK
)
public
JsonResult
handleBadSqlGrammarException
(
BadSqlGrammarException
e
)
{
log
.
error
(
"sql语法解析异常 error sql = 【{}】"
,
e
.
getSql
(),
e
);
return
JsonResult
.
buildErrorStateResult
(
"参数错误。"
,
null
);
}
@ExceptionHandler
(
MissingServletRequestParameterException
.
class
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseBody
public
JsonResult
handelMissingServletRequestParameterException
(
MissingServletRequestParameterException
re
)
{
return
JsonResult
.
buildErrorStateResult
(
re
.
getMessage
(),
null
);
}
}
src/main/java/cn/quantgroup/customer/rest/UserModifyPhoneController.java
0 → 100644
View file @
9b836f9c
package
cn
.
quantgroup
.
customer
.
rest
;
import
cn.quantgroup.customer.rest.param.phone.ModifyPhoneRecord
;
import
cn.quantgroup.customer.rest.vo.JsonResult
;
import
cn.quantgroup.customer.util.ValidationUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.validation.Valid
;
@Validated
@Slf4j
@RestController
@RequestMapping
(
"/v1"
)
public
class
UserModifyPhoneController
{
/**
* 修改手机号
*/
@PostMapping
(
value
=
"/modify/phoneNo"
)
public
JsonResult
modifyPhoneNo
(
@Valid
@RequestBody
ModifyPhoneRecord
modifyPhoneRecord
)
{
if
(
StringUtils
.
isNotBlank
(
modifyPhoneRecord
.
getCurPhoneNo
())
&&
!
ValidationUtil
.
validatePhoneNo
(
modifyPhoneRecord
.
getCurPhoneNo
()))
{
return
JsonResult
.
buildErrorStateResult
(
"手机号格式错误"
,
null
);
}
System
.
out
.
println
(
modifyPhoneRecord
);
return
JsonResult
.
buildSuccessResult
(
"success"
,
modifyPhoneRecord
);
}
}
src/main/java/cn/quantgroup/customer/rest/param/phone/ModifyPhoneRecord.java
0 → 100644
View file @
9b836f9c
package
cn
.
quantgroup
.
customer
.
rest
.
param
.
phone
;
import
cn.quantgroup.customer.util.ValidationUtil
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.NotBlank
;
import
javax.validation.constraints.NotNull
;
import
javax.validation.constraints.Pattern
;
import
java.io.Serializable
;
@Data
public
class
ModifyPhoneRecord
implements
Serializable
{
@NotBlank
(
message
=
"用户ID不能为空"
)
private
String
userId
;
@NotBlank
(
message
=
"原手机号不能为空"
)
@Pattern
(
regexp
=
ValidationUtil
.
phoneRegExp
,
message
=
"当前手机号码格式错误"
)
private
String
prevPhoneNo
;
@NotBlank
(
message
=
"新手机号不能为空"
)
@Pattern
(
regexp
=
ValidationUtil
.
phoneRegExp
,
message
=
"当前手机号码格式错误"
)
private
String
curPhoneNo
;
@NotNull
(
message
=
"修改原因不能为空"
)
private
Integer
reason
;
@NotBlank
(
message
=
"操作人不能为空"
)
private
String
operator
;
@NotBlank
(
message
=
"备注不能为空"
)
private
String
remark
;
}
src/main/java/cn/quantgroup/customer/util/ValidationUtil.java
View file @
9b836f9c
...
...
@@ -14,6 +14,7 @@ import static java.util.regex.Pattern.compile;
import
static
java
.
util
.
regex
.
Pattern
.
matches
;
public
class
ValidationUtil
{
public
static
final
String
phoneRegExp
=
"^1[3456789][0-9]{9}$"
;
public
static
boolean
validatePhoneNo
(
String
phoneNo
)
{
if
(
StringUtils
.
isEmpty
(
phoneNo
))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment