|
@@ -1,19 +1,81 @@
|
|
|
package com.sky.ioc.service.system.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.sky.ioc.entity.domain.system.Users;
|
|
|
+import com.sky.ioc.entity.result.system.LoginUserVo;
|
|
|
import com.sky.ioc.mapper.system.UserMapper;
|
|
|
import com.sky.ioc.service.system.UserService;
|
|
|
+import com.sky.ioc.tool.JwtUtil;
|
|
|
+import com.sky.ioc.tool.Pbkdf2Sha256Digest;
|
|
|
+import com.sky.ioc.tool.RedisUtil;
|
|
|
import com.sky.ioc.tool.ReturnMsg;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
@Service
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
|
|
@Autowired
|
|
|
UserMapper userMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
@Override
|
|
|
public ReturnMsg updateCommonMenusByUserIdAndMenuId(long userId, String menuIDs) {
|
|
|
userMapper.updateCommonMenusByUserIdAndMenuId(userId,menuIDs);
|
|
|
return ReturnMsg.ok();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReturnMsg login(String username, String password) {
|
|
|
+ Users users = userMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUserName, username));
|
|
|
+ if (Objects.isNull(users)){
|
|
|
+ throw new RuntimeException("用户名或密码错误");
|
|
|
+ }
|
|
|
+ boolean verification = Pbkdf2Sha256Digest.verification(password, users.getPassword());
|
|
|
+ if (!verification){
|
|
|
+ throw new RuntimeException("用户名或密码错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 认证成功
|
|
|
+ LoginUserVo loginUser = new LoginUserVo();
|
|
|
+ BeanUtils.copyProperties(users, loginUser);
|
|
|
+
|
|
|
+ String token = JwtUtil.genJwtToken(users);
|
|
|
+
|
|
|
+ // 将用户信息存入redis
|
|
|
+ redisUtil.put("login:"+loginUser.getUserName(), loginUser);
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("userInfo", loginUser);
|
|
|
+ jsonObject.put("token", token);
|
|
|
+ return ReturnMsg.ok(jsonObject, "登录成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReturnMsg logout() {
|
|
|
+ ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ String token = sra.getRequest().getHeader("token");
|
|
|
+ // 前端注销,后端暂不处理
|
|
|
+ return ReturnMsg.ok("注销成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReturnMsg validate() {
|
|
|
+ ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ String token = sra.getRequest().getHeader("token");
|
|
|
+ String username = JwtUtil.parseJWT(token);
|
|
|
+ Users users = userMapper.selectOne(new LambdaQueryWrapper<Users>().eq(Users::getUserName, username));
|
|
|
+ LoginUserVo loginUser = new LoginUserVo();
|
|
|
+ BeanUtils.copyProperties(users, loginUser);
|
|
|
+ return ReturnMsg.ok(loginUser);
|
|
|
+ }
|
|
|
}
|