|
|
|
@ -3,31 +3,120 @@ package com.hzu.bookingsystem.controller;
|
|
|
|
|
import com.hzu.bookingsystem.VO.ResultVO;
|
|
|
|
|
import com.hzu.bookingsystem.bean.User;
|
|
|
|
|
import com.hzu.bookingsystem.service.UserService;
|
|
|
|
|
import com.hzu.bookingsystem.utils.CookieUtil;
|
|
|
|
|
import com.hzu.bookingsystem.utils.ResultVOUtil;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.Cookie;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
@RestController
|
|
|
|
|
@CrossOrigin
|
|
|
|
|
@RequestMapping("/user")
|
|
|
|
|
public class UserController {
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserService userService;
|
|
|
|
|
|
|
|
|
|
//创建订单
|
|
|
|
|
@PostMapping(value = "/create", consumes = "application/json")
|
|
|
|
|
public ResultVO create(@RequestBody User user) {
|
|
|
|
|
userService.create(user);
|
|
|
|
|
@Autowired
|
|
|
|
|
private StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
|
|
// 登录
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public ResultVO login(@RequestBody User user,
|
|
|
|
|
HttpServletResponse response) {
|
|
|
|
|
System.out.println(user);
|
|
|
|
|
//1. openid去和数据库里的数据匹配
|
|
|
|
|
User user1 = userService.findByUnameAndPwd(user.getUsername(),user.getPassword());
|
|
|
|
|
System.out.println(user1);
|
|
|
|
|
if (user1 == null) {
|
|
|
|
|
return ResultVOUtil.error(-2,"账号或密码不正确");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//2. 设置token至redis
|
|
|
|
|
String token = UUID.randomUUID().toString();
|
|
|
|
|
Integer expire = 7200;
|
|
|
|
|
redisTemplate.opsForValue().set(token, user1.getUId().toString(), expire, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
|
|
//3. 设置token至cookie
|
|
|
|
|
CookieUtil.set(response, "token", token, expire);
|
|
|
|
|
return ResultVOUtil.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登出
|
|
|
|
|
@GetMapping("/logout")
|
|
|
|
|
public ResultVO logout(HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response) {
|
|
|
|
|
//1. 从cookie里查询
|
|
|
|
|
Cookie cookie = CookieUtil.get(request, "token");
|
|
|
|
|
if (cookie != null) {
|
|
|
|
|
//2. 清除redis
|
|
|
|
|
redisTemplate.opsForValue().getOperations().delete(cookie.getValue());
|
|
|
|
|
//3. 清除cookie
|
|
|
|
|
CookieUtil.set(response, "token", null, 0);
|
|
|
|
|
}
|
|
|
|
|
return ResultVOUtil.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//创建用户
|
|
|
|
|
@PostMapping(value = "/addUser", consumes = "application/json")
|
|
|
|
|
public ResultVO addUser(@RequestBody User user) {
|
|
|
|
|
userService.add(user);
|
|
|
|
|
return ResultVOUtil.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//查找用户详情
|
|
|
|
|
@GetMapping(value = "/findById")
|
|
|
|
|
public ResultVO<Map<String, User>> findById(@RequestParam("uId") Integer uId) {
|
|
|
|
|
User user = userService.findById(uId);
|
|
|
|
|
return ResultVOUtil.success(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改自己的用户信息
|
|
|
|
|
* @param user
|
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping(value = "/updateBySelf", consumes = "application/json")
|
|
|
|
|
public ResultVO updateBySelf(@RequestBody User user,
|
|
|
|
|
HttpServletRequest request){
|
|
|
|
|
Integer uId = getUIdByCookie(request);
|
|
|
|
|
if (uId == null){
|
|
|
|
|
return ResultVOUtil.error(-1,"未找到该用户ID");
|
|
|
|
|
}
|
|
|
|
|
user.setUId(uId);
|
|
|
|
|
userService.update(user);
|
|
|
|
|
return ResultVOUtil.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通过cookie查找Uid
|
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public Integer getUIdByCookie(HttpServletRequest request) {
|
|
|
|
|
Cookie cookie = CookieUtil.get(request, "token");
|
|
|
|
|
if (cookie == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// 从Redis里找该KEY
|
|
|
|
|
String tokenValue = redisTemplate.opsForValue().get(cookie.getValue());
|
|
|
|
|
if (!StringUtils.isEmpty(tokenValue)) {
|
|
|
|
|
// 返回用户UID
|
|
|
|
|
return Integer.valueOf(tokenValue);
|
|
|
|
|
} else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|