2025-02-11 15:27:15 +08:00
|
|
|
package com.ff.system;
|
|
|
|
|
|
|
|
|
|
import com.ff.base.annotation.Log;
|
|
|
|
|
import com.ff.base.config.FFConfig;
|
|
|
|
|
import com.ff.base.core.controller.BaseController;
|
|
|
|
|
import com.ff.base.core.domain.AjaxResult;
|
2025-02-25 10:05:13 +08:00
|
|
|
import com.ff.base.enums.LoginType;
|
2025-02-11 15:27:15 +08:00
|
|
|
import com.ff.base.system.domain.SysUser;
|
|
|
|
|
import com.ff.base.core.domain.model.LoginUser;
|
|
|
|
|
import com.ff.base.enums.BusinessType;
|
2025-02-25 10:05:13 +08:00
|
|
|
import com.ff.base.system.domain.TenantSecretKey;
|
|
|
|
|
import com.ff.base.system.service.ITenantSecretKeyService;
|
2025-02-11 15:27:15 +08:00
|
|
|
import com.ff.base.utils.SecurityUtils;
|
|
|
|
|
import com.ff.base.utils.StringUtils;
|
|
|
|
|
import com.ff.base.utils.file.FileUploadUtils;
|
|
|
|
|
import com.ff.base.utils.file.MimeTypeUtils;
|
|
|
|
|
import com.ff.base.web.service.TokenService;
|
|
|
|
|
import com.ff.base.system.service.ISysUserService;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
2025-02-25 10:05:13 +08:00
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
2025-02-11 15:27:15 +08:00
|
|
|
/**
|
|
|
|
|
* 个人信息 业务处理
|
|
|
|
|
*
|
|
|
|
|
* @author ff
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/system/user/profile")
|
2025-02-25 10:05:13 +08:00
|
|
|
public class SysProfileController extends BaseController {
|
2025-02-11 15:27:15 +08:00
|
|
|
@Autowired
|
|
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
2025-02-25 10:05:13 +08:00
|
|
|
@Resource
|
|
|
|
|
private ITenantSecretKeyService tenantSecretKeyService;
|
|
|
|
|
|
2025-02-11 15:27:15 +08:00
|
|
|
/**
|
|
|
|
|
* 个人信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping
|
2025-02-25 10:05:13 +08:00
|
|
|
public AjaxResult profile() {
|
2025-02-11 15:27:15 +08:00
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
SysUser user = loginUser.getUser();
|
|
|
|
|
AjaxResult ajax = AjaxResult.success(user);
|
|
|
|
|
ajax.put("roleGroup", userService.selectUserRoleGroup(loginUser.getUsername()));
|
|
|
|
|
ajax.put("postGroup", userService.selectUserPostGroup(loginUser.getUsername()));
|
|
|
|
|
return ajax;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改用户
|
|
|
|
|
*/
|
|
|
|
|
@Log(title = "个人信息", businessType = BusinessType.UPDATE)
|
|
|
|
|
@PutMapping
|
2025-02-25 10:05:13 +08:00
|
|
|
public AjaxResult updateProfile(@RequestBody SysUser user) {
|
2025-02-11 15:27:15 +08:00
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
SysUser currentUser = loginUser.getUser();
|
|
|
|
|
currentUser.setNickName(user.getNickName());
|
|
|
|
|
currentUser.setEmail(user.getEmail());
|
|
|
|
|
currentUser.setPhonenumber(user.getPhonenumber());
|
|
|
|
|
currentUser.setSex(user.getSex());
|
2025-02-25 10:05:13 +08:00
|
|
|
if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser)) {
|
2025-02-11 15:27:15 +08:00
|
|
|
return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
|
|
|
|
|
}
|
2025-02-25 10:05:13 +08:00
|
|
|
if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser)) {
|
2025-02-11 15:27:15 +08:00
|
|
|
return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
|
|
|
|
|
}
|
2025-02-25 10:05:13 +08:00
|
|
|
if (userService.updateUserProfile(currentUser) > 0) {
|
2025-02-11 15:27:15 +08:00
|
|
|
// 更新缓存用户信息
|
|
|
|
|
tokenService.setLoginUser(loginUser);
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
return error("修改个人信息异常,请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置密码
|
|
|
|
|
*/
|
|
|
|
|
@Log(title = "个人信息", businessType = BusinessType.UPDATE)
|
|
|
|
|
@PutMapping("/updatePwd")
|
2025-02-25 10:05:13 +08:00
|
|
|
public AjaxResult updatePwd(String oldPassword, String newPassword) {
|
2025-02-11 15:27:15 +08:00
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
String userName = loginUser.getUsername();
|
|
|
|
|
String password = loginUser.getPassword();
|
2025-02-25 10:05:13 +08:00
|
|
|
if (!SecurityUtils.matchesPassword(oldPassword, password)) {
|
2025-02-11 15:27:15 +08:00
|
|
|
return error("修改密码失败,旧密码错误");
|
|
|
|
|
}
|
2025-02-25 10:05:13 +08:00
|
|
|
if (SecurityUtils.matchesPassword(newPassword, password)) {
|
2025-02-11 15:27:15 +08:00
|
|
|
return error("新密码不能与旧密码相同");
|
|
|
|
|
}
|
|
|
|
|
newPassword = SecurityUtils.encryptPassword(newPassword);
|
2025-02-25 10:05:13 +08:00
|
|
|
|
|
|
|
|
Integer loginType = loginUser.getUser().getLoginType();
|
|
|
|
|
Boolean result = Boolean.FALSE;
|
|
|
|
|
if (LoginType.TENANT.getValue().equals(loginType)) {
|
|
|
|
|
TenantSecretKey tenantSecretKey = tenantSecretKeyService.selectTenantSecretKeyByTenantKey(userName);
|
|
|
|
|
tenantSecretKey.setPassword(newPassword);
|
|
|
|
|
result = tenantSecretKeyService.updateTenantSecretKey(tenantSecretKey) > 0;
|
|
|
|
|
} else if (LoginType.ADMIN.getValue().equals(loginType)) {
|
|
|
|
|
result = userService.resetUserPwd(userName, newPassword) > 0;
|
|
|
|
|
}
|
|
|
|
|
if (result) {
|
2025-02-11 15:27:15 +08:00
|
|
|
// 更新缓存用户密码
|
|
|
|
|
loginUser.getUser().setPassword(newPassword);
|
|
|
|
|
tokenService.setLoginUser(loginUser);
|
|
|
|
|
return success();
|
|
|
|
|
}
|
|
|
|
|
return error("修改密码异常,请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 头像上传
|
|
|
|
|
*/
|
|
|
|
|
@Log(title = "用户头像", businessType = BusinessType.UPDATE)
|
|
|
|
|
@PostMapping("/avatar")
|
2025-02-25 10:05:13 +08:00
|
|
|
public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception {
|
|
|
|
|
if (!file.isEmpty()) {
|
2025-02-11 15:27:15 +08:00
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
|
String avatar = FileUploadUtils.upload(FFConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
|
2025-02-25 10:05:13 +08:00
|
|
|
if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) {
|
2025-02-11 15:27:15 +08:00
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
|
|
ajax.put("imgUrl", avatar);
|
|
|
|
|
// 更新缓存用户头像
|
|
|
|
|
loginUser.getUser().setAvatar(avatar);
|
|
|
|
|
tokenService.setLoginUser(loginUser);
|
|
|
|
|
return ajax;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return error("上传图片异常,请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
}
|