ProgramWechatLive.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\wechatlive;
  12. use EasyWeChat\Core\AbstractAPI;
  13. use EasyWeChat\Core\AccessToken;
  14. /**
  15. * Class ProgramWechatLive
  16. * @package crmeb\services\wechatlive
  17. */
  18. class ProgramWechatLive extends AbstractAPI
  19. {
  20. /**
  21. * 获取直播列表信息
  22. */
  23. const API_WECHAT_LIVE = 'https://api.weixin.qq.com/wxa/business/getliveinfo';
  24. /**
  25. * 创建直播间
  26. */
  27. const CREATE_LIVE_ROOM = 'https://api.weixin.qq.com/wxaapi/broadcast/room/create';
  28. /**
  29. * 直播间导入商品
  30. */
  31. const LIVE_ROOM_ADD_GOODS = 'https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods';
  32. /**
  33. * 获取商品列表信息
  34. */
  35. const GOODS_LIST = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved';
  36. /**
  37. * 商品添加并审核
  38. */
  39. const GOODS_ADD = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/add';
  40. /**
  41. * 撤回审核
  42. */
  43. const GOODS_RESET_AUDIT = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit';
  44. /**
  45. * 重新提交审核
  46. */
  47. const GOODS_AUDIT = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/autdit';
  48. /**
  49. * 删除商品
  50. */
  51. const GOODS_DELETE = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/delete';
  52. /**
  53. * 更新商品
  54. */
  55. const GOODS_UPDATE = 'https://api.weixin.qq.com/wxaapi/broadcast/goods/update';
  56. /**
  57. * 获取商品状态
  58. */
  59. const GOODS_INFO = 'https://api.weixin.qq.com/wxa/business/getgoodswarehouse';
  60. /**
  61. * 获取成员列表
  62. */
  63. const ROLE_LIST = 'https://api.weixin.qq.com/wxaapi/broadcast/role/getrolelist';
  64. /**
  65. * 添加直播间参数
  66. * @var array
  67. */
  68. protected $create_data = [
  69. 'name' => '', // 房间名字
  70. 'coverImg' => '', // 通过 uploadfile 上传,填写 mediaID
  71. 'startTime' => 0, // 开始时间
  72. 'endTime' => 0, // 结束时间
  73. 'anchorName' => '', // 主播昵称
  74. 'anchorWechat' => '', // 主播微信号
  75. 'shareImg' => '', //通过 uploadfile 上传,填写 mediaID
  76. 'feedsImg' => '', //通过 uploadfile 上传,填写 mediaID
  77. 'isFeedsPublic' => 1, // 是否开启官方收录,1 开启,0 关闭
  78. 'type' => 1, // 直播类型,1 推流 0 手机直播
  79. 'screenType' => 0, // 1:横屏 0:竖屏
  80. 'closeLike' => 0, // 是否 关闭点赞 1 关闭
  81. 'closeGoods' => 0, // 是否 关闭商品货架,1:关闭
  82. 'closeComment' => 0, // 是否开启评论,1:关闭
  83. 'closeReplay' => 1, // 是否关闭回放 1 关闭
  84. 'closeShare' => 0, // 是否关闭分享 1 关闭
  85. 'closeKf' => 0 // 是否关闭客服,1 关闭
  86. ];
  87. /**
  88. * ProgramWechatLive constructor.
  89. * @param AccessToken $accessToken
  90. */
  91. public function __construct(AccessToken $accessToken)
  92. {
  93. parent::__construct($accessToken);
  94. }
  95. /**
  96. * 获取直播间列表
  97. * @param int $page
  98. * @param int $limit
  99. * @return \EasyWeChat\Support\Collection|null
  100. * @throws \EasyWeChat\Core\Exceptions\HttpException
  101. */
  102. public function getLiveInfo(int $page = 1, int $limit = 10)
  103. {
  104. $page = ($page - 1) * $limit;
  105. $params = [
  106. 'start' => $page,
  107. 'limit' => $limit
  108. ];
  109. return $this->parseJSON('json', [self::API_WECHAT_LIVE, $params]);
  110. }
  111. /**
  112. * 获取直播间回放
  113. * @param int $room_id
  114. * @param int $page
  115. * @param int $limit
  116. * @return \EasyWeChat\Support\Collection|null
  117. * @throws \EasyWeChat\Core\Exceptions\HttpException
  118. */
  119. public function getLivePlayback(int $room_id, int $page = 1, int $limit = 10)
  120. {
  121. $page = ($page - 1) * $limit;
  122. $params = [
  123. 'action' => 'get_replay',
  124. 'room_id' => $room_id,
  125. 'start' => $page,
  126. 'limit' => $limit
  127. ];
  128. return $this->parseJSON('json', [self::API_WECHAT_LIVE, $params]);
  129. }
  130. /**
  131. * 创建直播间
  132. * @param $data
  133. * @return \EasyWeChat\Support\Collection|null
  134. * @throws \EasyWeChat\Core\Exceptions\HttpException
  135. */
  136. public function createRoom(array $data)
  137. {
  138. $params = array_merge($this->create_data, $data);
  139. return $this->parseJSON('json', [self::CREATE_LIVE_ROOM, $params]);
  140. }
  141. /**
  142. * 直播间导入商品
  143. * @param int $room_id
  144. * @param $ids
  145. * @return \EasyWeChat\Support\Collection|null
  146. * @throws \EasyWeChat\Core\Exceptions\HttpException
  147. */
  148. public function roomAddGoods(int $room_id, $ids)
  149. {
  150. $params = [
  151. 'ids' => $ids,
  152. 'roomId' => $room_id
  153. ];
  154. return $this->parseJSON('json', [self::LIVE_ROOM_ADD_GOODS, $params]);
  155. }
  156. /**
  157. * 获取商品列表
  158. * @param $status
  159. * @param int $page
  160. * @param int $limit
  161. * @return \EasyWeChat\Support\Collection|null
  162. * @throws \EasyWeChat\Core\Exceptions\HttpException
  163. */
  164. public function getGoodsList($status, int $page = 0, $limit = 30)
  165. {
  166. $params = [
  167. 'offset' => $page * $limit,
  168. 'limit' => $limit,
  169. 'status' => $status
  170. ];
  171. return $this->parseJSON('json', [self::GOODS_LIST, $params]);
  172. }
  173. /**
  174. * 获取商品详情
  175. * @param $ids
  176. * @return \EasyWeChat\Support\Collection|null
  177. * @throws \EasyWeChat\Core\Exceptions\HttpException
  178. */
  179. public function getGooodsInfo($ids)
  180. {
  181. $params = [
  182. 'goods_ids' => $ids
  183. ];
  184. return $this->parseJSON('json', [self::GOODS_INFO, $params]);
  185. }
  186. /**
  187. * 添加商品
  188. * @param string $coverImgUrl
  189. * @param string $name
  190. * @param int $priceType
  191. * @param string $url
  192. * @param $price
  193. * @param string $price2
  194. * @return \EasyWeChat\Support\Collection|null
  195. * @throws \EasyWeChat\Core\Exceptions\HttpException
  196. */
  197. public function addGoods(string $coverImgUrl, string $name, int $priceType, string $url, $price, $price2 = '')
  198. {
  199. $params = ['goodsInfo' => [
  200. 'coverImgUrl' => $coverImgUrl,
  201. 'name' => $name,
  202. 'priceType' => $priceType,
  203. 'price' => $price,
  204. 'url' => $url
  205. ]];
  206. if ($priceType != 1) $params['goodsInfo']['price2'] = $price2;
  207. return $this->parseJSON('json', [self::GOODS_ADD, $params]);
  208. }
  209. /**
  210. * 商品撤回审核
  211. * @param int $goodsId
  212. * @param int $auditId
  213. * @return \EasyWeChat\Support\Collection|null
  214. * @throws \EasyWeChat\Core\Exceptions\HttpException
  215. */
  216. public function resetauditGoods(int $goodsId, int $auditId)
  217. {
  218. $params = [
  219. 'goodsId' => $goodsId,
  220. 'auditId' => $auditId
  221. ];
  222. return $this->parseJSON('json', [self::GOODS_RESET_AUDIT, $params]);
  223. }
  224. /**
  225. * 商品重新提交审核
  226. * @param int $goodsId
  227. * @return \EasyWeChat\Support\Collection|null
  228. * @throws \EasyWeChat\Core\Exceptions\HttpException
  229. */
  230. public function auditGoods(int $goodsId)
  231. {
  232. $params = [
  233. 'goodsId' => $goodsId
  234. ];
  235. return $this->parseJSON('json', [self::GOODS_AUDIT, $params]);
  236. }
  237. /**
  238. * 删除商品
  239. * @param int $goodsId
  240. * @return \EasyWeChat\Support\Collection|null
  241. * @throws \EasyWeChat\Core\Exceptions\HttpException
  242. */
  243. public function deleteGoods(int $goodsId)
  244. {
  245. $params = [
  246. 'goodsId' => $goodsId
  247. ];
  248. return $this->parseJSON('json', [self::GOODS_DELETE, $params]);
  249. }
  250. /**
  251. * 更新商品
  252. * @param int $goodsId
  253. * @param string $coverImgUrl
  254. * @param string $name
  255. * @param int $priceType
  256. * @param string $url
  257. * @param $price
  258. * @param string $price2
  259. * @return \EasyWeChat\Support\Collection|null
  260. * @throws \EasyWeChat\Core\Exceptions\HttpException
  261. */
  262. public function updateGoods(int $goodsId, string $coverImgUrl, string $name, int $priceType, string $url, $price, $price2 = '')
  263. {
  264. $params = ['goodsInfo' => [
  265. 'goodsId' => $goodsId,
  266. 'coverImgUrl' => $coverImgUrl,
  267. 'name' => $name,
  268. 'priceType' => $priceType,
  269. 'price' => $price,
  270. 'url' => $url
  271. ]];
  272. if ($priceType != 1) $params['goodsInfo']['price2'] = $price2;
  273. return $this->parseJSON('json', [self::GOODS_UPDATE, $params]);
  274. }
  275. /**
  276. * 获取成员列表
  277. * @param int $goodsId
  278. * @return \EasyWeChat\Support\Collection|null
  279. * @throws \EasyWeChat\Core\Exceptions\HttpException
  280. */
  281. public function getRoleList($role = 2, int $page = 0, $limit = 30, $keyword = '')
  282. {
  283. $params = [
  284. 'role' => $role,
  285. 'offset' => $page * $limit,
  286. 'limit' => $limit,
  287. 'keyword' => $keyword
  288. ];
  289. return $this->parseJSON('get', [self::ROLE_LIST, $params]);
  290. }
  291. }