|
@@ -3,30 +3,30 @@ package com.zhentao.service.impl;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import com.sun.deploy.net.HttpResponse;
|
|
|
import com.zhentao.domain.UserLogin;
|
|
|
-import com.zhentao.dto.user.NoteDto;
|
|
|
+import com.zhentao.dto.user.*;
|
|
|
import com.zhentao.dto.Result;
|
|
|
-import com.zhentao.dto.user.UserDto;
|
|
|
-import com.zhentao.dto.user.UserDtoup;
|
|
|
-import com.zhentao.dto.user.UserRegister;
|
|
|
import com.zhentao.enums.ApiServerException;
|
|
|
import com.zhentao.exception.AsynException;
|
|
|
import com.zhentao.mapper.UserLoginMapper;
|
|
|
+import com.zhentao.service.FileService;
|
|
|
import com.zhentao.service.UserLoginService;
|
|
|
-import com.zhentao.tool.HttpUtils;
|
|
|
import com.zhentao.tool.TokenUtils;
|
|
|
+import io.minio.errors.*;
|
|
|
+import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
|
|
|
import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
-import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.DigestUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.management.openmbean.InvalidKeyException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.rmi.ServerException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@@ -42,6 +42,11 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
private StringRedisTemplate stringRedisTemplate;
|
|
|
@Autowired
|
|
|
private RedissonClient redissonClient;
|
|
|
+ @Autowired
|
|
|
+ private UserLoginMapper userLoginMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileService fileService;
|
|
|
@Override
|
|
|
public Result login(UserDto userDto) {
|
|
|
RLock lock = redissonClient.getLock(userDto.getPhone()+"phone");
|
|
@@ -213,6 +218,102 @@ public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result uploadPicture(MultipartFile multipartFile, Long id) throws IOException, ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, io.minio.errors.ServerException, java.security.InvalidKeyException, io.minio.errors.InternalException {
|
|
|
+ if (multipartFile == null) {
|
|
|
+ Result.ERR("上传失败", null);
|
|
|
+ }
|
|
|
+ UserLogin userLogin = userLoginMapper.selectById(id);
|
|
|
+ String url = fileService.uploadImage(multipartFile.getOriginalFilename(), multipartFile.getInputStream());
|
|
|
+ System.err.println(url);
|
|
|
+ userLogin.setAvatar(url);
|
|
|
+ userLoginMapper.updateById(userLogin);
|
|
|
+ return Result.OK("上传成功", url);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserLogin getUserInfo(Long id) {
|
|
|
+ return userLoginMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result updatePassword(String username, String oldPassword, String newPassword) {
|
|
|
+ // 获取分布式锁,防止并发修改密码问题
|
|
|
+ RLock lock = redissonClient.getLock(username);
|
|
|
+ try {
|
|
|
+ // 尝试获取锁,等待10秒,锁自动释放时间为20秒
|
|
|
+ boolean b = lock.tryLock(10, 20, java.util.concurrent.TimeUnit.SECONDS);
|
|
|
+ if (b) {
|
|
|
+ // 创建查询条件,根据用户名查询用户信息
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("user_username", username);
|
|
|
+ UserLogin one = this.getOne(queryWrapper);
|
|
|
+ if (one == null) {
|
|
|
+ // 如果用户不存在,抛出异常
|
|
|
+ throw new AsynException(ApiServerException.NULL_USERNAME);
|
|
|
+ }
|
|
|
+ String salt = one.getSalt();
|
|
|
+ // 使用之前相同的加密逻辑,对旧密码进行加密
|
|
|
+ String oldPasswordEncrypted = DigestUtils.md5DigestAsHex((salt + oldPassword).getBytes());
|
|
|
+ if (!oldPasswordEncrypted.equals(one.getUserPassword())) {
|
|
|
+ // 如果旧密码加密后与数据库中存储的密码不匹配,抛出异常
|
|
|
+ throw new AsynException(ApiServerException.NULL_PASSWORD);
|
|
|
+ }
|
|
|
+ // 使用相同的加密逻辑,对新密码进行加密
|
|
|
+ String newPasswordEncrypted = DigestUtils.md5DigestAsHex((salt + newPassword).getBytes());
|
|
|
+ // 更新用户密码到数据库
|
|
|
+ one.setUserPassword(newPasswordEncrypted);
|
|
|
+ boolean updateResult = this.updateById(one);
|
|
|
+ if (updateResult) {
|
|
|
+ return Result.OK("修改密码成功", updateResult);
|
|
|
+ } else {
|
|
|
+ return Result.ERR("修改密码失败", null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return Result.ERR("获取锁超时",null);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 捕获可能的异常,如获取锁时中断异常等
|
|
|
+ if (e instanceof InterruptedException) {
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ }
|
|
|
+ String message = e.getMessage();
|
|
|
+ return Result.ERR("修改密码失败:" + message,null);
|
|
|
+ } finally {
|
|
|
+ // 无论是否成功获取锁或修改密码,都要释放锁
|
|
|
+ if (lock.isLocked() && lock.isHeldByCurrentThread()) {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean updateUserInfo(UserInfoDto userInfoDto, Long userId) {
|
|
|
+ // 根据userId查询用户原始信息
|
|
|
+ QueryWrapper<UserLogin> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("id", userId);
|
|
|
+ UserLogin userLogin = userLoginMapper.selectOne(queryWrapper);
|
|
|
+ if (userLogin == null) {
|
|
|
+ // 用户不存在,返回修改失败
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 更新头像图片字段
|
|
|
+ if (userInfoDto.getAvator()!= null) {
|
|
|
+ userLogin.setAvatar(userInfoDto.getAvator());
|
|
|
+ }
|
|
|
+ // 更新用户昵称字段
|
|
|
+ if (userInfoDto.getUserName() != null) {
|
|
|
+ userLogin.setUserName(userInfoDto.getUserName());
|
|
|
+ }
|
|
|
+ // 更新手机号字段
|
|
|
+ if (userInfoDto.getUserMobile() != null) {
|
|
|
+ userLogin.setUserMobile(userInfoDto.getUserMobile());
|
|
|
+ }
|
|
|
+ // 将更新后的用户信息更新到数据库
|
|
|
+ int updateResult = userLoginMapper.updateById(userLogin);
|
|
|
+ return updateResult > 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|