|
1 | 1 | package com.imooc.controller;
|
2 | 2 |
|
| 3 | +import com.imooc.config.ProjectUrlConfig; |
| 4 | +import com.imooc.constant.CookieConstant; |
| 5 | +import com.imooc.constant.RedisConstant; |
| 6 | +import com.imooc.dataobject.SellerInfo; |
| 7 | +import com.imooc.enums.ResultEnum; |
| 8 | +import com.imooc.service.SellerService; |
| 9 | +import com.imooc.utils.CookieUtil; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.data.redis.core.StringRedisTemplate; |
| 12 | +import org.springframework.stereotype.Controller; |
| 13 | +import org.springframework.web.bind.annotation.GetMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestParam; |
| 16 | +import org.springframework.web.servlet.ModelAndView; |
| 17 | + |
| 18 | +import javax.servlet.http.Cookie; |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
3 | 25 | /**
|
4 | 26 | * Created by SqMax on 2018/4/1.
|
5 | 27 | */
|
| 28 | +@Controller |
| 29 | +@RequestMapping("/seller") |
6 | 30 | public class SellerUserController {
|
| 31 | + |
| 32 | + @Autowired |
| 33 | + private SellerService sellerService; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private StringRedisTemplate redisTemplate; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private ProjectUrlConfig projectUrlConfig; |
| 40 | + |
| 41 | + @GetMapping("/login") |
| 42 | + public ModelAndView login(@RequestParam("openid") String openid, |
| 43 | + HttpServletResponse response, |
| 44 | + Map<String,Object> map){ |
| 45 | + |
| 46 | + //1.openid去和数据库里的数据匹配 |
| 47 | + SellerInfo sellerInfo=sellerService.findSellerInfoByOpenid(openid); |
| 48 | + if(sellerInfo==null){ |
| 49 | + map.put("msg", ResultEnum.LOGIN_FAIL.getMessage()); |
| 50 | + map.put("url","/sell/seller/order/list"); |
| 51 | + return new ModelAndView("common/error"); |
| 52 | + } |
| 53 | + //2.设置token至redis |
| 54 | + String token= UUID.randomUUID().toString(); |
| 55 | + Integer expire= RedisConstant.EXPIRE; |
| 56 | + redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS); |
| 57 | + |
| 58 | + //3.设置token至cookie |
| 59 | + CookieUtil.set(response, CookieConstant.TOKEN,token,expire); |
| 60 | + |
| 61 | + return new ModelAndView("redirect:"+projectUrlConfig.getSell()+"/sell/seller/order/list"); |
| 62 | + } |
| 63 | + |
| 64 | + @GetMapping("/logout") |
| 65 | + public ModelAndView logout(HttpServletRequest request, |
| 66 | + HttpServletResponse response, |
| 67 | + Map<String,Object> map){ |
| 68 | + |
| 69 | + //1.从cookie里查询 |
| 70 | + Cookie cookie=CookieUtil.get(request,CookieConstant.TOKEN); |
| 71 | + if(cookie!=null){ |
| 72 | + //2.清除redis |
| 73 | + redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue())); |
| 74 | + //3.清除cookie |
| 75 | + CookieUtil.set(response,CookieConstant.TOKEN,null,0); |
| 76 | + } |
| 77 | + map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage()); |
| 78 | + map.put("url","/sell/seller/order/list"); |
| 79 | + return new ModelAndView("common/success",map); |
| 80 | + |
| 81 | + } |
7 | 82 | }
|
0 commit comments