package com.wyz.demo.service.impl;
|
|
|
|
|
|
import com.wyz.demo.dao.UserRepository;
|
|
import com.wyz.demo.dto.UserDTO;
|
|
import com.wyz.demo.po.UserPO;
|
|
import com.wyz.demo.service.UserService;
|
|
import com.wyz.demo.vo.Message;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.servlet.http.Cookie;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpSession;
|
|
|
|
@Service
|
|
@Slf4j
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
|
|
|
|
/**
|
|
* 用户注册
|
|
* @param userDTO
|
|
* @param resp
|
|
* @param sess
|
|
* @return
|
|
*/
|
|
@Override
|
|
public Message addUser(UserDTO userDTO, HttpServletResponse resp, HttpSession sess) {
|
|
|
|
log.info("请求注册的用户名是:{}",userDTO.getUsername());
|
|
log.info("请求注册的密码是:{}",userDTO.getPassword());
|
|
log.info("请求注册的手机号是:{}",userDTO.getPhone());
|
|
|
|
Message msg = new Message();
|
|
UserPO userPO = userRepository.getUserByUsername(userDTO.getUsername()); //判断用户名是否存在
|
|
if(userPO!=null){
|
|
msg.setFlag(-3);
|
|
msg.setMsg("该用户名已经注册");
|
|
}else{
|
|
msg.setFlag(2);
|
|
msg.setMsg("注册成功");
|
|
|
|
|
|
// 在数据库中加一个用户
|
|
UserPO u = new UserPO();
|
|
u.setUsername(userDTO.getUsername());
|
|
u.setPassword(userDTO.getPassword());
|
|
u.setPhone(userDTO.getPhone());
|
|
userRepository.addUser(u);
|
|
|
|
// 创建一个session, 把Jsessionid发给前端。前端会跳回首页, 首页会负责查看有没有session
|
|
sess.setAttribute("username",u.getUsername());
|
|
Cookie cookie = new Cookie("JSESSIONID",sess.getId());
|
|
resp.addCookie(cookie);
|
|
|
|
}
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 判断用户可否登录
|
|
* @param userDTO
|
|
* @param sess
|
|
* @return
|
|
*/
|
|
@Override
|
|
public Message checkUser(UserDTO userDTO, HttpServletResponse resp, HttpSession sess) {
|
|
|
|
log.info("请求登录的用户名是:{}",userDTO.getUsername());
|
|
log.info("请求登录的密码是:{}",userDTO.getPassword());
|
|
|
|
Message msg = new Message();
|
|
UserPO userPO = userRepository.getUserByUsername(userDTO.getUsername()); //判断用户名是否存在
|
|
|
|
if(userPO == null){
|
|
msg.setFlag(-1);
|
|
msg.setMsg("用户不存在");
|
|
}else if(!userDTO.getPassword().equals(userPO.getPassword())){
|
|
msg.setFlag(-2);
|
|
msg.setMsg("密码不正确");
|
|
}else{
|
|
msg.setFlag(1);
|
|
msg.setMsg("登录成功");
|
|
|
|
// 创建一个session, 把Jsessionid发给前端。前端会跳回首页, 首页会负责查看有没有session
|
|
sess.setAttribute("username",userPO.getUsername());
|
|
Cookie cookie = new Cookie("JSESSIONID",sess.getId());
|
|
resp.addCookie(cookie);
|
|
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
|
|
|
|
/**
|
|
* 判断用户是否已经登录
|
|
* @param sess
|
|
* @return
|
|
*/
|
|
@Override
|
|
public Message checkStatus(HttpSession sess) {
|
|
|
|
Message msg = new Message();
|
|
if(sess.isNew()){
|
|
msg.setFlag(-4);
|
|
msg.setMsg("用户还未登录");
|
|
sess.invalidate();
|
|
}else{
|
|
msg.setFlag(3);
|
|
msg.setMsg("用户已经登录");
|
|
msg.setUsername((String)sess.getAttribute("username"));
|
|
}
|
|
|
|
return msg;
|
|
}
|
|
}
|