Client.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020/7/28
  7. *
  8. *
  9. */
  10. namespace crmeb\services\easywechat\broadcast;
  11. use EasyWeChat\Core\Exceptions\HttpException;
  12. use EasyWeChat\MiniProgram\Core\AbstractMiniProgram;
  13. /**
  14. * Class Client.
  15. *
  16. * @author Abbotton <uctoo@foxmail.com>
  17. */
  18. class Client extends AbstractMiniProgram
  19. {
  20. const MSG_CODE = [
  21. '1' => '未创建直播间',
  22. '1003' => '商品id不存在',
  23. '47001' => '入参格式不符合规范',
  24. '200002' => '入参错误',
  25. '300001' => '禁止创建/更新商品 或 禁止编辑&更新房间',
  26. '300002' => '名称长度不符合规则',
  27. '300006' => '图片上传失败',
  28. '300022' => '此房间号不存在',
  29. '300023' => '房间状态 拦截',
  30. '300024' => '商品不存在',
  31. '300025' => '商品审核未通过',
  32. '300026' => '房间商品数量已经满额',
  33. '300027' => '导入商品失败',
  34. '300028' => '房间名称违规',
  35. '300029' => '主播昵称违规',
  36. '300030' => '主播微信号不合法',
  37. '300031' => '直播间封面图不合规',
  38. '300032' => '直播间分享图违规',
  39. '300033' => '添加商品超过直播间上限',
  40. '300034' => '主播微信昵称长度不符合要求',
  41. '300035' => '主播微信号不存在',
  42. '300003' => '价格输入不合规',
  43. '300004' => '商品名称存在违规违法内容',
  44. '300005' => '商品图片存在违规违法内容',
  45. '300007' => '线上小程序版本不存在该链接',
  46. '300008' => '添加商品失败',
  47. '300009' => '商品审核撤回失败',
  48. '300010' => '商品审核状态不对',
  49. '300011' => '操作非法',
  50. '300012' => '没有提审额度',
  51. '300013' => '提审失败',
  52. '300014' => '审核中,无法删除',
  53. '300017' => '商品未提审',
  54. '300018' => '图片尺寸不符合要求',
  55. '300021' => '商品添加成功,审核失败',
  56. '300036' => '请先在微信直播小程序中实名认证',
  57. '-1' => '系统错误',
  58. ];
  59. const API = 'https://api.weixin.qq.com/';
  60. /**
  61. * @param $api
  62. * @param $params
  63. * @return \EasyWeChat\Support\Collection|null
  64. * @throws \EasyWeChat\Core\Exceptions\HttpException
  65. */
  66. protected function httpPostJson($api, $params)
  67. {
  68. try {
  69. return $this->parseJSON('json', [self::API . $api, $params]);
  70. } catch (HttpException $e) {
  71. $code = $e->getCode();
  72. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  73. }
  74. }
  75. /**
  76. * @param $api
  77. * @param $params
  78. * @return \EasyWeChat\Support\Collection|null
  79. * @throws \EasyWeChat\Core\Exceptions\HttpException
  80. */
  81. protected function httpPost($api, $params)
  82. {
  83. try {
  84. return $this->parseJSON('post', [self::API . $api, $params]);
  85. } catch (HttpException $e) {
  86. $code = $e->getCode();
  87. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  88. }
  89. }
  90. /**
  91. * @param $api
  92. * @param $params
  93. * @return \EasyWeChat\Support\Collection|null
  94. * @throws \EasyWeChat\Core\Exceptions\HttpException
  95. */
  96. protected function httpGet($api, $params)
  97. {
  98. try {
  99. return $this->parseJSON('get', [self::API . $api, $params]);
  100. } catch (HttpException $e) {
  101. $code = $e->getCode();
  102. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  103. }
  104. }
  105. /**
  106. * Add broadcast goods.
  107. *
  108. * @param array $goodsInfo
  109. * @return \EasyWeChat\Support\Collection|null
  110. * @throws \EasyWeChat\Core\Exceptions\HttpException
  111. */
  112. public function create(array $goodsInfo)
  113. {
  114. $params = [
  115. 'goodsInfo' => $goodsInfo,
  116. ];
  117. return $this->httpPostJson('wxaapi/broadcast/goods/add', $params);
  118. }
  119. /**
  120. * Reset audit.
  121. *
  122. * @param int $auditId
  123. * @param int $goodsId
  124. * @return \EasyWeChat\Support\Collection|null
  125. * @throws \EasyWeChat\Core\Exceptions\HttpException
  126. */
  127. public function resetAudit(int $auditId, int $goodsId)
  128. {
  129. $params = [
  130. 'auditId' => $auditId,
  131. 'goodsId' => $goodsId,
  132. ];
  133. return $this->httpPostJson('wxaapi/broadcast/goods/resetaudit', $params);
  134. }
  135. /**
  136. * Resubmit audit goods.
  137. *
  138. * @param int $goodsId
  139. * @return \EasyWeChat\Support\Collection|null
  140. * @throws \EasyWeChat\Core\Exceptions\HttpException
  141. */
  142. public function resubmitAudit(int $goodsId)
  143. {
  144. $params = [
  145. 'goodsId' => $goodsId,
  146. ];
  147. return $this->httpPostJson('wxaapi/broadcast/goods/audit', $params);
  148. }
  149. /**
  150. * Delete broadcast goods.
  151. *
  152. * @param int $goodsId
  153. * @return \EasyWeChat\Support\Collection|null
  154. * @throws \EasyWeChat\Core\Exceptions\HttpException
  155. */
  156. public function delete(int $goodsId)
  157. {
  158. $params = [
  159. 'goodsId' => $goodsId,
  160. ];
  161. return $this->httpPostJson('wxaapi/broadcast/goods/delete', $params);
  162. }
  163. /**
  164. * Update goods info.
  165. *
  166. * @param array $goodsInfo
  167. * @return \EasyWeChat\Support\Collection|null
  168. * @throws \EasyWeChat\Core\Exceptions\HttpException
  169. */
  170. public function update(array $goodsInfo)
  171. {
  172. $params = [
  173. 'goodsInfo' => $goodsInfo,
  174. ];
  175. return $this->httpPostJson('wxaapi/broadcast/goods/update', $params);
  176. }
  177. /**
  178. * Get goods information and review status.
  179. *
  180. * @param array $goodsIdArray
  181. * @return \EasyWeChat\Support\Collection|null
  182. * @throws \EasyWeChat\Core\Exceptions\HttpException
  183. */
  184. public function getGoodsWarehouse(array $goodsIdArray)
  185. {
  186. $params = [
  187. 'goods_ids' => $goodsIdArray,
  188. ];
  189. return $this->httpPostJson('wxa/business/getgoodswarehouse', $params);
  190. }
  191. /**
  192. * Get goods list based on status
  193. *
  194. * @param array $params
  195. * @return \EasyWeChat\Support\Collection|null
  196. * @throws \EasyWeChat\Core\Exceptions\HttpException
  197. */
  198. public function getApproved(array $params)
  199. {
  200. return $this->httpGet('wxaapi/broadcast/goods/getapproved', $params);
  201. }
  202. /**
  203. * Add goods to the designated live room.
  204. *
  205. * @param array $params
  206. * @return \EasyWeChat\Support\Collection|null
  207. * @throws \EasyWeChat\Core\Exceptions\HttpException
  208. */
  209. public function addGoods(array $params)
  210. {
  211. return $this->httpPost('wxaapi/broadcast/room/addgoods', $params);
  212. }
  213. /**
  214. * Get Room List.
  215. *
  216. * @param int $start
  217. * @param int $limit
  218. * @return \EasyWeChat\Support\Collection|null
  219. * @throws \EasyWeChat\Core\Exceptions\HttpException
  220. */
  221. public function getRooms(int $start = 0, int $limit = 10)
  222. {
  223. $params = [
  224. 'start' => $start,
  225. 'limit' => $limit,
  226. ];
  227. return $this->httpPostJson('wxa/business/getliveinfo', $params);
  228. }
  229. /**
  230. * Get Playback List.
  231. *
  232. * @param int $roomId
  233. * @param int $start
  234. * @param int $limit
  235. * @return \EasyWeChat\Support\Collection|null
  236. * @throws \EasyWeChat\Core\Exceptions\HttpException
  237. */
  238. public function getPlaybacks(int $roomId, int $start = 0, int $limit = 10)
  239. {
  240. $params = [
  241. 'action' => 'get_replay',
  242. 'room_id' => $roomId,
  243. 'start' => $start,
  244. 'limit' => $limit,
  245. ];
  246. return $this->httpPostJson('wxa/business/getliveinfo', $params);
  247. }
  248. /**
  249. * Create a live room.
  250. *
  251. * @param array $params
  252. * @return \EasyWeChat\Support\Collection|null
  253. * @throws \EasyWeChat\Core\Exceptions\HttpException
  254. */
  255. public function createLiveRoom(array $params)
  256. {
  257. return $this->httpPostJson('wxaapi/broadcast/room/create', $params);
  258. }
  259. }