Client.php 8.6 KB

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