-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
554 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.gxj.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "wechat") | ||
public class WechatAccountConfig { | ||
|
||
private String mpAppId; | ||
private String mpAppSecret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.gxj.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.context.annotation.Configuration; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Configuration | ||
public class WechatMpConfig { | ||
|
||
@Autowired | ||
private WechatAccountConfig accountConfig; | ||
@Bean | ||
public WxMpService wxMpService() { | ||
WxMpService wxMpService = new WxMpServiceImpl(); | ||
wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); | ||
return wxMpService; | ||
} | ||
|
||
@Bean | ||
public WxMpConfigStorage wxMpConfigStorage() { | ||
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); | ||
wxMpConfigStorage.setAppId(accountConfig.getMpAppId()); | ||
wxMpConfigStorage.setSecret(accountConfig.getMpAppSecret()); | ||
return wxMpConfigStorage; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.gxj.controller; | ||
|
||
import com.gxj.enums.ResultEnum; | ||
import com.gxj.exception.SellException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import me.chanjar.weixin.common.api.WxConsts; | ||
import me.chanjar.weixin.common.exception.WxErrorException; | ||
import me.chanjar.weixin.mp.api.WxMpService; | ||
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
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 java.net.URLEncoder; | ||
|
||
@Controller | ||
@RequestMapping("/wechat") | ||
@Slf4j | ||
public class WechatController { | ||
|
||
@Autowired | ||
private WxMpService wxMpService; | ||
|
||
@GetMapping("/authorize") | ||
public String authorize(@RequestParam("returnUrl") String returnUrl) { | ||
String url = "https://mytestsell.mynatapp.cc/sell/wechat/userInfo"; | ||
String redirectUrl = | ||
wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl)); | ||
log.info("【微信网页授权】获取code, result={}", redirectUrl); | ||
return "redirect:" + redirectUrl; | ||
} | ||
|
||
@GetMapping("/userInfo") | ||
public String userInfo(@RequestParam("code") String code, | ||
@RequestParam("state") String returnUrl) { | ||
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = new WxMpOAuth2AccessToken(); | ||
try { | ||
wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code); | ||
} catch (WxErrorException e){ | ||
log.error("【微信网页授权】{}", e); | ||
throw new SellException(ResultEnum.WECHAT_MP_ERROR.getCode(), e.getError().getErrorMsg()); | ||
} | ||
String openId = wxMpOAuth2AccessToken.getOpenId(); | ||
|
||
return "redirect:" + returnUrl + "?openid=" + openId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.gxj.controller; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
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.bind.annotation.RestController; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RestController | ||
@RequestMapping("/weixin") | ||
@Slf4j | ||
public class WeixinController { | ||
|
||
@GetMapping("/auth") | ||
public void auth(@RequestParam("code") String code) { | ||
log.info("进入auth方法...."); | ||
log.info("code={}", code); | ||
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx7aef45e7ab0a62e4&secret=4786ff747ab75b3e26ed59b28b72d3fb&code=" | ||
+ code + "&grant_type=authorization_code"; | ||
RestTemplate restTemplate = new RestTemplate(); | ||
String response = restTemplate.getForObject(url, String.class); | ||
log.info("response={}", response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.gxj.service; | ||
|
||
import com.gxj.dto.OrderDTO; | ||
|
||
public interface BuyerService { | ||
|
||
OrderDTO findOrderOne(String openid, String orderId); | ||
OrderDTO cancelOrder(String openid, String orderId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.gxj.service.impl; | ||
|
||
import com.gxj.dto.OrderDTO; | ||
import com.gxj.enums.ResultEnum; | ||
import com.gxj.exception.SellException; | ||
import com.gxj.service.BuyerService; | ||
import com.gxj.service.OrderService; | ||
import com.mysql.cj.util.StringUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class BuyerServiceImpl implements BuyerService { | ||
|
||
@Autowired | ||
private OrderService orderService; | ||
|
||
@Override | ||
public OrderDTO findOrderOne(String openid, String orderId) { | ||
return checkOrderOwner(openid, orderId); | ||
} | ||
|
||
@Override | ||
public OrderDTO cancelOrder(String openid, String orderId) { | ||
OrderDTO orderDTO = checkOrderOwner(openid, orderId); | ||
if (orderDTO == null) { | ||
log.error("【取消订单】查不到该订单, orderId={}", orderId); | ||
throw new SellException(ResultEnum.ORDER_NOT_EXIST); | ||
} | ||
return orderService.cancel(orderDTO); | ||
} | ||
|
||
private OrderDTO checkOrderOwner(String openid, String orderId) { | ||
if (StringUtils.isNullOrEmpty(openid)) { | ||
log.error("【查询订单详情】openid为空"); | ||
throw new SellException(ResultEnum.PARAM_ERROR); | ||
} | ||
if (StringUtils.isNullOrEmpty(orderId)) { | ||
log.error("【查询订单详情】orderId为空"); | ||
throw new SellException(ResultEnum.PARAM_ERROR); | ||
} | ||
OrderDTO orderDTO = orderService.findOne(orderId); | ||
if (!orderDTO.getBuyerOpenid().equalsIgnoreCase(openid)) { | ||
log.error("【查询订单详情】订单的openid不一致, openid={}, orderDTO={}", openid, orderDTO); | ||
throw new SellException(ResultEnum.ORDER_OWNER_ERROR); | ||
} | ||
return orderDTO; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/gxj/utils/serializer/Date2LongSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.gxj.utils.serializer; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
public class Date2LongSerializer extends JsonSerializer<Date> { | ||
|
||
@Override | ||
public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { | ||
jsonGenerator.writeNumber(date.getTime() / 1000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.