LiveRoomLogic.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\live;
  20. use app\common\enum\LiveEnum;
  21. use app\common\enum\WechatErrorEnum;
  22. use app\common\exception\WechatException;
  23. use app\common\service\WeChatConfigService;
  24. use app\common\service\WeChatService;
  25. use EasyWeChat\Factory;
  26. use think\Exception;
  27. /**
  28. * 直播间逻辑层
  29. * Class LiveRoomLogic
  30. * @package app\adminapi\logic\live
  31. */
  32. class LiveRoomLogic
  33. {
  34. /**
  35. * @notes 直播间列表
  36. * @param int $limitOffset
  37. * @param int $limitLength
  38. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  39. * @throws \GuzzleHttp\Exception\GuzzleException
  40. * @author cjhao
  41. * @date 2021/11/27 10:27
  42. */
  43. public function lists(int $limitOffset,int $limitLength)
  44. {
  45. $result = WeChatService::getLiveRoom($limitOffset-1, $limitLength);
  46. if (!is_array($result)) {
  47. return $result;
  48. }
  49. $data = [];
  50. foreach ($result['room_info'] as $item) {
  51. $data[] = [
  52. 'name' => $item['name'],
  53. 'room_id' => $item['roomid'],
  54. 'cover_img' => $item['cover_img'],
  55. 'anchor_name' => $item['anchor_name'],
  56. 'live_status' => LiveEnum::getLiveStatus($item['live_status']),
  57. 'goods' => count($item['goods']),
  58. 'start_time' => date('Y-m-d H:i:s', $item['start_time']),
  59. 'end_time' => date('Y-m-d H:i:s', $item['end_time'])
  60. ];
  61. }
  62. $list = [
  63. 'lists' => $data,
  64. 'count' => $result['total'],
  65. 'page_no' => $limitOffset,
  66. 'page_size' => $limitLength,
  67. ];
  68. return $list;
  69. }
  70. /**
  71. * @notes 创建直播间
  72. * @param array $post
  73. * @return bool|string
  74. * @throws \EasyWeChat\Kernel\Exceptions\HttpException
  75. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  76. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  77. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  78. * @throws \Psr\SimpleCache\InvalidArgumentException
  79. * @author cjhao
  80. * @date 2021/11/22 18:26
  81. */
  82. public function add(array $post)
  83. {
  84. try {
  85. WechatErrorEnum::wechatErrorMessage();
  86. $data = [
  87. 'name' => $post['name'],
  88. 'coverImg' => $post['cover_img'],
  89. 'startTime' => $post['start_time'],
  90. 'endTime' => $post['end_time'],
  91. 'anchorName' => $post['anchor_name'],
  92. 'anchorWechat' => $post['anchor_wechat'],
  93. 'subAnchorWechat' => $post['sub_anchor_wechat'] ?? '',
  94. 'createrWechat' => $post['anchor_wechat'],
  95. 'shareImg' => $post['share_img'],
  96. 'feedsImg' => $post['feeds_img'],
  97. 'type' => $post['type'],
  98. 'isFeedsPublic' => $post['is_feeds_public'],
  99. 'closeLike' => $post['close_like'],
  100. 'closeGoods' => $post['close_goods'],
  101. 'closeComment' => $post['close_comment'],
  102. 'closeReplay' => $post['close_replay'],
  103. 'closeShare' => $post['close_share'],
  104. 'closeKf' => $post['close_kf'],
  105. ];
  106. $config = WeChatConfigService::getMnpConfig();
  107. $app = Factory::miniProgram($config);
  108. $result = $app->broadcast->createLiveRoom($data);
  109. if (0 != $result['errcode']) {
  110. throw new WechatException($result['errmsg'],$result['errcode']);
  111. }
  112. return true;
  113. } catch (\Exception $e) {
  114. return $e->getMessage();
  115. }
  116. }
  117. /**
  118. * @notes 删除直播间
  119. * @param int $roomId
  120. * @return bool|string
  121. * @author cjhao
  122. * @date 2021/11/23 10:35
  123. */
  124. public function del(int $roomId)
  125. {
  126. try {
  127. $config = WeChatConfigService::getMnpConfig();
  128. $app = Factory::miniProgram($config);
  129. $result = $app->broadcast->deleteLiveRoom(['id'=>$roomId]);
  130. if ($result['errcode'] != 0) {
  131. throw new WechatException($result['errmsg'],$result['errcode']);
  132. }
  133. return true;
  134. } catch (\Exception $e) {
  135. return $e->getMessage();
  136. }
  137. }
  138. }