LiveRoomServices.php 9.8 KB

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