|
@@ -3,17 +3,31 @@ package com.zanxiang.sdk.service.Impl;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zanxiang.common.exception.BaseException;
|
|
|
+import com.zanxiang.module.util.JsonUtil;
|
|
|
import com.zanxiang.module.util.bean.BeanUtil;
|
|
|
import com.zanxiang.mybatis.entity.GameApplet;
|
|
|
+import com.zanxiang.mybatis.entity.Order;
|
|
|
+import com.zanxiang.mybatis.entity.User;
|
|
|
import com.zanxiang.mybatis.mapper.GameAppletMapper;
|
|
|
+import com.zanxiang.sdk.domain.dto.AppletMsgDTO;
|
|
|
import com.zanxiang.sdk.domain.dto.GameAppletDTO;
|
|
|
import com.zanxiang.sdk.domain.params.UserData;
|
|
|
import com.zanxiang.sdk.domain.vo.GameAppletVO;
|
|
|
import com.zanxiang.sdk.service.GameAppletService;
|
|
|
+import com.zanxiang.sdk.service.OrderService;
|
|
|
+import com.zanxiang.sdk.service.UserService;
|
|
|
+import com.zanxiang.sdk.service.api.WxApiService;
|
|
|
import com.zanxiang.sdk.util.SignUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
|
|
+import java.net.URI;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
/**
|
|
@@ -25,17 +39,18 @@ import java.util.Objects;
|
|
|
@Service
|
|
|
public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApplet> implements GameAppletService {
|
|
|
|
|
|
- /**
|
|
|
- * 小游戏消息推送
|
|
|
- *
|
|
|
- * @param appId 应用程序id
|
|
|
- * @param signature 签名
|
|
|
- * @param timestamp 时间戳
|
|
|
- * @param nonce 现时标志
|
|
|
- * @param echoStr 回声str
|
|
|
- * @return {@link String}
|
|
|
- * @throws Exception 异常
|
|
|
- */
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderService orderService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WxApiService wxApiService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
@Override
|
|
|
public String appletMsgCheck(String appId, String signature, String timestamp, String nonce, String echoStr) throws Exception {
|
|
|
log.error("验证参数, appId : {}, signature : {}, timestamp : {}, nonce : {}, echoStr : {}", appId, signature, timestamp, nonce, echoStr);
|
|
@@ -52,21 +67,13 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
|
return echoStr;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 小游戏消息推送
|
|
|
- *
|
|
|
- * @param appId 应用程序id
|
|
|
- * @param signature 签名
|
|
|
- * @param timestamp 时间戳
|
|
|
- * @param nonce 现时标志
|
|
|
- * @param echoStr 回声str
|
|
|
- * @return {@link String}
|
|
|
- * @throws Exception 异常
|
|
|
- */
|
|
|
@Override
|
|
|
- public String appletMsg(String appId, String signature, String timestamp, String nonce, String echoStr, String postData) throws Exception {
|
|
|
- log.error("接收到事件消息, appId : {}, signature : {}, timestamp : {}, nonce : {}, echoStr : {}, postData : {}",
|
|
|
- appId, signature, timestamp, nonce, echoStr, postData);
|
|
|
+ public String appletMsg(String appId, String signature, String timestamp, String nonce, String postData) throws Exception {
|
|
|
+ log.error("接收到事件消息, appId : {}, signature : {}, timestamp : {}, nonce : {}, postData : {}",
|
|
|
+ appId, signature, timestamp, nonce, postData);
|
|
|
+ if (Strings.isBlank(postData)) {
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
GameApplet gameApplet = super.getOne(new LambdaQueryWrapper<GameApplet>()
|
|
|
.eq(GameApplet::getAppId, appId));
|
|
|
if (gameApplet == null) {
|
|
@@ -77,15 +84,50 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
|
if (!Objects.equals(mySignature, signature)) {
|
|
|
return "success";
|
|
|
}
|
|
|
+ AppletMsgDTO appletMsgDTO = JsonUtil.toObj(postData, AppletMsgDTO.class);
|
|
|
+ //查询用户信息
|
|
|
+ User user = userService.getOne(new LambdaQueryWrapper<User>()
|
|
|
+ .eq(User::getGameId, gameApplet.getGameId())
|
|
|
+ .eq(User::getOpenId, appletMsgDTO.getFromUserName()));
|
|
|
+ if (user == null) {
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+ //查询用户最新订单
|
|
|
+ Order order = orderService.getOne(new LambdaQueryWrapper<Order>()
|
|
|
+ .eq(Order::getUserId, user.getId())
|
|
|
+ .orderByDesc(Order::getCreateTime)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (order == null) {
|
|
|
+ return "success";
|
|
|
+ }
|
|
|
+ this.sendCustomMessage(gameApplet, user, order);
|
|
|
return "success";
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 通过游戏id查询
|
|
|
- *
|
|
|
- * @param gameId 游戏id
|
|
|
- * @return {@link GameAppletDTO}
|
|
|
- */
|
|
|
+ private void sendCustomMessage(GameApplet gameApplet, User user, Order order) {
|
|
|
+ //获取接口token
|
|
|
+ String accessToken = wxApiService.getAccessToken(gameApplet.getAppId(), gameApplet.getAppSecret());
|
|
|
+ URI uri = UriComponentsBuilder.fromHttpUrl("https://api.weixin.qq.com/cgi-bin/message/custom/business/send")
|
|
|
+ .queryParam("access_token", accessToken)
|
|
|
+ .build().toUri();
|
|
|
+ Map<String, Object> paramMap = new HashMap<>(4);
|
|
|
+ paramMap.put("touser", user.getOpenId());
|
|
|
+ paramMap.put("msgtype", "link");
|
|
|
+
|
|
|
+ Map<String, Object> linkMap = new HashMap<>(4);
|
|
|
+ linkMap.put("title", "点我充值");
|
|
|
+ linkMap.put("description", "点我充值" + order.getAmount() + "元,用于购买" + order.getAmount() + "元档充值");
|
|
|
+ linkMap.put("url", "http://gg.84game.cn/wechatPay/index.html?userId=" + user.getId() + "&orderId=" + order.getId());
|
|
|
+ //linkMap.put("thumb_url", "图片地址");
|
|
|
+
|
|
|
+ paramMap.put("link", linkMap);
|
|
|
+
|
|
|
+ log.error("客服消息发送参数, paramMap : {}", JsonUtil.toString(paramMap));
|
|
|
+ // 发送请求
|
|
|
+ String result = restTemplate.postForObject(uri, paramMap, String.class);
|
|
|
+ log.error("客服消息发送结果, result : {}", result);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public GameAppletDTO getByGameId(Long gameId) {
|
|
|
GameApplet gameApplet = super.getOne(new LambdaQueryWrapper<GameApplet>()
|
|
@@ -96,12 +138,6 @@ public class GameAppletServiceImpl extends ServiceImpl<GameAppletMapper, GameApp
|
|
|
return BeanUtil.copy(gameApplet, GameAppletDTO.class);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 微信小游戏初始化获取信息
|
|
|
- *
|
|
|
- * @param userData : 用户信息
|
|
|
- * @return {@link GameAppletVO}
|
|
|
- */
|
|
|
@Override
|
|
|
public GameAppletVO gameAppletInit(UserData userData) {
|
|
|
GameApplet gameApplet = super.getOne(new LambdaQueryWrapper<GameApplet>()
|