From 2fda3b7c4daa0597a8f67d7d133e9e613f5006a0 Mon Sep 17 00:00:00 2001 From: lipengjun Date: Mon, 29 Jun 2020 19:50:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E9=97=B4=E5=95=86=E5=93=81=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaLiveGoodsService.java | 123 ++++++++++++++++ .../wx/miniapp/api/WxMaLiveService.java | 57 ++++++-- .../wx/miniapp/api/WxMaMaterialService.java | 46 ++++++ .../wx/miniapp/api/WxMaService.java | 14 ++ .../miniapp/api/impl/BaseWxMaServiceImpl.java | 12 ++ .../api/impl/WxMaLiveGoodsServiceImpl.java | 132 ++++++++++++++++++ .../miniapp/api/impl/WxMaLiveServiceImpl.java | 51 +++++-- .../api/impl/WxMaMaterialServiceImpl.java | 28 ++++ ...WxMaGetLiveInfo.java => WxMaLiveInfo.java} | 27 +++- .../wx/miniapp/bean/WxMaLiveResult.java | 59 ++++++++ .../impl/WxMaLiveGoodsServiceImplTest.java | 92 ++++++++++++ .../api/impl/WxMaLiveServiceImplTest.java | 51 +++++-- 12 files changed, 651 insertions(+), 41 deletions(-) create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveGoodsService.java create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaMaterialService.java create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImpl.java create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaMaterialServiceImpl.java rename weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/{WxMaGetLiveInfo.java => WxMaLiveInfo.java} (71%) create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java create mode 100644 weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImplTest.java diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveGoodsService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveGoodsService.java new file mode 100644 index 0000000000..f52f9efde2 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveGoodsService.java @@ -0,0 +1,123 @@ +package cn.binarywang.wx.miniapp.api; + +import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; +import me.chanjar.weixin.common.error.WxErrorException; + +import java.util.List; + +/** + *
+ * 直播间商品相关操作接口
+ * 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/commodity-api.html
+ * Created by lipengjun on 2020/6/29.
+ * 
+ * + * @author lipengjun (939961241@qq.com) + */ +public interface WxMaLiveGoodsService { + String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add"; + String RESET_AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit"; + String AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit"; + String DELETE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete"; + String UPDATE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update"; + String GET_GOODS_WARE_HOUSE = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse"; + String GET_APPROVED_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved"; + + /** + * 商品添加并提审 + *
+   * 调用此接口上传并提审需要直播的商品信息,审核通过后商品录入【小程序直播】商品库
+   * 注意:开发者必须保存【商品ID】与【审核单ID】,如果丢失,则无法调用其他相关接口
+   * 调用额度:500次/一天
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/goods/add?access_token=ACCESS_TOKEN
+   * 
+ * + * @param goods 商品 + * @return 返回auditId、goodsId + * @throws WxErrorException . + */ + WxMaLiveResult addGoods(WxMaLiveInfo.Goods goods) throws WxErrorException; + + /** + * 撤回审核 + *
+   * 调用此接口,可撤回直播商品的提审申请,消耗的提审次数不返还
+   * 调用额度:500次/一天
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit?access_token=ACCESS_TOKEN
+   * 
+   * @param auditId 审核单ID
+   * @param goodsId 商品ID
+   * @return 撤回审核是否成功
+   * @throws WxErrorException .
+   */
+  boolean resetAudit(Integer auditId, Integer goodsId) throws WxErrorException;
+
+  /**
+   * 重新提交审核
+   * 
+   * 调用此接口,可撤回直播商品的提审申请,消耗的提审次数不返还
+   * 调用额度:500次/一天(与接口'商品添加并提审'共用500次限制)
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/goods/audit?access_token=ACCESS_TOKEN
+   * 
+   * @param goodsId 商品ID
+   * @return 审核单ID
+   * @throws WxErrorException .
+   */
+  String auditGoods(Integer goodsId) throws WxErrorException;
+
+  /**
+   * 删除商品
+   * 
+   * 调用此接口,可删除【小程序直播】商品库中的商品,删除后直播间上架的该商品也将被同步删除,不可恢复;
+   * 调用额度:1000次/一天
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/goods/delete?access_token=ACCESS_TOKEN
+   * 
+   * @param goodsId 商品ID
+   * @return 删除商品是否成功
+   * @throws WxErrorException .
+   */
+  boolean deleteGoods(Integer goodsId) throws WxErrorException;
+
+  /**
+   * 更新商品
+   * 
+   * 调用此接口可以更新商品信息,审核通过的商品仅允许更新价格类型与价格,审核中的商品不允许更新,未审核的商品允许更新所有字段, 只传入需要更新的字段。
+   * 调用额度:1000次/一天
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/goods/update?access_token=ACCESS_TOKEN
+   * 
+ * + * @param goods 商品 + * @return 更新商品是否成功 + * @throws WxErrorException . + */ + boolean updateGoods(WxMaLiveInfo.Goods goods) throws WxErrorException; + + /** + * 获取商品状态 + *
+   * 调用此接口可获取商品的信息与审核状态
+   * 调用额度:1000次/一天
+   * http请求方式:POST https://api.weixin.qq.com/wxa/business/getgoodswarehouse?access_token=ACCESS_TOKEN
+   * 
+   * @param goodsIds 商品ID集
+   * @return 商品状态信息
+   * @throws WxErrorException .
+   */
+  WxMaLiveResult getGoodsWareHouse(List goodsIds) throws WxErrorException;
+
+  /**
+   * 获取商品列表
+   * 
+   * 调用此接口可获取商品列表
+   * 调用额度:10000次/一天
+   * http请求方式:GET https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved?access_token=ACCESS_TOKEN
+   * 
+   * @param offset 分页条数起点
+   * @param limit  分页大小,默认30,不超过100
+   * @param status 商品状态,0:未审核。1:审核中,2:审核通过,3:审核驳回
+   * @return 商品列表
+   * @throws WxErrorException .
+   */
+  WxMaLiveResult getApprovedGoods(Integer offset, Integer limit, Integer status) throws WxErrorException;
+}
diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java
index 8a39e2957b..a50a67faeb 100644
--- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java
+++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java
@@ -1,6 +1,6 @@
 package cn.binarywang.wx.miniapp.api;
 
-import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo;
+import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo;
 import me.chanjar.weixin.common.error.WxErrorException;
 
 import java.util.List;
@@ -15,6 +15,22 @@
  */
 public interface WxMaLiveService {
   String GET_LIVE_INFO = "http://api.weixin.qq.com/wxa/business/getliveinfo";
+  String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=%s";
+  String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods";
+
+  /**
+   * 创建直播间
+   * 
+   * 调用此接口创建直播间,创建成功后将在直播间列表展示,调用额度:10000次/一天
+   * 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#1
+   * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=ACCESS_TOKEN
+   * 
+ * + * @param roomInfo 直播间信息 + * @return . + * @throws WxErrorException . + */ + Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorException; /** * 获取直播房间列表.(分页) @@ -24,39 +40,54 @@ public interface WxMaLiveService { * @return . * @throws WxErrorException . */ - WxMaGetLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException; + WxMaLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException; /** * 获取所有直播间信息(没有分页直接获取全部) + * * @return * @throws WxErrorException */ - List getLiveinfos() throws WxErrorException; + List getLiveinfos() throws WxErrorException; /** - * * 获取直播房间回放数据信息. * - * @param action 获取回放 + * @param action 获取回放 * @param room_id 直播间 id - * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 - * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 + * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 + * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 * @return * @throws WxErrorException */ - WxMaGetLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; + WxMaLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; /** - * * 获取直播房间回放数据信息. + *

+ * 获取回放 (默认:get_replay) * - * 获取回放 (默认:get_replay) * @param room_id 直播间 id - * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 - * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 + * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 + * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 * @return * @throws WxErrorException */ - WxMaGetLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; + WxMaLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; + /** + * 直播间导入商品 + *

+ * 调用接口往指定直播间导入已入库的商品 + * 调用频率 + * 调用额度:10000次/一天 + *

+ * http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods?access_token=ACCESS_TOKEN + *

+   * @param roomId 房间ID
+   * @param goodsIds 数组列表,可传入多个,里面填写 商品 ID
+   * @return 导入商品是否成功
+   * @throws WxErrorException .
+   */
+  boolean addGoodsToRoom(Integer roomId, List goodsIds) throws WxErrorException;
 }
diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaMaterialService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaMaterialService.java
new file mode 100644
index 0000000000..7c9b9e21a0
--- /dev/null
+++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaMaterialService.java
@@ -0,0 +1,46 @@
+package cn.binarywang.wx.miniapp.api;
+
+import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
+import me.chanjar.weixin.common.error.WxErrorException;
+
+import java.io.File;
+
+/**
+ * 
+ * 素材管理的相关接口
+ * 文档地址:https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html
+ * Created by lipengjun on 2020/6/29.
+ * 
+ * + * @author lipengjun (939961241@qq.com) + */ +public interface WxMaMaterialService { + String MEDIA_UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?type=%s"; + /** + *
+   * 新增临时素材
+   * 小程序获取临时素材,用于直播间商品
+   * 素材管理接口对所有认证的订阅号和服务号开放。通过本接口,小程序可以新增临时素材(即上传临时多媒体文件),返回的mediaId用于直播间商品使用。
+   * 请注意:
+   *  1、对于临时素材,每个素材(media_id)会在开发者上传或粉丝发送到微信服务器3天后自动删除(所以用户发送给开发者的素材,若开发者需要,应尽快下载到本地),以节省服务器资源。
+   *  2、media_id是可复用的。
+   *  3、素材的格式大小等要求与公众平台官网一致。具体是,图片大小不超过2M,支持png/jpeg/jpg/gif格式,语音大小不超过5M,长度不超过60秒,支持mp3/amr格式
+   *  4、需使用https调用本接口。
+   *  本接口即为原“上传多媒体文件”接口。
+   *  注意事项:
+   *    上传的临时多媒体文件有格式和大小限制,如下:
+   *    图片(image): 2M,支持PNG\JPEG\JPG\GIF格式
+   *    语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
+   *    视频(video):10MB,支持MP4格式
+   *    缩略图(thumb):64KB,支持JPG格式
+   * 媒体文件在后台保存时间为3天,即3天后media_id失效。
+   * 详情请见: 新增临时素材
+   * 接口url格式:https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
+   * 
+ * + * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} + * @param file 文件对象 + * @throws WxErrorException + */ + WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException; +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java index 875d9430d4..caac54388b 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java @@ -280,4 +280,18 @@ public interface WxMaService extends WxService { * @return . */ WxMaLiveService getLiveService(); + + /** + * 获取直播间商品服务对象 + * + * @return . + */ + WxMaLiveGoodsService getLiveGoodsService(); + + /** + * 获取小程序素材管理服务对象 + * + * @return . + */ + WxMaMaterialService getMaterialService(); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java index ee891ecb7b..34581ad804 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java @@ -56,6 +56,8 @@ public abstract class BaseWxMaServiceImpl implements WxMaService, RequestH private final WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this); private final WxMaCloudService cloudService = new WxMaCloudServiceImpl(this); private final WxMaLiveService liveService = new WxMaLiveServiceImpl(this); + private final WxMaLiveGoodsService liveGoodsService = new WxMaLiveGoodsServiceImpl(this); + private final WxMaMaterialService maMaterialService = new WxMaMaterialServiceImpl(this); private int retrySleepMillis = 1000; private int maxRetryTimes = 5; @@ -394,4 +396,14 @@ public WxMaCloudService getCloudService() { public WxMaLiveService getLiveService() { return this.liveService; } + + @Override + public WxMaLiveGoodsService getLiveGoodsService() { + return this.liveGoodsService; + } + + @Override + public WxMaMaterialService getMaterialService() { + return this.maMaterialService; + } } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImpl.java new file mode 100644 index 0000000000..f20e0fc288 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImpl.java @@ -0,0 +1,132 @@ +package cn.binarywang.wx.miniapp.api.impl; + +import cn.binarywang.wx.miniapp.api.WxMaLiveGoodsService; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; +import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; +import com.google.common.base.Joiner; +import com.google.common.collect.ImmutableMap; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import lombok.AllArgsConstructor; +import me.chanjar.weixin.common.WxType; +import me.chanjar.weixin.common.error.WxError; +import me.chanjar.weixin.common.error.WxErrorException; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + *
+ *  Created by lipengjun on 2020/6/29.
+ * 
+ * + * @author lipengjun (939961241@qq.com) + */ +@AllArgsConstructor +public class WxMaLiveGoodsServiceImpl implements WxMaLiveGoodsService { + private static final JsonParser JSON_PARSER = new JsonParser(); + + private WxMaService wxMaService; + + @Override + public WxMaLiveResult addGoods(WxMaLiveInfo.Goods goods) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("goodsInfo", goods); + String responseContent = this.wxMaService.post(ADD_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return WxMaLiveResult.fromJson(jsonObject.toString()); + } + + @Override + public boolean resetAudit(Integer auditId, Integer goodsId) throws WxErrorException { + Map map = new HashMap<>(4); + map.put("auditId", auditId); + map.put("goodsId", goodsId); + String responseContent = this.wxMaService.post(RESET_AUDIT_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return true; + } + + @Override + public String auditGoods(Integer goodsId) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("goodsId", goodsId); + String responseContent = this.wxMaService.post(AUDIT_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return jsonObject.get("auditId").getAsString(); + } + + @Override + public boolean deleteGoods(Integer goodsId) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("goodsId", goodsId); + String responseContent = this.wxMaService.post(DELETE_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return true; + } + + @Override + public boolean updateGoods(WxMaLiveInfo.Goods goods) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("goodsInfo", goods); + String responseContent = this.wxMaService.post(UPDATE_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return true; + } + + @Override + public WxMaLiveResult getGoodsWareHouse(List goodsIds) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("goods_ids", goodsIds); + String responseContent = this.wxMaService.post(GET_GOODS_WARE_HOUSE, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return WxMaLiveResult.fromJson(jsonObject.toString()); + } + + @Override + public WxMaLiveResult getApprovedGoods(Integer offset, Integer limit, Integer status) throws WxErrorException { + ImmutableMap params = ImmutableMap.of("status", status, "offset", offset, "limit", limit); + String responseContent = wxMaService.get(GET_APPROVED_GOODS, Joiner.on("&").withKeyValueSeparator("=").join(params)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + JsonArray goodsArr = jsonObject.getAsJsonArray("goods"); + if (goodsArr.size() > 0) { + for (int i = 0; i < goodsArr.size(); i++) { + // 接口返回key是驼峰 + JsonObject goods = (JsonObject) goodsArr.get(i); + goods.addProperty("goods_id", goods.get("goodsId").getAsInt()); + goods.addProperty("cover_img_url", goods.get("coverImgUrl").getAsString()); + goods.addProperty("price_type", goods.get("priceType").getAsInt()); + goods.addProperty("third_party_tag", goods.get("thirdPartyTag").getAsInt()); + goods.addProperty("audit_status", status); + } + } + return WxMaLiveResult.fromJson(jsonObject.toString()); + } + +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java index 6f1a154bb1..9987fad834 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java @@ -2,7 +2,7 @@ import cn.binarywang.wx.miniapp.api.WxMaLiveService; import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -10,6 +10,8 @@ import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; @@ -25,22 +27,36 @@ */ @AllArgsConstructor public class WxMaLiveServiceImpl implements WxMaLiveService { + final Logger log = LoggerFactory.getLogger(this.getClass()); private static final JsonParser JSON_PARSER = new JsonParser(); - private WxMaService service; + private WxMaService wxMaService; @Override - public WxMaGetLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException { + public Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorException { + // fixme 请求头需要设置 content-type:application/json;charset=utf-8 + // 问题:https://developers.weixin.qq.com/community/develop/doc/000a84e22d434002317afed845b800?highLine=%25E5%2588%259B%25E5%25BB%25BA%25E7%259B%25B4%25E6%2592%25AD%25E9%2597%25B4%25E6%258E%25A5%25E5%258F%25A3 + + String responseContent = this.wxMaService.post(CREATE_ROOM, WxMaGsonBuilder.create().toJson(roomInfo)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return jsonObject.get("roomId").getAsInt(); + } + + @Override + public WxMaLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException { JsonObject jsonObject = getJsonObject(start, limit, null); - return WxMaGetLiveInfo.fromJson(jsonObject.toString()); + return WxMaLiveInfo.fromJson(jsonObject.toString()); } @Override - public List getLiveinfos() throws WxErrorException { - List results = new ArrayList<>(); + public List getLiveinfos() throws WxErrorException { + List results = new ArrayList<>(); Integer start = 0; Integer limit = 80; Integer tatal = 0; - WxMaGetLiveInfo liveInfo = null; + WxMaLiveInfo liveInfo = null; do { if (tatal != 0 && tatal <= start) { break; @@ -62,19 +78,32 @@ public List getLiveinfos() throws WxErrorException { } @Override - public WxMaGetLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { + public WxMaLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { Map map = new HashMap(4); map.put("action", action); map.put("room_id", room_id); JsonObject jsonObject = getJsonObject(start, limit, map); - return WxMaGetLiveInfo.fromJson(jsonObject.toString()); + return WxMaLiveInfo.fromJson(jsonObject.toString()); } @Override - public WxMaGetLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { + public WxMaLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { return getLiveReplay("get_replay", room_id, start, limit); } + @Override + public boolean addGoodsToRoom(Integer roomId, List goodsIds) throws WxErrorException { + Map map = new HashMap<>(2); + map.put("roomId", roomId); + map.put("ids", goodsIds); + String responseContent = this.wxMaService.post(ADD_GOODS, WxMaGsonBuilder.create().toJson(map)); + JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); + if (jsonObject.get("errcode").getAsInt() != 0) { + throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); + } + return true; + } + /** * 包装一下 * @@ -90,7 +119,7 @@ private JsonObject getJsonObject(Integer start, Integer limit, Map + * Created by lipengjun on 2020/6/29. + *
+ * + * @author lipengjun (939961241@qq.com) + */ +@RequiredArgsConstructor +public class WxMaMaterialServiceImpl implements WxMaMaterialService { + private final WxMaService wxMaService; + + @Override + public WxMediaUploadResult mediaUpload(String mediaType, File file) throws WxErrorException { + String url = String.format(MEDIA_UPLOAD_URL, mediaType); + return this.wxMaService.execute(MediaUploadRequestExecutor.create(this.wxMaService.getRequestHttp()), url, file); + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaGetLiveInfo.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java similarity index 71% rename from weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaGetLiveInfo.java rename to weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java index 77817d3cec..81f730c0f3 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaGetLiveInfo.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java @@ -14,7 +14,7 @@ * @date 2020/4/5 */ @Data -public class WxMaGetLiveInfo implements Serializable { +public class WxMaLiveInfo implements Serializable { private static final long serialVersionUID = 7285263767524755887L; private Integer errcode; private String errmsg; @@ -30,8 +30,8 @@ public class WxMaGetLiveInfo implements Serializable { @SerializedName("live_replay") private List liveReplay; - public static WxMaGetLiveInfo fromJson(String json) { - return WxMaGsonBuilder.create().fromJson(json, WxMaGetLiveInfo.class); + public static WxMaLiveInfo fromJson(String json) { + return WxMaGsonBuilder.create().fromJson(json, WxMaLiveInfo.class); } /** @@ -54,8 +54,19 @@ public static class RoomInfo implements Serializable { private Long endTime; @SerializedName("anchor_name") private String anchorName; + @SerializedName("anchor_wechat") + private String anchorWechat; @SerializedName("anchor_img") private String anchorImg; + private Integer type; + @SerializedName("screen_type") + private Integer screenType; + @SerializedName("close_like") + private Integer closeLike; + @SerializedName("closeGoods") + private Integer closeGoods; + @SerializedName("close_comment") + private Integer closeComment; private List goods; } @@ -65,11 +76,17 @@ public static class RoomInfo implements Serializable { @Data public static class Goods implements Serializable { private static final long serialVersionUID = 5769245932149287574L; - @SerializedName("cover_img") - private String coverImg; + private Integer goodsId; + private String coverImgUrl; private String url; + private Integer priceType; private String price; + private String price2; private String name; + /** + * 1, 2:表示是为api添加商品,否则是在MP添加商品 + */ + private String thirdPartyTag; } /** diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java new file mode 100644 index 0000000000..7665df93de --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java @@ -0,0 +1,59 @@ +package cn.binarywang.wx.miniapp.bean; + +import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +/** + *
+ * 直播间操作返回结果
+ * Created by lipengjun on 2020/6/29.
+ * 
+ * + * @author lipengjun (939961241@qq.com) + */ +@Data +public class WxMaLiveResult implements Serializable { + private static final long serialVersionUID = 1L; + private Integer errcode; + private String errmsg; + private Integer total; + private Integer auditId; + private Integer goodsId; + private List goods; + + public static WxMaLiveResult fromJson(String json) { + return WxMaGsonBuilder.create().fromJson(json, WxMaLiveResult.class); + } + + /** + * 商品列表 + */ + @Data + public static class Goods implements Serializable { + private static final long serialVersionUID = 5769245932149287574L; + @SerializedName("goods_id") + private Integer goodsId; + @SerializedName("cover_img_url") + private String coverImgUrl; + private String name; + private String url; + @SerializedName("price_type") + private Integer priceType; + /** + * 0:未审核,1:审核中,2:审核通过,3审核失败 + */ + @SerializedName("audit_status") + private Integer auditStatus; + private String price; + private String price2; + /** + * 1, 2:表示是为api添加商品,否则是在MP添加商品 + */ + @SerializedName("third_party_tag") + private String thirdPartyTag; + } +} diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImplTest.java new file mode 100644 index 0000000000..25e92efdcd --- /dev/null +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImplTest.java @@ -0,0 +1,92 @@ +package cn.binarywang.wx.miniapp.api.impl; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; +import cn.binarywang.wx.miniapp.test.ApiTestModule; +import com.google.inject.Inject; +import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import java.io.File; +import java.util.Arrays; + +import static org.testng.Assert.assertNotNull; + +/** + * 测试直播商品管理相关的接口 + * + * @author lipengjun (939961241@qq.com) + */ +@Test +@Guice(modules = ApiTestModule.class) +public class WxMaLiveGoodsServiceImplTest { + + @Inject + private WxMaService wxService; + + @Test + public void addGoods() throws Exception { + //上传临时素材 + WxMediaUploadResult mediaUpload = this.wxService.getMaterialService().mediaUpload("image", new File("E:\\1.png")); + + WxMaLiveInfo.Goods goods = new WxMaLiveInfo.Goods(); + goods.setCoverImgUrl(mediaUpload.getMediaId()); + goods.setName("宫廷奢华真丝四件套"); + goods.setPrice("1599"); + goods.setPrice2("0"); + goods.setPriceType(1); + goods.setUrl("pages/goods/goods?id=b7c4fbf95493bd294054fe4296d0d9ad"); + WxMaLiveResult liveResult = this.wxService.getLiveGoodsService().addGoods(goods); + assertNotNull(liveResult); + System.out.println(liveResult.toString()); + } + + @Test + public void resetAudit() throws Exception { + boolean result = this.wxService.getLiveGoodsService().resetAudit(715138516, 9); + System.out.println(result); + } + + @Test + public void auditGoods() throws Exception { + String result = this.wxService.getLiveGoodsService().auditGoods(9); + System.out.println(result); + } + + @Test + public void deleteGoods() throws Exception { + boolean result = this.wxService.getLiveGoodsService().deleteGoods(9); + System.out.println(result); + } + + @Test + public void updateGoods() throws Exception { + + WxMaLiveInfo.Goods goods = new WxMaLiveInfo.Goods(); + goods.setGoodsId(8); + goods.setName("宫廷奢华真丝四件套"); + goods.setCoverImgUrl("http://mmbiz.qpic.cn/mmbiz_png/omYktZNGamuUQE0WPVfqdnLV61JDhluXOac7PiaoZeticFpcR7wvicC0aXUC2VXkl7r1gN0QSKosv2satn6oCFeiaQ/0"); + goods.setPrice("2299"); + goods.setPrice2("0"); + goods.setPriceType(1); + goods.setUrl("pages/goods/goods?id=b7c4fbf95493bd294054fe4296d0d9ad"); + boolean maLiveInfo = this.wxService.getLiveGoodsService().updateGoods(goods); + System.out.println(maLiveInfo); + } + + @Test + public void getGoodsWareHouse() throws Exception { + WxMaLiveResult liveResult = this.wxService.getLiveGoodsService().getGoodsWareHouse(Arrays.asList(1, 2)); + assertNotNull(liveResult); + System.out.println(liveResult.toString()); + } + + @Test + public void getApprovedGoods() throws Exception { + WxMaLiveResult liveResult = this.wxService.getLiveGoodsService().getApprovedGoods(0, 4, 2); + assertNotNull(liveResult); + System.out.println(liveResult.toString()); + } +} diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java index 5ccd16a037..2982a30d4a 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java @@ -1,23 +1,19 @@ package cn.binarywang.wx.miniapp.api.impl; import cn.binarywang.wx.miniapp.api.WxMaService; -import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo; -import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; -import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; import cn.binarywang.wx.miniapp.test.ApiTestModule; -import cn.binarywang.wx.miniapp.test.TestConfig; -import com.google.common.collect.ImmutableMap; -import com.google.gson.JsonObject; import com.google.inject.Inject; -import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import org.testng.annotations.Guice; import org.testng.annotations.Test; +import java.io.File; +import java.util.Arrays; +import java.util.Calendar; import java.util.List; -import java.util.stream.Collectors; import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; /** * 测试直播相关的接口 @@ -31,9 +27,34 @@ public class WxMaLiveServiceImplTest { @Inject private WxMaService wxService; + @Test + public void createRoom() throws Exception { + //上传临时素材 + WxMediaUploadResult mediaUpload = this.wxService.getMaterialService().mediaUpload("image", new File("E:\\1.png")); + + WxMaLiveInfo.RoomInfo roomInfo = new WxMaLiveInfo.RoomInfo(); + roomInfo.setName("订阅通知直播间"); + roomInfo.setCoverImg(mediaUpload.getMediaId()); + Calendar c = Calendar.getInstance(); + c.set(2020, Calendar.SEPTEMBER, 10, 8, 0); + roomInfo.setStartTime(c.getTimeInMillis() / 1000); + c.set(2020, Calendar.SEPTEMBER, 10, 12, 0); + roomInfo.setEndTime(c.getTimeInMillis() / 1000); + roomInfo.setAnchorName("鹏军_专业小程序开发"); + roomInfo.setAnchorWechat("pengjun939961241"); + roomInfo.setShareImg(mediaUpload.getMediaId()); + roomInfo.setType(1); + roomInfo.setScreenType(1); + roomInfo.setCloseLike(0); + roomInfo.setCloseGoods(0); + roomInfo.setCloseComment(0); + Integer roomId = this.wxService.getLiveService().createRoom(roomInfo); + System.out.println(roomId); + } + @Test public void getLiveInfo() throws Exception { - WxMaGetLiveInfo list = this.wxService.getLiveService().getLiveInfo(0,10); + WxMaLiveInfo list = this.wxService.getLiveService().getLiveInfo(0, 10); assertNotNull(list); System.out.println(list.toString()); } @@ -41,15 +62,21 @@ public void getLiveInfo() throws Exception { @Test public void getLiveReplay() throws Exception { // [12, 11, 10, 9, 8, 7, 6, 5, 3, 2] - WxMaGetLiveInfo list = this.wxService.getLiveService().getLiveReplay(11,0,10); + WxMaLiveInfo list = this.wxService.getLiveService().getLiveReplay(3, 0, 10); assertNotNull(list); System.out.println(list.toString()); } @Test public void getLiveinfos() throws Exception { - List list = this.wxService.getLiveService().getLiveinfos(); + List list = this.wxService.getLiveService().getLiveinfos(); assertNotNull(list); System.out.println(list.toString()); } + + @Test + public void addGoodsToRoom() throws Exception { + boolean result = this.wxService.getLiveService().addGoodsToRoom(5, Arrays.asList(8, 7, 5, 4, 10)); + System.out.println(result); + } } From 832ac31696af20601be76ed7bc645fbc674ce091 Mon Sep 17 00:00:00 2001 From: lipengjun Date: Tue, 30 Jun 2020 15:58:37 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E9=97=B4=E5=95=86=E5=93=81=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java index a50a67faeb..9b820e7580 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java @@ -15,7 +15,7 @@ */ public interface WxMaLiveService { String GET_LIVE_INFO = "http://api.weixin.qq.com/wxa/business/getliveinfo"; - String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create?access_token=%s"; + String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create"; String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods"; /** From f4fb1e51c20692e7ec8efbb9d5e164fbfa35ce08 Mon Sep 17 00:00:00 2001 From: lipengjun Date: Tue, 30 Jun 2020 16:27:27 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E5=85=A5=E5=8F=82=E3=80=81?= =?UTF-8?q?=E5=87=BA=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaLiveService.java | 9 +-- .../miniapp/api/impl/WxMaLiveServiceImpl.java | 23 +++---- .../wx/miniapp/bean/WxMaLiveInfo.java | 49 +-------------- .../wx/miniapp/bean/WxMaLiveResult.java | 62 +++++++++++++++++++ .../api/impl/WxMaLiveServiceImplTest.java | 7 ++- 5 files changed, 81 insertions(+), 69 deletions(-) diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java index 9b820e7580..4ea0ac0e0b 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java @@ -1,6 +1,7 @@ package cn.binarywang.wx.miniapp.api; import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; import me.chanjar.weixin.common.error.WxErrorException; import java.util.List; @@ -40,7 +41,7 @@ public interface WxMaLiveService { * @return . * @throws WxErrorException . */ - WxMaLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException; + WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException; /** * 获取所有直播间信息(没有分页直接获取全部) @@ -48,7 +49,7 @@ public interface WxMaLiveService { * @return * @throws WxErrorException */ - List getLiveinfos() throws WxErrorException; + List getLiveinfos() throws WxErrorException; /** * 获取直播房间回放数据信息. @@ -60,7 +61,7 @@ public interface WxMaLiveService { * @return * @throws WxErrorException */ - WxMaLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; + WxMaLiveResult getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; /** * 获取直播房间回放数据信息. @@ -73,7 +74,7 @@ public interface WxMaLiveService { * @return * @throws WxErrorException */ - WxMaLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; + WxMaLiveResult getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; /** * 直播间导入商品 diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java index 9987fad834..41c7d60878 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java @@ -3,6 +3,7 @@ import cn.binarywang.wx.miniapp.api.WxMaLiveService; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -10,8 +11,6 @@ import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxErrorException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; @@ -27,15 +26,11 @@ */ @AllArgsConstructor public class WxMaLiveServiceImpl implements WxMaLiveService { - final Logger log = LoggerFactory.getLogger(this.getClass()); private static final JsonParser JSON_PARSER = new JsonParser(); private WxMaService wxMaService; @Override public Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorException { - // fixme 请求头需要设置 content-type:application/json;charset=utf-8 - // 问题:https://developers.weixin.qq.com/community/develop/doc/000a84e22d434002317afed845b800?highLine=%25E5%2588%259B%25E5%25BB%25BA%25E7%259B%25B4%25E6%2592%25AD%25E9%2597%25B4%25E6%258E%25A5%25E5%258F%25A3 - String responseContent = this.wxMaService.post(CREATE_ROOM, WxMaGsonBuilder.create().toJson(roomInfo)); JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); if (jsonObject.get("errcode").getAsInt() != 0) { @@ -45,18 +40,18 @@ public Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorExceptio } @Override - public WxMaLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException { + public WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException { JsonObject jsonObject = getJsonObject(start, limit, null); - return WxMaLiveInfo.fromJson(jsonObject.toString()); + return WxMaLiveResult.fromJson(jsonObject.toString()); } @Override - public List getLiveinfos() throws WxErrorException { - List results = new ArrayList<>(); + public List getLiveinfos() throws WxErrorException { + List results = new ArrayList<>(); Integer start = 0; Integer limit = 80; Integer tatal = 0; - WxMaLiveInfo liveInfo = null; + WxMaLiveResult liveInfo = null; do { if (tatal != 0 && tatal <= start) { break; @@ -78,16 +73,16 @@ public List getLiveinfos() throws WxErrorException { } @Override - public WxMaLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { + public WxMaLiveResult getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { Map map = new HashMap(4); map.put("action", action); map.put("room_id", room_id); JsonObject jsonObject = getJsonObject(start, limit, map); - return WxMaLiveInfo.fromJson(jsonObject.toString()); + return WxMaLiveResult.fromJson(jsonObject.toString()); } @Override - public WxMaLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { + public WxMaLiveResult getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { return getLiveReplay("get_replay", room_id, start, limit); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java index 81f730c0f3..8a98b4a218 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveInfo.java @@ -1,14 +1,12 @@ package cn.binarywang.wx.miniapp.bean; -import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; -import com.google.gson.annotations.SerializedName; import lombok.Data; import java.io.Serializable; import java.util.List; /** - * 获取直播房间列表 + * 直播接口入参 * * @author yjwang * @date 2020/4/5 @@ -16,23 +14,6 @@ @Data public class WxMaLiveInfo implements Serializable { private static final long serialVersionUID = 7285263767524755887L; - private Integer errcode; - private String errmsg; - private Integer total; - /** - * 直播间列表 - */ - @SerializedName("room_info") - private List roomInfos; - /** - * 获取回放源视频列表 - */ - @SerializedName("live_replay") - private List liveReplay; - - public static WxMaLiveInfo fromJson(String json) { - return WxMaGsonBuilder.create().fromJson(json, WxMaLiveInfo.class); - } /** * 直播列表 @@ -42,30 +23,18 @@ public static class RoomInfo implements Serializable { private static final long serialVersionUID = 7745775280267417154L; private String name; private Integer roomid; - @SerializedName("cover_img") private String coverImg; - @SerializedName("share_img") private String shareImg; - @SerializedName("live_status") private Integer liveStatus; - @SerializedName("start_time") private Long startTime; - @SerializedName("end_time") private Long endTime; - @SerializedName("anchor_name") private String anchorName; - @SerializedName("anchor_wechat") private String anchorWechat; - @SerializedName("anchor_img") private String anchorImg; private Integer type; - @SerializedName("screen_type") private Integer screenType; - @SerializedName("close_like") private Integer closeLike; - @SerializedName("closeGoods") private Integer closeGoods; - @SerializedName("close_comment") private Integer closeComment; private List goods; } @@ -88,20 +57,4 @@ public static class Goods implements Serializable { */ private String thirdPartyTag; } - - /** - * 回放数据列表 - */ - @Data - public static class LiveReplay implements Serializable { - private static final long serialVersionUID = 7683927205627536320L; - @SerializedName("expire_time") - private String expireTime; - @SerializedName("create_time") - private String createTime; - @SerializedName("media_url") - private String mediaUrl; - } - - } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java index 7665df93de..2b0407e6aa 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaLiveResult.java @@ -25,6 +25,18 @@ public class WxMaLiveResult implements Serializable { private Integer goodsId; private List goods; + /** + * 直播间列表 + */ + @SerializedName("room_info") + private List roomInfos; + + /** + * 获取回放源视频列表 + */ + @SerializedName("live_replay") + private List liveReplay; + public static WxMaLiveResult fromJson(String json) { return WxMaGsonBuilder.create().fromJson(json, WxMaLiveResult.class); } @@ -56,4 +68,54 @@ public static class Goods implements Serializable { @SerializedName("third_party_tag") private String thirdPartyTag; } + + /** + * 直播列表 + */ + @Data + public static class RoomInfo implements Serializable { + private static final long serialVersionUID = 7745775280267417154L; + private String name; + private Integer roomid; + @SerializedName("cover_img") + private String coverImg; + @SerializedName("share_img") + private String shareImg; + @SerializedName("live_status") + private Integer liveStatus; + @SerializedName("start_time") + private Long startTime; + @SerializedName("end_time") + private Long endTime; + @SerializedName("anchor_name") + private String anchorName; + @SerializedName("anchor_wechat") + private String anchorWechat; + @SerializedName("anchor_img") + private String anchorImg; + private Integer type; + @SerializedName("screen_type") + private Integer screenType; + @SerializedName("close_like") + private Integer closeLike; + @SerializedName("closeGoods") + private Integer closeGoods; + @SerializedName("close_comment") + private Integer closeComment; + private List goods; + } + + /** + * 回放数据列表 + */ + @Data + public static class LiveReplay implements Serializable { + private static final long serialVersionUID = 7683927205627536320L; + @SerializedName("expire_time") + private String expireTime; + @SerializedName("create_time") + private String createTime; + @SerializedName("media_url") + private String mediaUrl; + } } diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java index 2982a30d4a..b6e5c9be64 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java @@ -2,6 +2,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.bean.WxMaLiveInfo; +import cn.binarywang.wx.miniapp.bean.WxMaLiveResult; import cn.binarywang.wx.miniapp.test.ApiTestModule; import com.google.inject.Inject; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; @@ -54,7 +55,7 @@ public void createRoom() throws Exception { @Test public void getLiveInfo() throws Exception { - WxMaLiveInfo list = this.wxService.getLiveService().getLiveInfo(0, 10); + WxMaLiveResult list = this.wxService.getLiveService().getLiveInfo(0, 10); assertNotNull(list); System.out.println(list.toString()); } @@ -62,14 +63,14 @@ public void getLiveInfo() throws Exception { @Test public void getLiveReplay() throws Exception { // [12, 11, 10, 9, 8, 7, 6, 5, 3, 2] - WxMaLiveInfo list = this.wxService.getLiveService().getLiveReplay(3, 0, 10); + WxMaLiveResult list = this.wxService.getLiveService().getLiveReplay(3, 0, 10); assertNotNull(list); System.out.println(list.toString()); } @Test public void getLiveinfos() throws Exception { - List list = this.wxService.getLiveService().getLiveinfos(); + List list = this.wxService.getLiveService().getLiveinfos(); assertNotNull(list); System.out.println(list.toString()); }