package com.gkhy.exam.admin.system; import cn.hutool.core.codec.Base64; import com.gkhy.exam.common.annotation.Log; import com.gkhy.exam.common.api.CommonResult; import com.gkhy.exam.common.domain.entity.SysUser; import com.gkhy.exam.common.enums.BusinessType; import com.gkhy.exam.common.exception.ApiException; import com.gkhy.exam.common.utils.SecurityUtils; import com.gkhy.exam.common.utils.StringUtils; import com.gkhy.exam.framework.web.service.SysPasswordService; import com.gkhy.exam.system.service.SysUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Map; @Api(tags = "个人信息前端控制器") @RestController @RequestMapping("/system/user/profile") public class SysProfileController { @Autowired private SysUserService userService; @Autowired private SysPasswordService passwordService; @GetMapping public CommonResult profile(){ SysUser user= SecurityUtils.getLoginUser().getUser(); return CommonResult.success(user); } @Log(title = "个人信息", businessType = BusinessType.UPDATE) @PutMapping public CommonResult updateProfile(@RequestBody SysUser user) { SysUser currentUser = SecurityUtils.getLoginUser().getUser(); currentUser.setName(user.getName()); currentUser.setPhone(user.getPhone()); currentUser.setSex(user.getSex()); if (StringUtils.isNotEmpty(user.getPhone()) && !userService.checkPhoneUnique(currentUser)) { throw new ApiException("手机号码已存在"); } if (userService.updateUser(currentUser)>0) { // 更新缓存用户信息 SecurityUtils.getLoginUser().setUser(currentUser); return CommonResult.success(); } throw new ApiException("修改个人信息异常,请联系管理员"); } @Log(title = "个人信息", businessType = BusinessType.UPDATE) @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", name = "oldPassword", dataType = "String", required = true, value = "旧密码"), @ApiImplicitParam(paramType = "body", name = "newPassword", dataType = "String", required = true, value = "新密码") }) @PutMapping("/updatePwd") public CommonResult updatePwd(@RequestBody Map body) { String oldPassword=body.get("oldPassword"); String newPassword=body.get("newPassword"); if(StringUtils.isBlank(oldPassword)||StringUtils.isBlank(oldPassword)){ throw new ApiException("旧密码或者新密码不能为空"); } oldPassword=Base64.decodeStr(oldPassword); newPassword=Base64.decodeStr(newPassword); SysUser currentUser = SecurityUtils.getLoginUser().getUser(); if (!passwordService.matches(currentUser, oldPassword)) { throw new ApiException("修改密码失败,旧密码错误"); } if (!passwordService.matches(currentUser, newPassword)) { throw new ApiException("新密码不能与旧密码相同"); } currentUser.setPassword(SecurityUtils.encryptPassword(newPassword)); if (userService.resetUserPwd(currentUser)) { // 更新缓存用户密码 SecurityUtils.getLoginUser().getUser().setPassword(SecurityUtils.encryptPassword(newPassword)); return CommonResult.success(); } throw new ApiException("修改密码异常,请联系管理员"); } }