Skip to content

Commit

Permalink
wechat pay done(liao account)
Browse files Browse the repository at this point in the history
  • Loading branch information
sqmax committed Apr 10, 2018
1 parent 99a30f7 commit 17e59e1
Show file tree
Hide file tree
Showing 34 changed files with 700 additions and 46 deletions.
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/imooc/aspect/SellerAuthorizeAspect.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
package com.imooc.aspect;

import com.imooc.constant.CookieConstant;
import com.imooc.constant.RedisConstant;
import com.imooc.exception.SellerAuthorizeException;
import com.imooc.utils.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
* Created by SqMax on 2018/4/2.
*/
@Aspect
@Component
@Slf4j
public class SellerAuthorizeAspect {

@Autowired
private StringRedisTemplate redisTemplate;

// @Pointcut("execution(public * com.imooc.controller.Seller*.*(..))"+
// "&& !execution(public * com.imooc.controller.SellerUserController.*(..))")
// public void verify(){}
//
// @Before("verify()")
// public void doVerify(){
// ServletRequestAttributes attributes=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request=attributes.getRequest();
//
// //查询cookie
// Cookie cookie= CookieUtil.get(request,CookieConstant.TOKEN);
// if (cookie==null){
// log.warn("【登录校验】Cookie中查不到token");
// throw new SellerAuthorizeException();
// }
//
// //去redis查询
// String tokenValue=redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
// if (StringUtils.isEmpty(tokenValue)){
// log.warn("【登录校验】 Redis中查不到token");
// throw new SellerAuthorizeException();
// }
// }

}
25 changes: 24 additions & 1 deletion src/main/java/com/imooc/config/ProjectUrlConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package com.imooc.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* Created by SqMax on 2018/4/1.
*/
public class ProjectUrl {
@Data
@ConfigurationProperties(prefix = "projectUrl")
@Component
public class ProjectUrlConfig {

/**
* 微信公众平台授权url
*/
public String wechatMpAuthorize;

/**
* 微信开放平台授权url
*/
public String wechatOpenAuthorize;

/**
* 点餐系统
*/
public String sell;

}
10 changes: 10 additions & 0 deletions src/main/java/com/imooc/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.imooc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
* Created by SqMax on 2018/4/2.
*/
@Component
public class WebSocketConfig {

// @Bean
// public ServerEndpointExporter serverEndpointExporter(){
// return new ServerEndpointExporter();
// }
}
23 changes: 23 additions & 0 deletions src/main/java/com/imooc/config/WechatAccountConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* Created by SqMax on 2018/3/23.
*/
Expand All @@ -12,9 +14,24 @@
@ConfigurationProperties(prefix = "wechat")
public class WechatAccountConfig {

/**
* 公众平台id
*/
private String mpAppId;
/**
* 公众平台密钥
*/
private String mpAppSecret;

/**
* 开放平台id
*/
private String openAppId;
/**
* 开放平台密钥
*/
private String openAppSecret;

/**
* 商户号
*/
Expand All @@ -35,4 +52,10 @@ public class WechatAccountConfig {
*/
private String notifyUrl;

/**
* 微信模板id
*/
private Map<String,String> templateId;


}
26 changes: 26 additions & 0 deletions src/main/java/com/imooc/config/WechatOpenConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
package com.imooc.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
* Created by SqMax on 2018/4/1.
*/
@Component
public class WechatOpenConfig {

@Autowired
private WechatAccountConfig accountConfig;

@Bean
public WxMpService wxOpenService(){
WxMpService wxOpenService=new WxMpServiceImpl();
wxOpenService.setWxMpConfigStorage(wxOpenConfigStorage());
return wxOpenService;
}
@Bean
public WxMpConfigStorage wxOpenConfigStorage(){
WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage=new WxMpInMemoryConfigStorage();
wxMpInMemoryConfigStorage.setAppId(accountConfig.getOpenAppId());
wxMpInMemoryConfigStorage.setSecret(accountConfig.getOpenAppSecret());
return wxMpInMemoryConfigStorage;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/imooc/constant/CookieConstant.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
* Created by SqMax on 2018/4/1.
*/
public interface CookieConstant {

String TOKEN="token";

Integer EXPIRE=7200;
}
7 changes: 6 additions & 1 deletion src/main/java/com/imooc/constant/RedisConstant.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.imooc.constant;

/**
* redis常量
* Created by SqMax on 2018/4/1.
*/
public class RedisConstant {
public interface RedisConstant {

String TOKEN_PREFIX="token_%s";

Integer EXPIRE=7200;//2小时
}
31 changes: 30 additions & 1 deletion src/main/java/com/imooc/controller/PayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
import com.imooc.exception.SellException;
import com.imooc.service.OrderService;
import com.imooc.service.PayService;
import com.imooc.utils.JsonUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

Expand All @@ -17,7 +22,8 @@
* Created by SqMax on 2018/3/26.
*/
@Controller
@RequestMapping("/pay")
//@RequestMapping("/pay")
@Slf4j
public class PayController {

@Autowired
Expand All @@ -26,6 +32,28 @@ public class PayController {
@Autowired
private PayService payService;

@GetMapping("/pay")
public ModelAndView index(@RequestParam("openid") String openid,
@RequestParam("orderId") String orderId,
@RequestParam("returnUrl") String returnUrl,
Map<String,Object> map){
log.info("openid={}",openid);
//1.查询订单
// String orderId="1234563";
OrderDTO orderDTO=orderService.findOne(orderId);
if(orderDTO==null){
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
//2.发起支付
orderDTO.setBuyerOpenid(openid);
PayResponse payResponse=payService.create(orderDTO);

map.put("payResponse",payResponse);
map.put("returnUrl","http://www.imooc.com");

return new ModelAndView("pay/create",map);
}

@GetMapping("/create")
public ModelAndView create(@RequestParam("orderId") String orderId,
@RequestParam("returnUrl") String returnUrl,
Expand All @@ -51,6 +79,7 @@ public ModelAndView create(@RequestParam("orderId") String orderId,
@PostMapping("/notify")
public ModelAndView notify(@RequestBody String notifyData){

log.info("notifyData:{}",notifyData);
payService.notify(notifyData);

//返回给微信处理结果
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/com/imooc/controller/SellerUserController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,82 @@
package com.imooc.controller;

import com.imooc.config.ProjectUrlConfig;
import com.imooc.constant.CookieConstant;
import com.imooc.constant.RedisConstant;
import com.imooc.dataobject.SellerInfo;
import com.imooc.enums.ResultEnum;
import com.imooc.service.SellerService;
import com.imooc.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 SqMax on 2018/4/1.
*/
@Controller
@RequestMapping("/seller")
public class SellerUserController {

@Autowired
private SellerService sellerService;

@Autowired
private StringRedisTemplate redisTemplate;

@Autowired
private ProjectUrlConfig projectUrlConfig;

@GetMapping("/login")
public ModelAndView login(@RequestParam("openid") String openid,
HttpServletResponse response,
Map<String,Object> map){

//1.openid去和数据库里的数据匹配
SellerInfo sellerInfo=sellerService.findSellerInfoByOpenid(openid);
if(sellerInfo==null){
map.put("msg", ResultEnum.LOGIN_FAIL.getMessage());
map.put("url","/sell/seller/order/list");
return new ModelAndView("common/error");
}
//2.设置token至redis
String token= UUID.randomUUID().toString();
Integer expire= RedisConstant.EXPIRE;
redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX,token),openid,expire, TimeUnit.SECONDS);

//3.设置token至cookie
CookieUtil.set(response, CookieConstant.TOKEN,token,expire);

return new ModelAndView("redirect:"+projectUrlConfig.getSell()+"/sell/seller/order/list");
}

@GetMapping("/logout")
public ModelAndView logout(HttpServletRequest request,
HttpServletResponse response,
Map<String,Object> map){

//1.从cookie里查询
Cookie cookie=CookieUtil.get(request,CookieConstant.TOKEN);
if(cookie!=null){
//2.清除redis
redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX,cookie.getValue()));
//3.清除cookie
CookieUtil.set(response,CookieConstant.TOKEN,null,0);
}
map.put("msg",ResultEnum.LOGOUT_SUCCESS.getMessage());
map.put("url","/sell/seller/order/list");
return new ModelAndView("common/success",map);

}
}
Loading

0 comments on commit 17e59e1

Please sign in to comment.