LiveRoomServices.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. $liveProductServices = app()->make(LiveRoomGoodsServices::class);
  39. foreach ($list as &$item) {
  40. $item['product_ids'] = array_column($liveProductServices->selectList(['live_room_id' => $item['id']])->toArray(), 'live_goods_id');
  41. }
  42. $count = $this->dao->count($where);
  43. return compact('count', 'list');
  44. }
  45. public function userList(array $where)
  46. {
  47. $where['is_show'] = 1;
  48. $where['is_del'] = 0;
  49. [$page, $limit] = $this->getPageValue();
  50. $list = $this->dao->getList($where, '*', ['roomGoods.goods', 'anchor'], $page, $limit);
  51. foreach ($list as &$item) {
  52. $item['roomid'] = $item['room_id'];
  53. $item['goods'] = [];
  54. $item['show_time'] = date('m/d H:i', strtotime($item['start_time']));
  55. if (isset($item['roomGoods']) && $item['roomGoods']) {
  56. $item['goods'] = array_column($item['roomGoods'], 'goods');
  57. }
  58. if (in_array($item['live_status'], [105, 106])) {
  59. $item['live_status'] = 101;
  60. }
  61. if (in_array($item['live_status'], [104, 107])) {
  62. $item['live_status'] = 103;
  63. }
  64. unset($item['roomGoods']);
  65. }
  66. return $list;
  67. }
  68. public function getPlaybacks(int $id)
  69. {
  70. $room = $this->dao->get(['id' => $id, 'is_del' => 0]);
  71. if (!$room) {
  72. throw new AdminException(100026);
  73. }
  74. [$page, $limit] = $this->getPageValue();
  75. return MiniProgramService::getLivePlayback($room['room_id'], $page, $limit);
  76. }
  77. public function add(array $data)
  78. {
  79. [$data['start_time'], $data['end_time']] = $data['start_time'];
  80. /** @var LiveAnchorServices $anchorServices */
  81. $anchorServices = app()->make(LiveAnchorServices::class);
  82. $anchor = $anchorServices->get(['wechat' => $data['anchor_wechat']]);
  83. if (!$anchor) {
  84. throw new AdminException(400432);
  85. }
  86. $data['start_time'] = strtotime($data['start_time']);
  87. $data['end_time'] = strtotime($data['end_time']);
  88. $time = time() + 600;
  89. $time6 = time() + 180 * 24 * 3600;
  90. if ($data['start_time'] < $time || $data['start_time'] > $time6) {
  91. throw new AdminException(400433);
  92. }
  93. $t = $data['end_time'] - $data['start_time'];
  94. if ($t < 1800 || $t > 24 * 3600) {
  95. throw new AdminException(400434);
  96. }
  97. $data['anchor_name'] = $data['anchor_name'] ?? $anchor['name'];
  98. $data['add_time'] = time();
  99. $wxRoom = $this->wxCreate($data);
  100. $data['room_id'] = $wxRoom['roomId'];
  101. $data['status'] = 2;
  102. if (!$this->dao->save($data)) {
  103. throw new AdminException(100021);
  104. }
  105. return true;
  106. }
  107. public function apply($id, $status, $msg = '')
  108. {
  109. if (!$id) throw new AdminException(100100);
  110. $status = $status == 1 ? 1 : -1;
  111. if ($status == -1 && !$msg) throw new AdminException(400435);
  112. $room = $this->dao->get($id);
  113. if (!$room) {
  114. throw new AdminException(100026);
  115. }
  116. $room->status = $status;
  117. if ($status == -1)
  118. $room->error_msg = $msg;
  119. else {
  120. $room->room_id = $this->wxCreate($room)['roomId'];
  121. $room->status = 2;
  122. }
  123. $room->save();
  124. }
  125. public function wxCreate($room)
  126. {
  127. try {
  128. /** @var DownloadImage $downloadImage */
  129. $downloadImage = app()->make(DownloadImage::class);
  130. $coverImg = $downloadImage->downloadImage($room['cover_img'])['path'];
  131. $shareImg = $downloadImage->downloadImage($room['share_img'])['path'];
  132. } catch (\Throwable $e) {
  133. Log::error('添加直播间封面图出错误,原因:' . $e->getMessage());
  134. $coverImg = $room['cover_img'];
  135. $shareImg = $room['share_img'];
  136. }
  137. $data = [
  138. 'startTime' => is_string($room['start_time']) ? strtotime($room['start_time']) : $room['start_time'],
  139. 'endTime' => is_string($room['end_time']) ? strtotime($room['end_time']) : $room['end_time'],
  140. 'name' => $room['name'],
  141. 'anchorName' => $room['anchor_name'],
  142. 'anchorWechat' => $room['anchor_wechat'],
  143. 'screenType' => $room['screen_type'],
  144. 'closeGoods' => $room['close_goods'] == 1 ? 0 : 1,
  145. 'closeLike' => $room['close_like'] == 1 ? 0 : 1,
  146. 'closeComment' => $room['close_comment'] == 1 ? 0 : 1,
  147. 'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
  148. 'type' => $room['type'],
  149. 'coverImg' => MiniProgramService::materialTemporaryService()->uploadImage(root_path() . 'public' . $coverImg)->media_id,
  150. 'shareImg' => MiniProgramService::materialTemporaryService()->uploadImage(root_path() . 'public' . $shareImg)->media_id,
  151. 'closekf' => 1
  152. ];
  153. $data['feedsImg'] = $data['coverImg'];
  154. @unlink($coverImg);
  155. @unlink($shareImg);
  156. return MiniProgramService::createLiveRoom($data);
  157. }
  158. public function isShow(int $id, $is_show)
  159. {
  160. if (!$id) throw new AdminException(100100);
  161. $this->dao->update($id, ['is_show' => $is_show]);
  162. return true;
  163. }
  164. public function delete(int $id)
  165. {
  166. if (!$id) throw new AdminException(100100);
  167. $room = $this->dao->get(['id' => $id, 'is_del' => 0]);
  168. if ($room) {
  169. if (!$this->dao->update($id, ['is_del' => 1])) {
  170. throw new AdminException(100008);
  171. }
  172. /** @var LiveRoomGoodsServices $liveRoomGoods */
  173. $liveRoomGoods = app()->make(LiveRoomGoodsServices::class);
  174. $liveRoomGoods->delete(['live_room_id' => $id]);
  175. }
  176. return true;
  177. }
  178. public function mark($id, $mark)
  179. {
  180. return $this->dao->update($id, compact('mark'));
  181. }
  182. /**
  183. * 直播间添加商品
  184. * @param $room_id
  185. * @param array $ids
  186. * @return bool
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\DbException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. */
  191. public function exportGoods(int $room_id, array $ids)
  192. {
  193. if (!$room_id) throw new AdminException(100100);
  194. if (!$ids) throw new AdminException(100100);
  195. $liveGoodsServices = app()->make(LiveGoodsServices::class);
  196. if (count($ids) != count($goods = $liveGoodsServices->goodsList($ids)))
  197. throw new AdminException(400436);
  198. if (!$room = $this->dao->validRoom($room_id))
  199. throw new AdminException(400437);
  200. $data = [];
  201. /** @var LiveRoomGoodsServices $liveRoomGoodsServices */
  202. $liveRoomGoodsServices = app()->make(LiveRoomGoodsServices::class);
  203. //查询已经关联的
  204. $roomGoods = $liveRoomGoodsServices->getColumn(['live_room_id' => $room_id], 'live_goods_id', 'Live_goods_id');
  205. $goods_ids = [];
  206. foreach ($goods as $key => $item) {
  207. if (isset($roomGoods[$item['id']])) {
  208. unset($goods[$key]);
  209. } else {
  210. $goods_ids[] = $item['goods_id'];
  211. $data[] = [
  212. 'live_room_id' => $room_id,
  213. 'live_goods_id' => $item['id']
  214. ];
  215. }
  216. }
  217. if ($goods_ids) {
  218. $liveRoomGoodsServices->saveAll($data);
  219. return MiniProgramService::roomAddGoods($room['room_id'], $goods_ids);
  220. }
  221. return true;
  222. }
  223. /**
  224. * 同步直播间状态
  225. * @return bool
  226. * @throws \Exception
  227. */
  228. public function syncRoomStatus()
  229. {
  230. $start = 1;
  231. $limit = 50;
  232. $data = $dataAll = [];
  233. $rooms = $this->dao->getColumn([], 'id,room_id,live_status', 'room_id');
  234. do {
  235. $wxRooms = MiniProgramService::getLiveInfo($start, $limit);
  236. foreach ($wxRooms as $room) {
  237. if ($rooms && isset($rooms[$room['roomid']])) {
  238. if ($room['live_status'] != $rooms[$room['roomid']]['live_status']) {
  239. $this->dao->update($rooms[$room['roomid']]['id'], ['live_status' => $room['live_status']]);
  240. }
  241. } else {
  242. $data['name'] = $room['name'];
  243. $data['room_id'] = $room['roomid'];
  244. $data['cover_img'] = $room['cover_img'];
  245. $data['share_img'] = $room['share_img'];
  246. $data['live_status'] = $room['live_status'];
  247. $data['start_time'] = $room['start_time'];
  248. $data['end_time'] = $room['end_time'];
  249. $data['anchor_name'] = $room['anchor_name'];
  250. $dataAll[] = $data;
  251. }
  252. }
  253. $start++;
  254. } while (count($wxRooms) >= $limit);
  255. if ($dataAll) {
  256. $this->dao->saveAll($dataAll);
  257. }
  258. return true;
  259. }
  260. }