|
@@ -0,0 +1,137 @@
|
|
|
+package com.smcic.api.user.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.crypto.SecureUtil;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.smcic.api.user.dto.LoginDto;
|
|
|
+import com.smcic.api.user.dto.PwdResetDTO;
|
|
|
+import com.smcic.api.user.dto.TokenDto;
|
|
|
+import com.smcic.api.user.dto.UserDTO;
|
|
|
+import com.smcic.api.user.entity.User;
|
|
|
+import com.smcic.api.user.mapper.UserMapper;
|
|
|
+import com.smcic.api.user.service.IUserService;
|
|
|
+import com.smcic.core.advice.APIException;
|
|
|
+import com.smcic.core.auth.AESUtil;
|
|
|
+import com.smcic.core.auth.HttpContextUtil;
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author zyx
|
|
|
+ * @since 2023-02-15
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
|
|
|
+
|
|
|
+ public TokenDto login(LoginDto dto) {
|
|
|
+
|
|
|
+ Optional<User> optionalUser = lambdaQuery().eq(User::getLoginName, dto.getLoginName())
|
|
|
+ .eq(User::getLoginPassword, dto.getLoginPassword())
|
|
|
+ .oneOpt();
|
|
|
+
|
|
|
+ if (!optionalUser.isPresent()) {
|
|
|
+ throw new APIException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long id = optionalUser.get().getId();
|
|
|
+
|
|
|
+ String token = AESUtil.encryptHex(id.toString());
|
|
|
+ TokenDto tokenDto = new TokenDto();
|
|
|
+ tokenDto.setToken(token);
|
|
|
+ return tokenDto;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public User getLoginUser() {
|
|
|
+ Optional<String> token = HttpContextUtil.getToken();
|
|
|
+ if (!token.isPresent()) {
|
|
|
+ throw new APIException("获取token异常");
|
|
|
+ }
|
|
|
+ String userId = AESUtil.decryptStr(token.get());
|
|
|
+ User user = this.lambdaQuery().eq(User::getId, Long.parseLong(userId)).one();
|
|
|
+ if (user == null) {
|
|
|
+ throw new APIException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public User getLoginUserByToken(String token) {
|
|
|
+ String userId = AESUtil.decryptStr(token);
|
|
|
+ User user = this.lambdaQuery().eq(User::getId, Long.parseLong(userId)).one();
|
|
|
+ if (user == null) {
|
|
|
+ throw new APIException("用户不存在");
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void createUser(String userName, String pwd) {
|
|
|
+ User user = new User();
|
|
|
+ user.setLoginName(userName);
|
|
|
+ user.setLoginPassword(SecureUtil.sha256(pwd));
|
|
|
+ user.setEncodePassword(AESUtil.encryptHex(pwd));
|
|
|
+ save(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void createUser(String userName, String pwd, Integer roleId) {
|
|
|
+ User user = new User();
|
|
|
+ user.setLoginName(userName);
|
|
|
+ user.setLoginPassword(SecureUtil.sha256(pwd));
|
|
|
+ user.setEncodePassword(AESUtil.encryptHex(pwd));
|
|
|
+ user.setRoleId(roleId);
|
|
|
+ save(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void logout(){
|
|
|
+ Optional<String> token = HttpContextUtil.getToken();
|
|
|
+ if (!token.isPresent()) {
|
|
|
+ throw new RuntimeException("获取token异常");
|
|
|
+ }
|
|
|
+ String userId = AESUtil.decryptStr(token.get());
|
|
|
+ User user = this.lambdaQuery().eq(User::getId, Long.parseLong(userId)).one();
|
|
|
+ if (user == null) {
|
|
|
+ throw new APIException("用户不存在");
|
|
|
+ }
|
|
|
+ user.setLogoutTime(LocalDateTime.now());
|
|
|
+ updateById(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void reset(PwdResetDTO pwdResetDTO){
|
|
|
+ Optional<String> token = HttpContextUtil.getToken();
|
|
|
+
|
|
|
+ String uid = AESUtil.decryptStr(token.get());
|
|
|
+ User user = this.lambdaQuery().eq(User::getId, Long.parseLong(uid)).eq(User::getLoginPassword, pwdResetDTO.getOldPwd()).one();
|
|
|
+ if(null == user){
|
|
|
+ throw new APIException(40001, "原密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ user.setLoginPassword(pwdResetDTO.getPwd());
|
|
|
+ user.setEncodePassword("密码已修改");
|
|
|
+ updateById(user);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void updateRole(UserDTO userDTO){
|
|
|
+ User user = getById(userDTO.getId());
|
|
|
+ if(null == user){
|
|
|
+ throw new APIException(40001, "不存在的用户");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(userDTO.getRoleId() != null && userDTO.getRoleId() > 0)
|
|
|
+ user.setRoleId(userDTO.getRoleId());
|
|
|
+
|
|
|
+ if(!TextUtils.isEmpty(userDTO.getLoginPassword())){
|
|
|
+ user.setLoginPassword(SecureUtil.sha256(userDTO.getLoginPassword()));
|
|
|
+ user.setEncodePassword(AESUtil.encryptHex(userDTO.getLoginPassword()));
|
|
|
+ }
|
|
|
+
|
|
|
+ updateById(user);
|
|
|
+ }
|
|
|
+}
|