Переглянути джерело

:new: #1373 增加小程序自定义模板的支持:主要是导入抽样数据接口的实现,以及接收消息时对相关属性的支持

Binary Wang 5 роки тому
батько
коміт
96b370ea01

+ 41 - 1
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

@@ -23,6 +23,11 @@ public interface WxMaService {
   String GET_PAID_UNION_ID_URL = "https://api.weixin.qq.com/wxa/getpaidunionid";
 
   /**
+   * 导入抽样数据
+   */
+  String SET_DYNAMIC_DATA_URL = "https://api.weixin.qq.com/wxa/setdynamicdata";
+
+  /**
    * 获取登录后的session信息.
    *
    * @param jsCode 登录时获取的 code
@@ -30,6 +35,22 @@ public interface WxMaService {
   WxMaJscode2SessionResult jsCode2SessionInfo(String jsCode) throws WxErrorException;
 
   /**
+   * 导入抽样数据
+   * <pre>
+   * 第三方通过调用微信API,将数据写入到setdynamicdata这个API。每个Post数据包不超过5K,若数据过多可开多进(线)程并发导入数据(例如:数据量为十万量级可以开50个线程并行导数据)。
+   * 文档地址:https://wsad.weixin.qq.com/wsad/zh_CN/htmledition/widget-docs-v3/html/custom/quickstart/implement/import/index.html
+   * http请求方式:POST http(s)://api.weixin.qq.com/wxa/setdynamicdata?access_token=ACCESS_TOKEN
+   * </pre>
+   *
+   * @param data     推送到微信后台的数据列表,该数据被微信用于流量分配,注意该字段为string类型而不是object
+   * @param lifespan 数据有效时间,秒为单位,一般为86400,一天一次导入的频率
+   * @param scene    1代表用于搜索的数据
+   * @param type     用于标识数据所属的服务类目
+   * @throws WxErrorException .
+   */
+  void setDynamicData(int lifespan, String type, int scene, String data) throws WxErrorException;
+
+  /**
    * <pre>
    * 验证消息的确来自微信服务器.
    * 详情请见: http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319&token=&lang=zh_CN
@@ -73,6 +94,8 @@ public interface WxMaService {
    * @param transactionId 非必填 微信支付订单号
    * @param mchId         非必填 微信支付分配的商户号,和商户订单号配合使用
    * @param outTradeNo    非必填  微信支付商户订单号,和商户号配合使用
+   * @return UnionId.
+   * @throws WxErrorException .
    */
   String getPaidUnionId(String openid, String transactionId, String mchId, String outTradeNo) throws WxErrorException;
 
@@ -97,6 +120,13 @@ public interface WxMaService {
    * 比{@link #get}和{@link #post}方法更灵活,可以自己构造RequestExecutor用来处理不同的参数和不同的返回类型。
    * 可以参考,{@link MediaUploadRequestExecutor}的实现方法
    * </pre>
+   *
+   * @param <E>      .
+   * @param <T>      .
+   * @param data     参数或请求数据
+   * @param executor 执行器
+   * @param uri      接口请求地址
+   * @return .
    */
   <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
 
@@ -105,6 +135,8 @@ public interface WxMaService {
    * 设置当微信系统响应系统繁忙时,要等待多少 retrySleepMillis(ms) * 2^(重试次数 - 1) 再发起重试.
    * 默认:1000ms
    * </pre>
+   *
+   * @param retrySleepMillis 重试等待毫秒数
    */
   void setRetrySleepMillis(int retrySleepMillis);
 
@@ -113,6 +145,8 @@ public interface WxMaService {
    * 设置当微信系统响应系统繁忙时,最大重试次数.
    * 默认:5次
    * </pre>
+   *
+   * @param maxRetryTimes 最大重试次数
    */
   void setMaxRetryTimes(int maxRetryTimes);
 
@@ -125,6 +159,8 @@ public interface WxMaService {
 
   /**
    * 注入 {@link WxMaConfig} 的实现.
+   *
+   * @param wxConfigProvider config
    */
   void setWxMaConfig(WxMaConfig wxConfigProvider);
 
@@ -233,18 +269,22 @@ public interface WxMaService {
 
   /**
    * 请求http请求相关信息.
+   *
+   * @return .
    */
   RequestHttp getRequestHttp();
 
   /**
    * 获取物流助手接口服务对象
    *
-   * @return
+   * @return .
    */
   WxMaExpressService getExpressService();
 
   /**
    * 获取云开发接口服务对象
+   *
+   * @return .
    */
   WxMaCloudService getCloudService();
 }

+ 13 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java

@@ -4,7 +4,9 @@ import cn.binarywang.wx.miniapp.api.*;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.config.WxMaConfig;
 import com.google.common.base.Joiner;
+import com.google.common.collect.ImmutableMap;
 import com.google.gson.Gson;
+import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import lombok.extern.slf4j.Slf4j;
 import me.chanjar.weixin.common.WxType;
@@ -184,6 +186,17 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
   }
 
   @Override
+  public void setDynamicData(int lifespan, String type, int scene, String data) throws WxErrorException {
+    JsonObject jsonObject = new JsonObject();
+    jsonObject.addProperty("lifespan", lifespan);
+    jsonObject.addProperty("query", WxGsonBuilder.create().toJson(ImmutableMap.of("type", type)));
+    jsonObject.addProperty("data", data);
+    jsonObject.addProperty("scene", scene);
+
+    this.post(SET_DYNAMIC_DATA_URL, jsonObject.toString());
+  }
+
+  @Override
   public boolean checkSignature(String timestamp, String nonce, String signature) {
     try {
       return SHA1.gen(this.getWxMaConfig().getToken(), timestamp, nonce).equals(signature);

+ 9 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaMessage.java

@@ -135,6 +135,15 @@ public class WxMaMessage implements Serializable {
   @XStreamConverter(value = XStreamCDataConverter.class)
   private String statusCode;
 
+  @SerializedName("Scene")
+  @XStreamAlias("Scene")
+  private Integer scene;
+
+  @SerializedName("Query")
+  @XStreamAlias("Query")
+  @XStreamConverter(value = XStreamCDataConverter.class)
+  private String query;
+
   public static WxMaMessage fromXml(String xml) {
     return XStreamTransformer.fromXml(WxMaMessage.class, xml);
   }

+ 135 - 4
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImplTest.java

@@ -1,16 +1,17 @@
 package cn.binarywang.wx.miniapp.api.impl;
 
-import org.apache.commons.lang3.StringUtils;
-import org.testng.annotations.*;
-
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.config.WxMaConfig;
 import cn.binarywang.wx.miniapp.test.ApiTestModule;
 import com.google.inject.Inject;
 import me.chanjar.weixin.common.error.WxErrorException;
+import org.apache.commons.lang3.StringUtils;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.testng.Assert.*;
+import static org.testng.Assert.assertNotEquals;
+import static org.testng.Assert.assertTrue;
 
 /**
  * @author <a href="https://github.com/binarywang">Binary Wang</a>
@@ -49,4 +50,134 @@ public class WxMaServiceImplTest {
 
     System.out.println(postResult);
   }
+
+  @Test
+  public void testGetRequestHttpClient() {
+  }
+
+  @Test
+  public void testGetRequestHttpProxy() {
+  }
+
+  @Test
+  public void testGetRequestType() {
+  }
+
+  @Test
+  public void testInitHttp() {
+  }
+
+  @Test
+  public void testGetRequestHttp() {
+  }
+
+  @Test
+  public void testGetAccessToken() {
+  }
+
+  @Test
+  public void testJsCode2SessionInfo() {
+  }
+
+  @Test
+  public void testSetDynamicData() throws WxErrorException {
+    this.wxService.setDynamicData(86400, "1000001", 1,
+      "{\"items\":[{\"stock_name\":\"腾讯控股\", \"stock_code\":\"00700\", \"stock_market\":\"hk\"}], \"attribute\":{\"count\":2, \"totalcount\": 100, \"id\": \"XXX\", \"seq\": 0}}");
+  }
+
+  @Test
+  public void testCheckSignature() {
+  }
+
+  @Test
+  public void testTestGetAccessToken() {
+  }
+
+  @Test
+  public void testGet() {
+  }
+
+  @Test
+  public void testExecute() {
+  }
+
+  @Test
+  public void testGetWxMaConfig() {
+  }
+
+  @Test
+  public void testSetWxMaConfig() {
+  }
+
+  @Test
+  public void testSetRetrySleepMillis() {
+  }
+
+  @Test
+  public void testSetMaxRetryTimes() {
+  }
+
+  @Test
+  public void testGetMsgService() {
+  }
+
+  @Test
+  public void testGetMediaService() {
+  }
+
+  @Test
+  public void testGetUserService() {
+  }
+
+  @Test
+  public void testGetQrcodeService() {
+  }
+
+  @Test
+  public void testGetTemplateService() {
+  }
+
+  @Test
+  public void testGetSubscribeService() {
+  }
+
+  @Test
+  public void testGetAnalysisService() {
+  }
+
+  @Test
+  public void testGetCodeService() {
+  }
+
+  @Test
+  public void testGetJsapiService() {
+  }
+
+  @Test
+  public void testGetSettingService() {
+  }
+
+  @Test
+  public void testGetShareService() {
+  }
+
+  @Test
+  public void testGetRunService() {
+  }
+
+  @Test
+  public void testGetSecCheckService() {
+  }
+
+  @Test
+  public void testGetPluginService() {
+  }
+
+  @Test
+  public void testGetExpressService() {
+  }
+
+  @Test
+  public void testGetCloudService() {
+  }
 }