Client.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. '300038' => '请先在小程序后台配置直播客服',
  59. '-1' => '系统错误',
  60. ];
  61. const API = 'https://api.weixin.qq.com/';
  62. /**
  63. * @param $api
  64. * @param $params
  65. * @return \EasyWeChat\Support\Collection|null
  66. * @throws \EasyWeChat\Core\Exceptions\HttpException
  67. */
  68. protected function httpPostJson($api, $params)
  69. {
  70. try {
  71. return $this->parseJSON('json', [self::API . $api, $params]);
  72. } catch (HttpException $e) {
  73. $code = $e->getCode();
  74. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  75. }
  76. }
  77. /**
  78. * @param $api
  79. * @param $params
  80. * @return \EasyWeChat\Support\Collection|null
  81. * @throws \EasyWeChat\Core\Exceptions\HttpException
  82. */
  83. protected function httpPost($api, $params)
  84. {
  85. try {
  86. return $this->parseJSON('post', [self::API . $api, $params]);
  87. } catch (HttpException $e) {
  88. $code = $e->getCode();
  89. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  90. }
  91. }
  92. /**
  93. * @param $api
  94. * @param $params
  95. * @return \EasyWeChat\Support\Collection|null
  96. * @throws \EasyWeChat\Core\Exceptions\HttpException
  97. */
  98. protected function httpGet($api, $params)
  99. {
  100. try {
  101. return $this->parseJSON('get', [self::API . $api, $params]);
  102. } catch (HttpException $e) {
  103. $code = $e->getCode();
  104. throw new HttpException("接口异常[$code]" . (self::MSG_CODE[$code] ?? $e->getMessage()), $code);
  105. }
  106. }
  107. /**
  108. * Add broadcast goods.
  109. *
  110. * @param array $goodsInfo
  111. * @return \EasyWeChat\Support\Collection|null
  112. * @throws \EasyWeChat\Core\Exceptions\HttpException
  113. */
  114. public function create(array $goodsInfo)
  115. {
  116. $params = [
  117. 'goodsInfo' => $goodsInfo,
  118. ];
  119. return $this->httpPostJson('wxaapi/broadcast/goods/add', $params);
  120. }
  121. /**
  122. * Reset audit.
  123. *
  124. * @param int $auditId
  125. * @param int $goodsId
  126. * @return \EasyWeChat\Support\Collection|null
  127. * @throws \EasyWeChat\Core\Exceptions\HttpException
  128. */
  129. public function resetAudit(int $auditId, int $goodsId)
  130. {
  131. $params = [
  132. 'auditId' => $auditId,
  133. 'goodsId' => $goodsId,
  134. ];
  135. return $this->httpPostJson('wxaapi/broadcast/goods/resetaudit', $params);
  136. }
  137. /**
  138. * Resubmit audit goods.
  139. *
  140. * @param int $goodsId
  141. * @return \EasyWeChat\Support\Collection|null
  142. * @throws \EasyWeChat\Core\Exceptions\HttpException
  143. */
  144. public function resubmitAudit(int $goodsId)
  145. {
  146. $params = [
  147. 'goodsId' => $goodsId,
  148. ];
  149. return $this->httpPostJson('wxaapi/broadcast/goods/audit', $params);
  150. }
  151. /**
  152. * Delete broadcast goods.
  153. *
  154. * @param int $goodsId
  155. * @return \EasyWeChat\Support\Collection|null
  156. * @throws \EasyWeChat\Core\Exceptions\HttpException
  157. */
  158. public function delete(int $goodsId)
  159. {
  160. $params = [
  161. 'goodsId' => $goodsId,
  162. ];
  163. try{
  164. return $this->httpPostJson('wxaapi/broadcast/goods/delete', $params);
  165. } catch (HttpException $exception) {
  166. if ($exception->getCode() == 300015) return ;
  167. }
  168. }
  169. /**
  170. * Update goods info.
  171. *
  172. * @param array $goodsInfo
  173. * @return \EasyWeChat\Support\Collection|null
  174. * @throws \EasyWeChat\Core\Exceptions\HttpException
  175. */
  176. public function update(array $goodsInfo)
  177. {
  178. $params = [
  179. 'goodsInfo' => $goodsInfo,
  180. ];
  181. return $this->httpPostJson('wxaapi/broadcast/goods/update', $params);
  182. }
  183. /**
  184. * Get goods information and review status.
  185. *
  186. * @param array $goodsIdArray
  187. * @return \EasyWeChat\Support\Collection|null
  188. * @throws \EasyWeChat\Core\Exceptions\HttpException
  189. */
  190. public function getGoodsWarehouse(array $goodsIdArray)
  191. {
  192. $params = [
  193. 'goods_ids' => $goodsIdArray,
  194. ];
  195. return $this->httpPostJson('wxa/business/getgoodswarehouse', $params);
  196. }
  197. /**
  198. * Get goods list based on status
  199. *
  200. * @param array $params
  201. * @return \EasyWeChat\Support\Collection|null
  202. * @throws \EasyWeChat\Core\Exceptions\HttpException
  203. */
  204. public function getApproved(array $params)
  205. {
  206. return $this->httpGet('wxaapi/broadcast/goods/getapproved', $params);
  207. }
  208. /**
  209. * Add goods to the designated live room.
  210. *
  211. * @param array $params
  212. * @return \EasyWeChat\Support\Collection|null
  213. * @throws \EasyWeChat\Core\Exceptions\HttpException
  214. */
  215. public function addGoods(array $params)
  216. {
  217. return $this->httpPost('wxaapi/broadcast/room/addgoods', $params);
  218. }
  219. /**
  220. * Get Room List.
  221. *
  222. * @param int $start
  223. * @param int $limit
  224. * @return \EasyWeChat\Support\Collection|null
  225. * @throws \EasyWeChat\Core\Exceptions\HttpException
  226. */
  227. public function getRooms(int $start = 0, int $limit = 10)
  228. {
  229. $params = [
  230. 'start' => $start,
  231. 'limit' => $limit,
  232. ];
  233. return $this->httpPostJson('wxa/business/getliveinfo', $params);
  234. }
  235. /**
  236. * Get Playback List.
  237. *
  238. * @param int $roomId
  239. * @param int $start
  240. * @param int $limit
  241. * @return \EasyWeChat\Support\Collection|null
  242. * @throws \EasyWeChat\Core\Exceptions\HttpException
  243. */
  244. public function getPlaybacks(int $roomId, int $start = 0, int $limit = 10)
  245. {
  246. $params = [
  247. 'action' => 'get_replay',
  248. 'room_id' => $roomId,
  249. 'start' => $start,
  250. 'limit' => $limit,
  251. ];
  252. return $this->httpPostJson('wxa/business/getliveinfo', $params);
  253. }
  254. /**
  255. * Create a live room.
  256. *
  257. * @param array $params
  258. * @return \EasyWeChat\Support\Collection|null
  259. * @throws \EasyWeChat\Core\Exceptions\HttpException
  260. */
  261. public function createLiveRoom(array $params)
  262. {
  263. return $this->httpPostJson('wxaapi/broadcast/room/create', $params);
  264. }
  265. /**
  266. *
  267. * @param int $roomId
  268. * @return \EasyWeChat\Support\Collection|null
  269. * @author Qinii
  270. * @day 10/22/21
  271. */
  272. public function getPushUrl(int $roomId)
  273. {
  274. $params = [
  275. 'roomId' => $roomId,
  276. ];
  277. return $this->httpGet('wxaapi/broadcast/room/getpushurl', $params);
  278. }
  279. /**
  280. * 是否关闭客服 【0:开启,1:关闭】
  281. * @param int $roomId
  282. * @return \EasyWeChat\Support\Collection|null
  283. * @author Qinii
  284. * @day 10/22/21
  285. */
  286. public function closeKf(int $roomId,int $status)
  287. {
  288. $params = [
  289. 'roomId' => $roomId,
  290. 'closeKf' => $status ? 1 : 0,
  291. ];
  292. return $this->httpPostJson('wxaapi/broadcast/room/updatekf', $params);
  293. }
  294. /**
  295. * 1-禁言,0-取消禁言
  296. * @param int $roomId
  297. * @param int $type
  298. * @return \EasyWeChat\Support\Collection|null
  299. * @author Qinii
  300. * @day 10/22/21
  301. */
  302. public function banComment(int $roomId, int $status)
  303. {
  304. $params = [
  305. 'roomId' => $roomId,
  306. 'banComment' => $status ? 1 : 0,
  307. ];
  308. return $this->httpPostJson('wxaapi/broadcast/room/updatecomment', $params);
  309. }
  310. /**
  311. * 添加助手
  312. * @param array $params
  313. * @return \EasyWeChat\Support\Collection|null
  314. * @author Qinii
  315. * @day 10/25/21
  316. */
  317. public function addAssistant(array $params)
  318. {
  319. return $this->httpPostJson('wxaapi/broadcast/room/addassistant', $params);
  320. }
  321. /**
  322. * 删除助手
  323. * @param array $params
  324. * @return \EasyWeChat\Support\Collection|null
  325. * @author Qinii
  326. * @day 10/25/21
  327. */
  328. public function removeAssistant(int $roomId, string $username)
  329. {
  330. $params = [
  331. 'roomId' => $roomId,
  332. 'username' => $username,
  333. ];
  334. return $this->httpPostJson('wxaapi/broadcast/room/removeassistant', $params);
  335. }
  336. /**
  337. * 修改小助手
  338. * @param array $params
  339. * @return \EasyWeChat\Support\Collection|null
  340. * @author Qinii
  341. * @day 10/25/21
  342. */
  343. public function modifyAssistant(array $params)
  344. {
  345. return $this->httpPostJson('wxaapi/broadcast/room/modifyassistant', $params);
  346. }
  347. /**
  348. * 助手列表
  349. * @param int $roomId
  350. * @return \EasyWeChat\Support\Collection|null
  351. * @author Qinii
  352. * @day 10/25/21
  353. * wxa/business/get_wxa_followers?access_token=
  354. */
  355. public function getAssistantList(int $roomId)
  356. {
  357. $params = [
  358. 'roomId' => $roomId,
  359. ];
  360. return $this->httpGet('wxaapi/broadcast/room/getassistantlist', $params);
  361. }
  362. /**
  363. * 获取长期订阅用户
  364. * @param int $roomId
  365. * @return \EasyWeChat\Support\Collection|null
  366. * @author Qinii
  367. * @day 10/25/21
  368. */
  369. public function getFollowers(string $page, int $limit = 2000)
  370. {
  371. $params['limit'] = $limit;
  372. if ($page) $params['page_break'] = $page;
  373. return $this->httpPostJson('wxa/business/get_wxa_followers', $params);
  374. }
  375. /**
  376. * 群发发送订阅
  377. * @param int $roomId
  378. * @param array $data
  379. * @return \EasyWeChat\Support\Collection|null
  380. * @author Qinii
  381. * @day 10/25/21
  382. */
  383. public function pushMessage(int $roomId, array $data)
  384. {
  385. $params = [
  386. 'room_id' => $roomId,
  387. 'user_openid' => $data,
  388. ];
  389. return $this->httpPostJson('wxa/business/push_message', $params);
  390. }
  391. /**
  392. * 更新官方收录
  393. * @param int $roomId
  394. * @param int $status
  395. * @return \EasyWeChat\Support\Collection|null
  396. * @author Qinii
  397. * @day 10/30/21
  398. */
  399. public function updateFeedPublic(int $roomId, int $status)
  400. {
  401. $params = [
  402. 'roomId' => $roomId,
  403. 'isFeedsPublic' => $status ? 1 : 0,
  404. ];
  405. return $this->httpPostJson('wxaapi/broadcast/room/updatefeedpublic', $params);
  406. }
  407. public function goodsOnsale(int $roomId, int $goodsId, int $status)
  408. {
  409. $params = [
  410. 'roomId' => $roomId,
  411. 'goodsId' => $goodsId,
  412. 'onSale' => $status ? 1 : 0,
  413. ];
  414. return $this->httpPostJson('wxaapi/broadcast/goods/onsale', $params);
  415. }
  416. }