game-api/ff-admin/src/main/java/com/ff/system/SysProfileController.java

137 lines
5.2 KiB
Java
Raw Normal View History

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;
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;
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;
import javax.annotation.Resource;
2025-02-11 15:27:15 +08:00
/**
*
*
* @author ff
*/
@RestController
@RequestMapping("/system/user/profile")
public class SysProfileController extends BaseController {
2025-02-11 15:27:15 +08:00
@Autowired
private ISysUserService userService;
@Autowired
private TokenService tokenService;
@Resource
private ITenantSecretKeyService tenantSecretKeyService;
2025-02-11 15:27:15 +08:00
/**
*
*/
@GetMapping
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
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());
if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(currentUser)) {
2025-02-11 15:27:15 +08:00
return error("修改用户'" + loginUser.getUsername() + "'失败,手机号码已存在");
}
if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(currentUser)) {
2025-02-11 15:27:15 +08:00
return error("修改用户'" + loginUser.getUsername() + "'失败,邮箱账号已存在");
}
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")
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();
if (!SecurityUtils.matchesPassword(oldPassword, password)) {
2025-02-11 15:27:15 +08:00
return error("修改密码失败,旧密码错误");
}
if (SecurityUtils.matchesPassword(newPassword, password)) {
2025-02-11 15:27:15 +08:00
return error("新密码不能与旧密码相同");
}
newPassword = SecurityUtils.encryptPassword(newPassword);
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")
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);
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("上传图片异常,请联系管理员");
}
}