@ -1,5 +1,6 @@
package com.hyp.web.controller.system ;
package com.hyp.web.controller.system ;
import com.hyp.common.utils.sign.Sm4Util ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.PostMapping ;
@ -30,8 +31,7 @@ import com.hyp.system.service.ISysUserService;
* /
* /
@RestController
@RestController
@RequestMapping ( "/system/user/profile" )
@RequestMapping ( "/system/user/profile" )
public class SysProfileController extends BaseController
public class SysProfileController extends BaseController {
{
@Autowired
@Autowired
private ISysUserService userService ;
private ISysUserService userService ;
@ -42,8 +42,7 @@ public class SysProfileController extends BaseController
* 个 人 信 息
* 个 人 信 息
* /
* /
@GetMapping
@GetMapping
public AjaxResult profile ( )
public AjaxResult profile ( ) {
{
LoginUser loginUser = getLoginUser ( ) ;
LoginUser loginUser = getLoginUser ( ) ;
SysUser user = loginUser . getUser ( ) ;
SysUser user = loginUser . getUser ( ) ;
AjaxResult ajax = AjaxResult . success ( user ) ;
AjaxResult ajax = AjaxResult . success ( user ) ;
@ -57,24 +56,20 @@ public class SysProfileController extends BaseController
* /
* /
@Log ( title = "个人信息" , businessType = BusinessType . UPDATE )
@Log ( title = "个人信息" , businessType = BusinessType . UPDATE )
@PutMapping
@PutMapping
public AjaxResult updateProfile ( @RequestBody SysUser user )
public AjaxResult updateProfile ( @RequestBody SysUser user ) {
{
LoginUser loginUser = getLoginUser ( ) ;
LoginUser loginUser = getLoginUser ( ) ;
SysUser currentUser = loginUser . getUser ( ) ;
SysUser currentUser = loginUser . getUser ( ) ;
currentUser . setNickName ( user . getNickName ( ) ) ;
currentUser . setNickName ( user . getNickName ( ) ) ;
currentUser . setEmail ( user . getEmail ( ) ) ;
currentUser . setEmail ( user . getEmail ( ) ) ;
currentUser . setPhonenumber ( user . getPhonenumber ( ) ) ;
currentUser . setPhonenumber ( user . getPhonenumber ( ) ) ;
currentUser . setSex ( user . getSex ( ) ) ;
currentUser . setSex ( user . getSex ( ) ) ;
if ( StringUtils . isNotEmpty ( user . getPhonenumber ( ) ) & & ! userService . checkPhoneUnique ( currentUser ) )
if ( StringUtils . isNotEmpty ( user . getPhonenumber ( ) ) & & ! userService . checkPhoneUnique ( currentUser ) ) {
{
return error ( "修改用户'" + loginUser . getUsername ( ) + "'失败,手机号码已存在" ) ;
return error ( "修改用户'" + loginUser . getUsername ( ) + "'失败,手机号码已存在" ) ;
}
}
if ( StringUtils . isNotEmpty ( user . getEmail ( ) ) & & ! userService . checkEmailUnique ( currentUser ) )
if ( StringUtils . isNotEmpty ( user . getEmail ( ) ) & & ! userService . checkEmailUnique ( currentUser ) ) {
{
return error ( "修改用户'" + loginUser . getUsername ( ) + "'失败,邮箱账号已存在" ) ;
return error ( "修改用户'" + loginUser . getUsername ( ) + "'失败,邮箱账号已存在" ) ;
}
}
if ( userService . updateUserProfile ( currentUser ) > 0 )
if ( userService . updateUserProfile ( currentUser ) > 0 ) {
{
// 更新缓存用户信息
// 更新缓存用户信息
tokenService . setLoginUser ( loginUser ) ;
tokenService . setLoginUser ( loginUser ) ;
return success ( ) ;
return success ( ) ;
@ -87,24 +82,22 @@ public class SysProfileController extends BaseController
* /
* /
@Log ( title = "个人信息" , businessType = BusinessType . UPDATE )
@Log ( title = "个人信息" , businessType = BusinessType . UPDATE )
@PutMapping ( "/updatePwd" )
@PutMapping ( "/updatePwd" )
public AjaxResult updatePwd ( String oldPassword , String newPassword )
public AjaxResult updatePwd ( String oldPassword , String newPassword ) throws Exception {
{
LoginUser loginUser = getLoginUser ( ) ;
LoginUser loginUser = getLoginUser ( ) ;
String userName = loginUser . getUsername ( ) ;
String userName = loginUser . getUsername ( ) ;
String password = loginUser . getPassword ( ) ;
String password = loginUser . getPassword ( ) ;
if ( ! SecurityUtils . matchesPassword ( oldPassword , password ) )
//解密
{
oldPassword = Sm4Util . decryptEcb ( oldPassword ) ;
newPassword = Sm4Util . decryptEcb ( newPassword ) ;
if ( ! SecurityUtils . matchesPassword ( oldPassword , password ) ) {
return error ( "修改密码失败,旧密码错误" ) ;
return error ( "修改密码失败,旧密码错误" ) ;
}
}
if ( SecurityUtils . matchesPassword ( newPassword , password ) )
if ( SecurityUtils . matchesPassword ( newPassword , password ) ) {
{
return error ( "新密码不能与旧密码相同" ) ;
return error ( "新密码不能与旧密码相同" ) ;
}
}
newPassword = SecurityUtils . encryptPassword ( newPassword ) ;
if ( userService . resetUserPwd ( userName , SecurityUtils . encryptPassword ( newPassword ) ) > 0 ) {
if ( userService . resetUserPwd ( userName , newPassword ) > 0 )
{
// 更新缓存用户密码
// 更新缓存用户密码
loginUser . getUser ( ) . setPassword ( newPassword) ;
loginUser . getUser ( ) . setPassword ( SecurityUtils . encryptPassword ( newPassword ) ) ;
tokenService . setLoginUser ( loginUser ) ;
tokenService . setLoginUser ( loginUser ) ;
return success ( ) ;
return success ( ) ;
}
}
@ -116,14 +109,11 @@ public class SysProfileController extends BaseController
* /
* /
@Log ( title = "用户头像" , businessType = BusinessType . UPDATE )
@Log ( title = "用户头像" , businessType = BusinessType . UPDATE )
@PostMapping ( "/avatar" )
@PostMapping ( "/avatar" )
public AjaxResult avatar ( @RequestParam ( "avatarfile" ) MultipartFile file ) throws Exception
public AjaxResult avatar ( @RequestParam ( "avatarfile" ) MultipartFile file ) throws Exception {
{
if ( ! file . isEmpty ( ) ) {
if ( ! file . isEmpty ( ) )
{
LoginUser loginUser = getLoginUser ( ) ;
LoginUser loginUser = getLoginUser ( ) ;
String avatar = FileUploadUtils . upload ( HypConfig . getAvatarPath ( ) , file , MimeTypeUtils . IMAGE_EXTENSION ) ;
String avatar = FileUploadUtils . upload ( HypConfig . getAvatarPath ( ) , file , MimeTypeUtils . IMAGE_EXTENSION ) ;
if ( userService . updateUserAvatar ( loginUser . getUsername ( ) , avatar ) )
if ( userService . updateUserAvatar ( loginUser . getUsername ( ) , avatar ) ) {
{
AjaxResult ajax = AjaxResult . success ( ) ;
AjaxResult ajax = AjaxResult . success ( ) ;
ajax . put ( "imgUrl" , avatar ) ;
ajax . put ( "imgUrl" , avatar ) ;
// 更新缓存用户头像
// 更新缓存用户头像