Skip to content

Commit 17e59e1

Browse files
author
sqmax
committed
wechat pay done(liao account)
1 parent 99a30f7 commit 17e59e1

34 files changed

+700
-46
lines changed

pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@
6868
<groupId>org.springframework.boot</groupId>
6969
<artifactId>spring-boot-starter-freemarker</artifactId>
7070
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-starter-data-redis</artifactId>
74+
</dependency>
75+
<dependency>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-starter-websocket</artifactId>
78+
</dependency>
7179
</dependencies>
7280

7381
<build>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
11
package com.imooc.aspect;
22

3+
import com.imooc.constant.CookieConstant;
4+
import com.imooc.constant.RedisConstant;
5+
import com.imooc.exception.SellerAuthorizeException;
6+
import com.imooc.utils.CookieUtil;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.aspectj.lang.annotation.Before;
10+
import org.aspectj.lang.annotation.Pointcut;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.data.redis.core.StringRedisTemplate;
13+
import org.springframework.stereotype.Component;
14+
import org.springframework.util.StringUtils;
15+
import org.springframework.web.context.request.RequestContextHolder;
16+
import org.springframework.web.context.request.ServletRequestAttributes;
17+
18+
import javax.servlet.http.Cookie;
19+
import javax.servlet.http.HttpServletRequest;
20+
321
/**
422
* Created by SqMax on 2018/4/2.
523
*/
24+
@Aspect
25+
@Component
26+
@Slf4j
627
public class SellerAuthorizeAspect {
28+
29+
@Autowired
30+
private StringRedisTemplate redisTemplate;
31+
32+
// @Pointcut("execution(public * com.imooc.controller.Seller*.*(..))"+
33+
// "&& !execution(public * com.imooc.controller.SellerUserController.*(..))")
34+
// public void verify(){}
35+
//
36+
// @Before("verify()")
37+
// public void doVerify(){
38+
// ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
39+
// HttpServletRequest request=attributes.getRequest();
40+
//
41+
// //查询cookie
42+
// Cookie cookie= CookieUtil.get(request,CookieConstant.TOKEN);
43+
// if (cookie==null){
44+
// log.warn("【登录校验】Cookie中查不到token");
45+
// throw new SellerAuthorizeException();
46+
// }
47+
//
48+
// //去redis查询
49+
// String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
50+
// if (StringUtils.isEmpty(tokenValue)){
51+
// log.warn("【登录校验】 Redis中查不到token");
52+
// throw new SellerAuthorizeException();
53+
// }
54+
// }
55+
756
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
package com.imooc.config;
22

3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
37
/**
48
* Created by SqMax on 2018/4/1.
59
*/
6-
public class ProjectUrl {
10+
@Data
11+
@ConfigurationProperties(prefix = "projectUrl")
12+
@Component
13+
public class ProjectUrlConfig {
14+
15+
/**
16+
* 微信公众平台授权url
17+
*/
18+
public String wechatMpAuthorize;
19+
20+
/**
21+
* 微信开放平台授权url
22+
*/
23+
public String wechatOpenAuthorize;
24+
25+
/**
26+
* 点餐系统
27+
*/
28+
public String sell;
29+
730
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
package com.imooc.config;
22

3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
6+
37
/**
48
* Created by SqMax on 2018/4/2.
59
*/
10+
@Component
611
public class WebSocketConfig {
12+
13+
// @Bean
14+
// public ServerEndpointExporter serverEndpointExporter(){
15+
// return new ServerEndpointExporter();
16+
// }
717
}

src/main/java/com/imooc/config/WechatAccountConfig.java

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.springframework.boot.context.properties.ConfigurationProperties;
55
import org.springframework.stereotype.Component;
66

7+
import java.util.Map;
8+
79
/**
810
* Created by SqMax on 2018/3/23.
911
*/
@@ -12,9 +14,24 @@
1214
@ConfigurationProperties(prefix = "wechat")
1315
public class WechatAccountConfig {
1416

17+
/**
18+
* 公众平台id
19+
*/
1520
private String mpAppId;
21+
/**
22+
* 公众平台密钥
23+
*/
1624
private String mpAppSecret;
1725

26+
/**
27+
* 开放平台id
28+
*/
29+
private String openAppId;
30+
/**
31+
* 开放平台密钥
32+
*/
33+
private String openAppSecret;
34+
1835
/**
1936
* 商户号
2037
*/
@@ -35,4 +52,10 @@ public class WechatAccountConfig {
3552
*/
3653
private String notifyUrl;
3754

55+
/**
56+
* 微信模板id
57+
*/
58+
private Map<String,String> templateId;
59+
60+
3861
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
package com.imooc.config;
22

3+
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
4+
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
5+
import me.chanjar.weixin.mp.api.WxMpService;
6+
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.stereotype.Component;
10+
311
/**
412
* Created by SqMax on 2018/4/1.
513
*/
14+
@Component
615
public class WechatOpenConfig {
16+
17+
@Autowired
18+
private WechatAccountConfig accountConfig;
19+
20+
@Bean
21+
public WxMpService wxOpenService(){
22+
WxMpService wxOpenService=new WxMpServiceImpl();
23+
wxOpenService.setWxMpConfigStorage(wxOpenConfigStorage());
24+
return wxOpenService;
25+
}
26+
@Bean
27+
public WxMpConfigStorage wxOpenConfigStorage(){
28+
WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage=new WxMpInMemoryConfigStorage();
29+
wxMpInMemoryConfigStorage.setAppId(accountConfig.getOpenAppId());
30+
wxMpInMemoryConfigStorage.setSecret(accountConfig.getOpenAppSecret());
31+
return wxMpInMemoryConfigStorage;
32+
}
733
}

src/main/java/com/imooc/constant/CookieConstant.java

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
* Created by SqMax on 2018/4/1.
55
*/
66
public interface CookieConstant {
7+
8+
String TOKEN="token";
9+
10+
Integer EXPIRE=7200;
711
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.imooc.constant;
22

33
/**
4+
* redis常量
45
* Created by SqMax on 2018/4/1.
56
*/
6-
public class RedisConstant {
7+
public interface RedisConstant {
8+
9+
String TOKEN_PREFIX="token_%s";
10+
11+
Integer EXPIRE=7200;//2小时
712
}

src/main/java/com/imooc/controller/PayController.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
import com.imooc.exception.SellException;
66
import com.imooc.service.OrderService;
77
import com.imooc.service.PayService;
8+
import com.imooc.utils.JsonUtil;
9+
import com.lly835.bestpay.enums.BestPayTypeEnum;
10+
import com.lly835.bestpay.model.PayRequest;
811
import com.lly835.bestpay.model.PayResponse;
12+
import lombok.extern.slf4j.Slf4j;
913
import org.springframework.beans.factory.annotation.Autowired;
1014
import org.springframework.stereotype.Controller;
15+
import org.springframework.ui.Model;
1116
import org.springframework.web.bind.annotation.*;
1217
import org.springframework.web.servlet.ModelAndView;
1318

@@ -17,7 +22,8 @@
1722
* Created by SqMax on 2018/3/26.
1823
*/
1924
@Controller
20-
@RequestMapping("/pay")
25+
//@RequestMapping("/pay")
26+
@Slf4j
2127
public class PayController {
2228

2329
@Autowired
@@ -26,6 +32,28 @@ public class PayController {
2632
@Autowired
2733
private PayService payService;
2834

35+
@GetMapping("/pay")
36+
public ModelAndView index(@RequestParam("openid") String openid,
37+
@RequestParam("orderId") String orderId,
38+
@RequestParam("returnUrl") String returnUrl,
39+
Map<String,Object> map){
40+
log.info("openid={}",openid);
41+
//1.查询订单
42+
// String orderId="1234563";
43+
OrderDTO orderDTO=orderService.findOne(orderId);
44+
if(orderDTO==null){
45+
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
46+
}
47+
//2.发起支付
48+
orderDTO.setBuyerOpenid(openid);
49+
PayResponse payResponse=payService.create(orderDTO);
50+
51+
map.put("payResponse",payResponse);
52+
map.put("returnUrl","http://www.imooc.com");
53+
54+
return new ModelAndView("pay/create",map);
55+
}
56+
2957
@GetMapping("/create")
3058
public ModelAndView create(@RequestParam("orderId") String orderId,
3159
@RequestParam("returnUrl") String returnUrl,
@@ -51,6 +79,7 @@ public ModelAndView create(@RequestParam("orderId") String orderId,
5179
@PostMapping("/notify")
5280
public ModelAndView notify(@RequestBody String notifyData){
5381

82+
log.info("notifyData:{}",notifyData);
5483
payService.notify(notifyData);
5584

5685
//返回给微信处理结果
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,82 @@
11
package com.imooc.controller;
22

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+
325
/**
426
* Created by SqMax on 2018/4/1.
527
*/
28+
@Controller
29+
@RequestMapping("/seller")
630
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+
}
782
}

0 commit comments

Comments
 (0)