forked from xeon2007/SpringBoot_Wechat_Sell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSellerUserController.java
More file actions
76 lines (61 loc) · 2.77 KB
/
SellerUserController.java
File metadata and controls
76 lines (61 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.ldlood.controller;
import com.ldlood.config.ProjectUrlConfig;
import com.ldlood.constant.CookieConstant;
import com.ldlood.constant.RedisConstant;
import com.ldlood.dataobject.SellerInfo;
import com.ldlood.enums.ResultEnum;
import com.ldlood.service.SellerService;
import com.ldlood.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* Created by Ldlood on 2017/8/13.
*/
@Controller
@RequestMapping("/seller")
public class SellerUserController {
@Autowired
private SellerService sellerService;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private ProjectUrlConfig projectUrlConfig;
@GetMapping("/login")
public ModelAndView login(@RequestParam("openid") String openid,
HttpServletResponse httpServletResponse,
Map<String, Object> map) {
SellerInfo sellerInfo = sellerService.findSellerInfoByOpenid(openid);
if (sellerInfo == null) {
map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
map.put("url", "/seller/order/list");
return new ModelAndView("common/error", map);
}
String token = UUID.randomUUID().toString();
Integer expire = RedisConstant.EXPIRE;
stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS);
CookieUtil.set(httpServletResponse, CookieConstant.TOKEN, token, CookieConstant.EXPIRE);
return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/seller/order/list");
}
@GetMapping("/logout")
public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
if (cookie != null) {
stringRedisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
}
map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMessage());
map.put("url", "/seller/order/list");
return new ModelAndView("common/success", map);
}
}