LiveRoomServices.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. declare (strict_types=1);
  12. namespace app\services\activity\live;
  13. use app\dao\activity\live\LiveRoomDao;
  14. use app\services\BaseServices;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\services\DownloadImageService;
  17. use crmeb\services\wechat\MiniProgram;
  18. use think\facade\Log;
  19. /**
  20. * 直播间
  21. * Class LiveRoomServices
  22. * @package app\services\activity\live
  23. * @mixin LiveRoomDao
  24. */
  25. class LiveRoomServices extends BaseServices
  26. {
  27. /**
  28. * LiveRoomServices constructor.
  29. * @param LiveRoomDao $dao
  30. */
  31. public function __construct(LiveRoomDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. public function getList(array $where)
  36. {
  37. $where['is_del'] = 0;
  38. [$page, $limit] = $this->getPageValue();
  39. $list = $this->dao->getList($where, '*', [], $page, $limit);
  40. $count = $this->dao->count($where);
  41. return compact('count', 'list');
  42. }
  43. public function userList(array $where)
  44. {
  45. $where['is_show'] = 1;
  46. $where['is_del'] = 0;
  47. [$page, $limit] = $this->getPageValue();
  48. $list = $this->dao->getList($where, '*', ['roomGoods.goods', 'anchor'], $page, $limit);
  49. foreach ($list as &$item) {
  50. $item['roomid'] = $item['room_id'];
  51. $item['goods'] = [];
  52. $item['show_time'] = date('m/d H:i', strtotime($item['start_time']));
  53. if (isset($item['roomGoods']) && $item['roomGoods']) {
  54. $item['goods'] = array_column($item['roomGoods'], 'goods');
  55. }
  56. if (in_array($item['live_status'], [105, 106])) {
  57. $item['live_status'] = 101;
  58. }
  59. if (in_array($item['live_status'], [104, 107])) {
  60. $item['live_status'] = 103;
  61. }
  62. unset($item['roomGoods']);
  63. }
  64. return $list;
  65. }
  66. public function getPlaybacks(int $id)
  67. {
  68. $room = $this->dao->get(['id' => $id, 'is_del' => 0]);
  69. if (!$room) {
  70. throw new AdminException('数据不存在');
  71. }
  72. [$page, $limit] = $this->getPageValue();
  73. return MiniProgram::getLivePlayback($room['room_id'], $page, $limit);
  74. }
  75. public function add(array $data)
  76. {
  77. /** @var LiveAnchorServices $anchorServices */
  78. $anchorServices = app()->make(LiveAnchorServices::class);
  79. $anchor = $anchorServices->get(['wechat' => $data['anchor_wechat']]);
  80. if (!$anchor) {
  81. throw new AdminException('该主播不存在');
  82. }
  83. $data['start_time'] = strtotime($data['start_time']);
  84. $data['end_time'] = strtotime($data['end_time']);
  85. $time = time() + 600;
  86. $time6 = time() + 180 * 24 * 3600;
  87. if ($data['start_time'] < $time || $data['start_time'] > $time6) {
  88. throw new AdminException('开播时间需要在当前时间的10分钟后 并且 开始时间不能在 6 个月后');
  89. }
  90. $t = $data['end_time'] - $data['start_time'];
  91. if ($t < 1800 || $t > 24 * 3600) {
  92. throw new AdminException('开播时间和结束时间间隔不得短于30分钟,不得超过24小时');
  93. }
  94. $data['anchor_name'] = $data['anchor_name'] ?? $anchor['name'];
  95. $data['add_time'] = time();
  96. $wxRoom = $this->wxCreate($data);
  97. $data['room_id'] = $wxRoom['roomId'];
  98. $data['status'] = 2;
  99. if (!$this->dao->save($data)) {
  100. throw new AdminException('添加直播间数据失败');
  101. }
  102. return true;
  103. }
  104. public function apply($id, $status, $msg = '')
  105. {
  106. $room = $this->dao->get($id);
  107. if (!$room) {
  108. throw new AdminException('数据不存在');
  109. }
  110. $room->status = $status;
  111. if ($status == -1)
  112. $room->error_msg = $msg;
  113. else {
  114. $room->room_id = $this->wxCreate($room)['roomId'];
  115. $room->status = 2;
  116. }
  117. $room->save();
  118. }
  119. public function wxCreate($room)
  120. {
  121. try {
  122. $coverImg = root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($room['cover_img'])['path'];
  123. $shareImg = root_path() . 'public' . app()->make(DownloadImageService::class)->downloadImage($room['share_img'])['path'];
  124. } catch (\Throwable $e) {
  125. Log::error('添加直播间封面图出错误,原因:' . $e->getMessage());
  126. $coverImg = root_path() . 'public' . $room['cover_img'];
  127. $shareImg = root_path() . 'public' . $room['share_img'];
  128. }
  129. $data = [
  130. 'startTime' => is_string($room['start_time']) ? strtotime($room['start_time']) : $room['start_time'],
  131. 'endTime' => is_string($room['end_time']) ? strtotime($room['end_time']) : $room['end_time'],
  132. 'name' => $room['name'],
  133. 'anchorName' => $room['anchor_name'],
  134. 'anchorWechat' => $room['anchor_wechat'],
  135. 'screenType' => $room['screen_type'],
  136. 'closeGoods' => $room['close_goods'] == 1 ? 0 : 1,
  137. 'closeLike' => $room['close_like'] == 1 ? 0 : 1,
  138. 'closeComment' => $room['close_comment'] == 1 ? 0 : 1,
  139. 'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
  140. 'type' => $room['type'],
  141. 'coverImg' => MiniProgram::temporaryUpload($coverImg)->media_id,
  142. 'shareImg' => MiniProgram::temporaryUpload($shareImg)->media_id,
  143. 'closeKf' => 1
  144. ];
  145. $data['feedsImg'] = $data['coverImg'];
  146. @unlink($coverImg);
  147. @unlink($shareImg);
  148. return MiniProgram::createLiveRoom($data);
  149. }
  150. public function isShow(int $id, $is_show)
  151. {
  152. $this->dao->update($id, ['is_show' => $is_show]);
  153. return $is_show == 1 ? '显示成功' : '隐藏成功';
  154. }
  155. public function delete(int $id)
  156. {
  157. $room = $this->dao->get(['id' => $id, 'is_del' => 0]);
  158. if ($room) {
  159. if (!$this->dao->update($id, ['is_del' => 1])) {
  160. throw new AdminException('删除失败');
  161. }
  162. /** @var LiveRoomGoodsServices $liveRoomGoods */
  163. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  164. $liveRoomGoods->delete(['live_room_id' => $id]);
  165. }
  166. return true;
  167. }
  168. public function mark($id, $mark)
  169. {
  170. return $this->dao->update($id, compact('mark'));
  171. }
  172. /**
  173. * 直播间添加商品
  174. * @param $room_id
  175. * @param array $ids
  176. * @return bool
  177. * @throws \think\db\exception\DataNotFoundException
  178. * @throws \think\db\exception\DbException
  179. * @throws \think\db\exception\ModelNotFoundException
  180. */
  181. public function exportGoods(int $room_id, array $ids)
  182. {
  183. $liveGoodsServices = app()->make(LiveGoodsServices::class);
  184. if (count($ids) != count($goods = $liveGoodsServices->goodsList($ids)))
  185. throw new AdminException('请选择正确的直播商品');
  186. if (!$room = $this->dao->validRoom($room_id))
  187. throw new AdminException('直播间状态有误');
  188. $data = [];
  189. /** @var LiveRoomGoodsServices $liveRoomGoodsServices */
  190. $liveRoomGoodsServices = app()->make(LiveRoomGoodsServices::class);
  191. //查询已经关联的
  192. $roomGoods = $liveRoomGoodsServices->getColumn(['live_room_id' => $room_id], 'live_goods_id', 'Live_goods_id');
  193. $goods_ids = [];
  194. foreach ($goods as $key => $item) {
  195. if (isset($roomGoods[$item['id']])) {
  196. unset($goods[$key]);
  197. } else {
  198. $goods_ids[] = $item['goods_id'];
  199. $data[] = [
  200. 'live_room_id' => $room_id,
  201. 'live_goods_id' => $item['id']
  202. ];
  203. }
  204. }
  205. if ($goods_ids) {
  206. $liveRoomGoodsServices->saveAll($data);
  207. return MiniProgram::roomAddGoods($room['room_id'], $goods_ids);
  208. }
  209. return true;
  210. }
  211. /**
  212. * 同步直播间状态
  213. * @return bool
  214. */
  215. public function syncRoomStatus()
  216. {
  217. $start = 0;
  218. $limit = 50;
  219. $data = $dataAll = [];
  220. $rooms = $this->dao->getColumn([], 'id,room_id,live_status', 'room_id');
  221. do {
  222. $wxRooms = MiniProgram::getLiveInfo($start, $limit);
  223. foreach ($wxRooms as $room) {
  224. if ($rooms && isset($rooms[$room['roomid']])) {
  225. if ($room['live_status'] != $rooms[$room['roomid']]['live_status']) {
  226. $this->dao->update($rooms[$room['roomid']]['id'], ['live_status' => $room['live_status']]);
  227. }
  228. } else {
  229. $data['name'] = $room['name'];
  230. $data['room_id'] = $room['roomid'];
  231. $data['cover_img'] = $room['cover_img'];
  232. $data['share_img'] = $room['share_img'];
  233. $data['live_status'] = $room['live_status'];
  234. $data['start_time'] = $room['start_time'];
  235. $data['end_time'] = $room['end_time'];
  236. $data['anchor_name'] = $room['anchor_name'];
  237. $dataAll[] = $data;
  238. }
  239. }
  240. $start++;
  241. } while (count($wxRooms) >= $limit);
  242. if ($dataAll) {
  243. $this->dao->saveAll($dataAll);
  244. }
  245. return true;
  246. }
  247. }