LiveRoomServices.php 9.8 KB

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