| @@ -0,0 +1,30 @@ | |||
| package com.iformall.douyin.miniapp.api; | |||
| import cn.binarywang.wx.miniapp.bean.*; | |||
| import com.google.gson.JsonObject; | |||
| import com.iformall.douyin.miniapp.api.bean.TtMaSubscribeMessage; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| /** | |||
| * <pre> | |||
| * 消息发送接口 | |||
| * </pre> | |||
| * | |||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
| */ | |||
| public interface TtMaMsgService { | |||
| String SUBSCRIBE_MSG_SEND_URL = "https://developer.toutiao.com/api/apps/subscribe_notification/developer/v1/notify"; | |||
| /** | |||
| * <pre> | |||
| * 发送订阅消息 | |||
| * https://microapp.bytedance.com/docs/zh-CN/mini-app/develop/server/subscribe-notification/notify/ | |||
| * </pre> | |||
| * | |||
| * @param subscribeMessage 订阅消息 | |||
| * @throws WxErrorException . | |||
| */ | |||
| void sendSubscribeMsg(TtMaSubscribeMessage subscribeMessage) throws WxErrorException; | |||
| } | |||
| @@ -127,4 +127,6 @@ public interface TtMaService { | |||
| */ | |||
| TtMaQrcodeService getQrcodeService(); | |||
| TtMaMsgService getMsgService(); | |||
| } | |||
| @@ -0,0 +1,30 @@ | |||
| package com.iformall.douyin.miniapp.api.bean; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage; | |||
| import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | |||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaRetainInfo; | |||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaUserPortrait; | |||
| import cn.binarywang.wx.miniapp.bean.analysis.WxMaVisitDistribution; | |||
| import cn.binarywang.wx.miniapp.bean.code.WxMaCodeCommitRequest; | |||
| import cn.binarywang.wx.miniapp.bean.code.WxMaCodeVersionDistribution; | |||
| import cn.binarywang.wx.miniapp.util.json.*; | |||
| import com.google.gson.Gson; | |||
| import com.google.gson.GsonBuilder; | |||
| /** | |||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
| */ | |||
| public class TtMaGsonBuilder { | |||
| private static final GsonBuilder INSTANCE = new GsonBuilder(); | |||
| static { | |||
| INSTANCE.disableHtmlEscaping(); | |||
| INSTANCE.registerTypeAdapter(TtMaSubscribeMessage.class, new TtMaSubscribeMessageGsonAdapter()); | |||
| } | |||
| public static Gson create() { | |||
| return INSTANCE.create(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,94 @@ | |||
| package com.iformall.douyin.miniapp.api.bean; | |||
| import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||
| import lombok.*; | |||
| import java.io.Serializable; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| /** | |||
| * 订阅消息. | |||
| * @author S | |||
| */ | |||
| @Getter | |||
| @Setter | |||
| @NoArgsConstructor | |||
| @AllArgsConstructor | |||
| @Builder | |||
| public class TtMaSubscribeMessage implements Serializable { | |||
| private static final long serialVersionUID = -5931206947637944325L; | |||
| // private String accessToken; | |||
| private String appId; | |||
| /** | |||
| * 接收者(用户)的 openid. | |||
| * <pre> | |||
| * 参数:touser | |||
| * 是否必填: 是 | |||
| * 描述: 接收者(用户)的 openid | |||
| * </pre> | |||
| */ | |||
| private String openId; | |||
| /** | |||
| * 所需下发的模板消息的id. | |||
| * <pre> | |||
| * 参数:template_id | |||
| * 是否必填: 是 | |||
| * 描述: 所需下发的模板消息的id | |||
| * </pre> | |||
| */ | |||
| private String tplId; | |||
| /** | |||
| * 点击模板卡片后的跳转页面,仅限本小程序内的页面. | |||
| * <pre> | |||
| * 参数:page | |||
| * 是否必填: 否 | |||
| * 描述: 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 | |||
| * </pre> | |||
| */ | |||
| private String page; | |||
| /** | |||
| * 模板内容,不填则下发空模板. | |||
| * <pre> | |||
| * 参数:data | |||
| * 是否必填: 是 | |||
| * 描述: 模板内容,不填则下发空模板 | |||
| * </pre> | |||
| */ | |||
| private List<Data> data; | |||
| public TtMaSubscribeMessage addData(Data datum) { | |||
| if (this.data == null) { | |||
| this.data = new ArrayList<>(); | |||
| } | |||
| this.data.add(datum); | |||
| return this; | |||
| } | |||
| public String toJson() { | |||
| return TtMaGsonBuilder.create().toJson(this); | |||
| } | |||
| @lombok.Data | |||
| @NoArgsConstructor | |||
| @AllArgsConstructor | |||
| public static class Data implements Serializable { | |||
| private static final long serialVersionUID = 1L; | |||
| private String name; | |||
| private String value; | |||
| } | |||
| } | |||
| @@ -0,0 +1,44 @@ | |||
| package com.iformall.douyin.miniapp.api.bean; | |||
| import com.google.gson.JsonElement; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonSerializationContext; | |||
| import com.google.gson.JsonSerializer; | |||
| import java.lang.reflect.Type; | |||
| /** | |||
| * . | |||
| * | |||
| * @author S | |||
| */ | |||
| public class TtMaSubscribeMessageGsonAdapter implements JsonSerializer<TtMaSubscribeMessage> { | |||
| @Override | |||
| public JsonElement serialize(TtMaSubscribeMessage message, Type typeOfSrc, JsonSerializationContext context) { | |||
| JsonObject messageJson = new JsonObject(); | |||
| // messageJson.addProperty("access_token", message.getAccessToken()); | |||
| messageJson.addProperty("app_id", message.getAppId()); | |||
| messageJson.addProperty("tpl_id", message.getTplId()); | |||
| messageJson.addProperty("open_id", message.getOpenId()); | |||
| if (message.getPage() != null) { | |||
| messageJson.addProperty("page", message.getPage()); | |||
| } | |||
| JsonObject data = new JsonObject(); | |||
| messageJson.add("data", data); | |||
| if (message.getData() == null) { | |||
| return messageJson; | |||
| } | |||
| for (TtMaSubscribeMessage.Data datum : message.getData()) { | |||
| JsonObject dataJson = new JsonObject(); | |||
| dataJson.addProperty("value", datum.getValue()); | |||
| data.add(datum.getName(), dataJson); | |||
| } | |||
| return messageJson; | |||
| } | |||
| } | |||
| @@ -0,0 +1,32 @@ | |||
| package com.iformall.douyin.miniapp.api.impl; | |||
| import cn.binarywang.wx.miniapp.bean.*; | |||
| import cn.binarywang.wx.miniapp.constant.WxMaConstants; | |||
| import com.google.gson.JsonObject; | |||
| import com.google.gson.JsonParser; | |||
| import com.iformall.douyin.miniapp.api.TtMaMsgService; | |||
| import com.iformall.douyin.miniapp.api.TtMaService; | |||
| import com.iformall.douyin.miniapp.api.bean.TtMaSubscribeMessage; | |||
| import lombok.AllArgsConstructor; | |||
| import me.chanjar.weixin.common.WxType; | |||
| import me.chanjar.weixin.common.error.WxError; | |||
| import me.chanjar.weixin.common.error.WxErrorException; | |||
| /** | |||
| * @author <a href="https://github.com/binarywang">Binary Wang</a> | |||
| */ | |||
| @AllArgsConstructor | |||
| public class TtMaMsgServiceImpl implements TtMaMsgService { | |||
| private static final JsonParser JSON_PARSER = new JsonParser(); | |||
| private TtMaService ttMaService; | |||
| @Override | |||
| public void sendSubscribeMsg(TtMaSubscribeMessage subscribeMessage) throws WxErrorException { | |||
| String responseContent = this.ttMaService.post(SUBSCRIBE_MSG_SEND_URL, subscribeMessage); | |||
| JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||
| if (jsonObject.get("err_no").getAsInt() != 0) { | |||
| throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); | |||
| } | |||
| } | |||
| } | |||
| @@ -9,10 +9,8 @@ import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject; | |||
| import com.google.common.base.Joiner; | |||
| import com.google.gson.Gson; | |||
| import com.google.gson.JsonParser; | |||
| import com.iformall.douyin.miniapp.api.TtMaQrcode; | |||
| import com.iformall.douyin.miniapp.api.TtMaQrcodeService; | |||
| import com.iformall.douyin.miniapp.api.TtMaService; | |||
| import com.iformall.douyin.miniapp.api.TtMaUserService; | |||
| import com.iformall.douyin.miniapp.api.*; | |||
| import com.iformall.douyin.miniapp.api.bean.TtMaGsonBuilder; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import me.chanjar.weixin.common.WxType; | |||
| import me.chanjar.weixin.common.bean.WxAccessToken; | |||
| @@ -51,6 +49,7 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||
| private TtMaUserService userService = new TtMaUserServiceImpl(this); | |||
| private TtMaQrcodeService qrcodeService = new TtMaQrcodeServiceImpl(this); | |||
| private TtMaMsgService msgService = new TtMaMsgServiceImpl(this); | |||
| private int retrySleepMillis = 1000; | |||
| private int maxRetryTimes = 5; | |||
| @@ -172,7 +171,7 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||
| @Override | |||
| public String post(String url, Object obj) throws WxErrorException { | |||
| return this.execute(SimplePostRequestExecutor.create(this), url, WxGsonBuilder.create().toJson(obj)); | |||
| return this.execute(SimplePostRequestExecutor.create(this), url, TtMaGsonBuilder.create().toJson(obj)); | |||
| } | |||
| /** | |||
| @@ -292,4 +291,9 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||
| return this.qrcodeService; | |||
| } | |||
| @Override | |||
| public TtMaMsgService getMsgService() { | |||
| return this.msgService; | |||
| } | |||
| } | |||
| @@ -25,7 +25,7 @@ public class BaseTtPayAdapterService { | |||
| throws Exception { | |||
| OrderQueryResult orderQueryResult = DouYinPayHelper.orderQuery(appInfo.getAppId(), payAccount.getApiKey(), oldRecord.getPayOrderNo(), null); | |||
| int code = DouYinPayHelper.getPayStatusFromOrderQueryResult(orderQueryResult,oldRecord.getPayOrderNo()); | |||
| PayQueryAdapterResult result = new PayQueryAdapterResult(code, EnumPayStatus.getEnum(code).getMessage(), orderQueryResult,oldRecord.getPayOrderNo(),orderQueryResult.getPayTime()); | |||
| PayQueryAdapterResult result = new PayQueryAdapterResult(code, EnumPayStatus.getEnum(code).getMessage(), orderQueryResult,oldRecord.getTransactionId(),orderQueryResult.getPayTime()); | |||
| return result; | |||
| } | |||
| @@ -48,7 +48,8 @@ public class TtMiniAppPayAdapterService extends BaseTtPayAdapterService implemen | |||
| CreatePreOrderResult preOrderResult = DouYinPayHelper.createPreOrder(preOrder); | |||
| if(preOrderResult.isSuccess()){ | |||
| record.setPrepayId(preOrderResult.getOrderId()); | |||
| // record.setPrepayId(preOrderResult.getOrderId()); | |||
| record.setTransactionId(preOrderResult.getOrderId()); | |||
| }else{ | |||
| record.setFailReason(preOrderResult.getMsg()); | |||
| } | |||