LiveRoomServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\utils\DownloadImage;
  17. use crmeb\services\app\MiniProgramService;
  18. use think\facade\Log;
  19. /**
  20. * Class LiveRoomServices
  21. * @package app\services\activity\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(100026);
  69. }
  70. [$page, $limit] = $this->getPageValue();
  71. return MiniProgramService::getLivePlayback($room['room_id'], $page, $limit);
  72. }
  73. public function add(array $data)
  74. {
  75. [$data['start_time'], $data['end_time']] = $data['start_time'];
  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(400432);
  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(400433);
  88. }
  89. $t = $data['end_time'] - $data['start_time'];
  90. if ($t < 1800 || $t > 24 * 3600) {
  91. throw new AdminException(400434);
  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(100021);
  100. }
  101. return true;
  102. }
  103. public function apply($id, $status, $msg = '')
  104. {
  105. if (!$id) throw new AdminException(100100);
  106. $status = $status == 1 ? 1 : -1;
  107. if ($status == -1 && !$msg) throw new AdminException(400435);
  108. $room = $this->dao->get($id);
  109. if (!$room) {
  110. throw new AdminException(100026);
  111. }
  112. $room->status = $status;
  113. if ($status == -1)
  114. $room->error_msg = $msg;
  115. else {
  116. $room->room_id = $this->wxCreate($room)['roomId'];
  117. $room->status = 2;
  118. }
  119. $room->save();
  120. }
  121. public function wxCreate($room)
  122. {
  123. try {
  124. /** @var DownloadImage $downloadImage */
  125. $downloadImage = app()->make(DownloadImage::class);
  126. $coverImg = $downloadImage->downloadImage($room['cover_img'])['path'];
  127. $shareImg = $downloadImage->downloadImage($room['share_img'])['path'];
  128. } catch (\Throwable $e) {
  129. Log::error('添加直播间封面图出错误,原因:' . $e->getMessage());
  130. $coverImg = $room['cover_img'];
  131. $shareImg = $room['share_img'];
  132. }
  133. $data = [
  134. 'startTime' => is_string($room['start_time']) ? strtotime($room['start_time']) : $room['start_time'],
  135. 'endTime' => is_string($room['end_time']) ? strtotime($room['end_time']) : $room['end_time'],
  136. 'name' => $room['name'],
  137. 'anchorName' => $room['anchor_name'],
  138. 'anchorWechat' => $room['anchor_wechat'],
  139. 'screenType' => $room['screen_type'],
  140. 'closeGoods' => $room['close_goods'] == 1 ? 0 : 1,
  141. 'closeLike' => $room['close_like'] == 1 ? 0 : 1,
  142. 'closeComment' => $room['close_comment'] == 1 ? 0 : 1,
  143. 'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
  144. 'type' => $room['type'],
  145. 'coverImg' => MiniProgramService::materialTemporaryService()->uploadImage(root_path() . 'public' . $coverImg)->media_id,
  146. 'shareImg' => MiniProgramService::materialTemporaryService()->uploadImage(root_path() . 'public' . $shareImg)->media_id,
  147. 'closekf' => 1
  148. ];
  149. $data['feedsImg'] = $data['coverImg'];
  150. @unlink($coverImg);
  151. @unlink($shareImg);
  152. return MiniProgramService::createLiveRoom($data);
  153. }
  154. public function isShow(int $id, $is_show)
  155. {
  156. if (!$id) throw new AdminException(100100);
  157. $this->dao->update($id, ['is_show' => $is_show]);
  158. return true;
  159. }
  160. public function delete(int $id)
  161. {
  162. if (!$id) throw new AdminException(100100);
  163. $room = $this->dao->get(['id' => $id, 'is_del' => 0]);
  164. if ($room) {
  165. if (!$this->dao->update($id, ['is_del' => 1])) {
  166. throw new AdminException(100008);
  167. }
  168. /** @var LiveRoomGoodsServices $liveRoomGoods */
  169. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  170. $liveRoomGoods->delete(['live_room_id' => $id]);
  171. }
  172. return true;
  173. }
  174. public function mark($id, $mark)
  175. {
  176. return $this->dao->update($id, compact('mark'));
  177. }
  178. /**
  179. * 直播间添加商品
  180. * @param $room_id
  181. * @param array $ids
  182. * @return bool
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function exportGoods(int $room_id, array $ids)
  188. {
  189. if (!$room_id) throw new AdminException(100100);
  190. if (!$ids) throw new AdminException(100100);
  191. $liveGoodsServices = app()->make(LiveGoodsServices::class);
  192. if (count($ids) != count($goods = $liveGoodsServices->goodsList($ids)))
  193. throw new AdminException(400436);
  194. if (!$room = $this->dao->validRoom($room_id))
  195. throw new AdminException(400437);
  196. $data = [];
  197. /** @var LiveRoomGoodsServices $liveRoomGoodsServices */
  198. $liveRoomGoodsServices = app()->make(LiveRoomGoodsServices::class);
  199. //查询已经关联的
  200. $roomGoods = $liveRoomGoodsServices->getColumn(['live_room_id' => $room_id], 'live_goods_id', 'Live_goods_id');
  201. $goods_ids = [];
  202. foreach ($goods as $key => $item) {
  203. if (isset($roomGoods[$item['id']])) {
  204. unset($goods[$key]);
  205. } else {
  206. $goods_ids[] = $item['goods_id'];
  207. $data[] = [
  208. 'live_room_id' => $room_id,
  209. 'live_goods_id' => $item['id']
  210. ];
  211. }
  212. }
  213. if ($goods_ids) {
  214. $liveRoomGoodsServices->saveAll($data);
  215. return MiniProgramService::roomAddGoods($room['room_id'], $goods_ids);
  216. }
  217. return true;
  218. }
  219. /**
  220. * 同步直播间状态
  221. * @return bool
  222. */
  223. public function syncRoomStatus()
  224. {
  225. $start = 1;
  226. $limit = 50;
  227. $data = $dataAll = [];
  228. $rooms = $this->dao->getColumn([], 'id,room_id,live_status', 'room_id');
  229. // if (!$rooms) return true;
  230. do {
  231. $wxRooms = MiniProgramService::getLiveInfo($start, $limit);
  232. foreach ($wxRooms as $room) {
  233. if ($rooms && isset($rooms[$room['roomid']])) {
  234. if ($room['live_status'] != $rooms[$room['roomid']]['live_status']) {
  235. $this->dao->update($rooms[$room['roomid']]['id'], ['live_status' => $room['live_status']]);
  236. }
  237. } else {
  238. $data['name'] = $room['name'];
  239. $data['room_id'] = $room['roomid'];
  240. $data['cover_img'] = $room['cover_img'];
  241. $data['share_img'] = $room['share_img'];
  242. $data['live_status'] = $room['live_status'];
  243. $data['start_time'] = $room['start_time'];
  244. $data['end_time'] = $room['end_time'];
  245. $data['anchor_name'] = $room['anchor_name'];
  246. $dataAll[] = $data;
  247. }
  248. }
  249. $start++;
  250. } while (count($wxRooms) >= $limit);
  251. if ($dataAll) {
  252. $this->dao->saveAll($dataAll);
  253. }
  254. return true;
  255. }
  256. }