BroadcastRoomRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace app\common\repositories\store\broadcast;
  3. use app\common\dao\store\broadcast\BroadcastRoomDao;
  4. use app\common\model\store\broadcast\BroadcastRoom;
  5. use app\common\repositories\BaseRepository;
  6. use ln\jobs\SendSmsJob;
  7. use ln\services\DownloadImageService;
  8. use ln\services\MiniProgramService;
  9. use ln\services\SwooleTaskService;
  10. use EasyWeChat\Core\Exceptions\HttpException;
  11. use Exception;
  12. use FormBuilder\Exception\FormBuilderException;
  13. use FormBuilder\Factory\Elm;
  14. use FormBuilder\Form;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\Queue;
  21. use think\facade\Route;
  22. /**
  23. * Class BroadcastRoomRepository
  24. * @package app\common\repositories\store\broadcast
  25. * @author zfy
  26. * @day 2020/7/29
  27. * @mixin BroadcastRoomDao
  28. */
  29. class BroadcastRoomRepository extends BaseRepository
  30. {
  31. /**
  32. * @var BroadcastRoomDao
  33. */
  34. protected $dao;
  35. /**
  36. * BroadcastRoomRepository constructor.
  37. * @param BroadcastRoomDao $dao
  38. */
  39. public function __construct(BroadcastRoomDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. public function getList($merId, array $where, $page, $limit)
  44. {
  45. $where['mer_id'] = $merId;
  46. $query = $this->dao->search($where)->order('create_time DESC');
  47. $count = $query->count();
  48. $list = $query->page($page, $limit)->select();
  49. return compact('count', 'list');
  50. }
  51. public function userList(array $where, $page, $limit)
  52. {
  53. $where['show_tag'] = 1;
  54. $query = $this->dao->search($where)->with('broadcast.goods')->where('room_id', '>', 0)->whereNotIn('live_status', [107])->order('star DESC, sort DESC, create_time DESC');
  55. $count = $query->count();
  56. $list = $query->page($page, $limit)->select();
  57. foreach ($list as $item) {
  58. $item->show_time = date('m/d H:i', strtotime($item->start_time));
  59. }
  60. return compact('count', 'list');
  61. }
  62. public function adminList(array $where, $page, $limit)
  63. {
  64. $query = $this->dao->search($where)->with(['merchant' => function ($query) {
  65. $query->field('mer_name,mer_id,is_trader');
  66. }])->order('BroadcastRoom.star DESC, BroadcastRoom.sort DESC, BroadcastRoom.create_time DESC');
  67. $count = $query->count();
  68. $list = $query->page($page, $limit)->select();
  69. return compact('count', 'list');
  70. }
  71. /**
  72. * @return Form
  73. * @throws FormBuilderException
  74. * @author zfy
  75. * @day 2020/7/29
  76. */
  77. public function createForm()
  78. {
  79. return Elm::createForm(Route::buildUrl('merchantBroadcastRoomCreate')->build(), [
  80. Elm::input('name', '直播间名字')->required(),
  81. Elm::frameImage('cover_img', '背景图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=cover_img&type=1')
  82. ->info('建议像素1080*1920,大小不超过2M')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]),
  83. Elm::frameImage('share_img', '分享图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=share_img&type=1')
  84. ->info('建议像素800*640,大小不超过1M')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false]),
  85. Elm::input('anchor_name', '主播昵称')->required()->placeholder('请输入主播昵称,主播需通过小程序直播认证,否则会提交失败。'),
  86. Elm::input('anchor_wechat', '主播微信号')->required()->placeholder('请输入主播微信号,主播需通过小程序直播认证,否则会提交失败。'),
  87. Elm::input('phone', '联系电话')->required(),
  88. Elm::dateTimeRange('start_time', '直播时间')->value([]),
  89. Elm::radio('type', '直播间类型', 0)->options([['value' => 0, 'label' => '手机直播']])->disabled(true),
  90. Elm::radio('screen_type', '显示样式', 0)->options([['value' => 0, 'label' => '竖屏'], ['value' => 1, 'label' => '横屏']]),
  91. Elm::switches('close_like', '是否开启点赞', 0)->activeValue(0)->inactiveValue(1)->inactiveText('关闭')->activeText('开启'),
  92. Elm::switches('close_goods', '是否开启货架', 0)->activeValue(0)->inactiveValue(1)->inactiveText('关闭')->activeText('开启'),
  93. Elm::switches('close_comment', '是否开启评论', 0)->activeValue(0)->inactiveValue(1)->inactiveText('关闭')->activeText('开启'),
  94. Elm::switches('replay_status', '是否开启回放', 0)->activeValue(1)->inactiveValue(0)->inactiveText('关闭')->activeText('开启'),
  95. Elm::switches('close_share', '是否开启分享', 0)->activeValue(0)->inactiveValue(1)->inactiveText('关闭')->activeText('开启'),
  96. Elm::switches('close_kf', '是否开启客服', 0)->activeValue(0)->inactiveValue(1)->inactiveText('关闭')->activeText('开启'),
  97. ])->setTitle('创建直播间');
  98. }
  99. public function updateForm($id)
  100. {
  101. $data = $this->dao->get($id)->toArray();
  102. $data['start_time'] = [$data['start_time'], $data['end_time']];
  103. return $this->createForm()->setAction(Route::buildUrl('merchantBroadcastRoomUpdate', compact('id'))->build())->formData($data)->setTitle('编辑直播间');
  104. }
  105. public function create($merId, array $data)
  106. {
  107. $data['status'] = request()->merchant()->is_bro_room == 1 ? 0 : 1;
  108. $data['mer_id'] = $merId;
  109. return Db::transaction(function () use ($data) {
  110. $room = $this->dao->create($data);
  111. if ($data['status'] == 1) {
  112. $room->room_id = $this->wxCreate($room);
  113. $room->status = 2;
  114. $room->save();
  115. } else {
  116. SwooleTaskService::admin('notice', [
  117. 'type' => 'new_broadcast',
  118. 'data' => [
  119. 'title' => '新直播间申请',
  120. 'message' => '您有1个新的直播间审核,请及时处理!',
  121. 'id' => $room->broadcast_room_id
  122. ]
  123. ]);
  124. }
  125. return $room;
  126. });
  127. }
  128. public function updateRoom($merId, $id, array $data)
  129. {
  130. $data['status'] = 0;
  131. $room = $this->dao->getWhere(['mer_id' => $merId, 'broadcast_room_id' => $id]);
  132. $room->save($data);
  133. SwooleTaskService::admin('notice', [
  134. 'type' => 'new_broadcast',
  135. 'data' => [
  136. 'title' => '新直播间申请',
  137. 'message' => '您有1个新的直播间审核,请及时处理!',
  138. 'id' => $room->broadcast_room_id
  139. ]
  140. ]);
  141. }
  142. public function applyForm($id)
  143. {
  144. return Elm::createForm(Route::buildUrl('systemBroadcastRoomApply', compact('id'))->build(), [
  145. Elm::radio('status', '审核状态', 1)->options([['value' => -1, 'label' => '未通过'], ['value' => 1, 'label' => '通过']])->control([
  146. ['value' => -1, 'rule' => [
  147. Elm::textarea('msg', '未通过原因', '信息有误,请完善')->required()
  148. ]]
  149. ]),
  150. ])->setTitle('审核直播间');
  151. }
  152. public function apply($id, $status, $msg = '')
  153. {
  154. $room = $this->dao->get($id);
  155. Db::transaction(function () use ($msg, $status, $room) {
  156. $room->status = $status;
  157. if ($status == -1)
  158. $room->error_msg = $msg;
  159. else {
  160. $room->room_id = $this->wxCreate($room);
  161. $room->status = 2;
  162. }
  163. $room->save();
  164. SwooleTaskService::merchant('notice', [
  165. 'type' => 'broadcast_status_' . ($status == -1 ? 'fail' : 'success'),
  166. 'data' => [
  167. 'title' => '直播间审核通知',
  168. 'message' => $status == -1 ? '您的直播间审核未通过!' : '您的直播间审核已通过',
  169. 'id' => $room->broadcast_room_id
  170. ]
  171. ], $room->mer_id);
  172. if ($status == -1) {
  173. Queue::push(SendSmsJob::class, [
  174. 'tempId' => 'BROADCAST_ROOM_FAIL',
  175. 'id' => $room['broadcast_room_id']
  176. ]);
  177. }
  178. });
  179. }
  180. public function wxCreate(BroadcastRoom $room)
  181. {
  182. if ($room['room_id'])
  183. throw new ValidateException('直播间已创建');
  184. $room = $room->toArray();
  185. $miniProgramService = MiniProgramService::create();
  186. $coverImg = './public' . app()->make(DownloadImageService::class)->downloadImage($room['cover_img'])['path'];
  187. $shareImg = './public' . app()->make(DownloadImageService::class)->downloadImage($room['share_img'])['path'];
  188. $shareImgMedia = $miniProgramService->material()->uploadImage($shareImg)->media_id;
  189. $data = [
  190. 'startTime' => strtotime($room['start_time']),
  191. 'endTime' => strtotime($room['end_time']),
  192. 'name' => $room['name'],
  193. 'anchorName' => $room['anchor_name'],
  194. 'anchorWechat' => $room['anchor_wechat'],
  195. 'screenType' => $room['screen_type'],
  196. 'closeGoods' => $room['close_goods'],
  197. 'closeLike' => $room['close_like'],
  198. 'closeComment' => $room['close_comment'],
  199. 'closeShare' => $room['close_share'],
  200. 'closeKf' => $room['close_kf'],
  201. 'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
  202. 'coverImg' => $miniProgramService->material()->uploadImage($coverImg)->media_id,
  203. 'shareImg' => $shareImgMedia,
  204. 'feedsImg' => $shareImgMedia,
  205. ];
  206. @unlink($coverImg);
  207. @unlink($shareImg);
  208. try {
  209. $roomId = $miniProgramService->miniBroadcast()->createLiveRoom($data)->roomId;
  210. } catch (Exception $e) {
  211. throw new ValidateException($e->getMessage());
  212. }
  213. Queue::push(SendSmsJob::class, [
  214. 'tempId' => 'BROADCAST_ROOM_CODE',
  215. 'id' => $room['broadcast_room_id']
  216. ]);
  217. return $roomId;
  218. }
  219. public function isShow($id, $isShow, bool $admin = false)
  220. {
  221. return $this->dao->update($id, [($admin ? 'is_show' : 'is_mer_show') => $isShow]);
  222. }
  223. public function mark($id, $mark)
  224. {
  225. return $this->dao->update($id, compact('mark'));
  226. }
  227. /**
  228. * @param $merId
  229. * @param array $ids
  230. * @param $roomId
  231. * @throws DataNotFoundException
  232. * @throws DbException
  233. * @throws ModelNotFoundException
  234. * @author zfy
  235. * @day 2020/7/31
  236. */
  237. public function exportGoods($merId, array $ids, $roomId)
  238. {
  239. $broadcastGoodsRepository = app()->make(BroadcastGoodsRepository::class);
  240. if (count($ids) != count($goods = $broadcastGoodsRepository->goodsList($merId, $ids)))
  241. throw new ValidateException('请选择正确的直播商品');
  242. if (!$room = $this->dao->validRoom($roomId, $merId))
  243. throw new ValidateException('直播间状态有误');
  244. $broadcastRoomGoodsRepository = app()->make(BroadcastRoomGoodsRepository::class);
  245. $goodsId = $broadcastRoomGoodsRepository->goodsId($room->broadcast_room_id);
  246. $ids = [];
  247. $data = [];
  248. foreach ($goods as $item) {
  249. if (!in_array($item->broadcast_goods_id, $goodsId)) {
  250. $data[] = [
  251. 'broadcast_room_id' => $room->broadcast_room_id,
  252. 'broadcast_goods_id' => $item->broadcast_goods_id
  253. ];
  254. $ids[] = $item->goods_id;
  255. }
  256. }
  257. if (!count($ids)) return;
  258. Db::transaction(function () use ($ids, $broadcastRoomGoodsRepository, $goods, $room, $data) {
  259. $broadcastRoomGoodsRepository->insertAll($data);
  260. MiniProgramService::create()->miniBroadcast()->addGoods(['roomId' => $room->room_id, 'ids' => $ids]);
  261. });
  262. }
  263. public function rmExportGoods($merId, $roomId, $id)
  264. {
  265. if (!$this->dao->merExists($roomId, $merId))
  266. throw new ValidateException('直播间不存在');
  267. app()->make(BroadcastRoomGoodsRepository::class)->rmGoods($id, $roomId);
  268. }
  269. /**
  270. * @throws HttpException
  271. * @throws DbException
  272. * @author zfy
  273. * @day 2020/7/31
  274. */
  275. public function syncRoomStatus()
  276. {
  277. $start = 0;
  278. $limit = 50;
  279. $client = MiniProgramService::create()->miniBroadcast();
  280. do {
  281. $data = $client->getRooms($start, $limit)->room_info;
  282. $start += 50;
  283. $rooms = $this->getRooms(array_column($data, 'roomid'));
  284. foreach ($data as $room) {
  285. if (isset($rooms[$room['roomid']]) && $room['live_status'] != $rooms[$room['roomid']]['live_status']) {
  286. $this->dao->update($rooms[$room['roomid']]['broadcast_room_id'], ['live_status' => $room['live_status']]);
  287. }
  288. }
  289. } while (count($data) >= $limit);
  290. }
  291. public function merDelete($id)
  292. {
  293. // $room = $this->dao->get($id);
  294. // if ($room && ($room->status == -1 || $room->status == 0 || $room->live_status == 107 || $room->live_status == 103)) {
  295. return $this->dao->merDelete($id);
  296. // }
  297. // throw new ValidateException('状态有误,删除失败');
  298. }
  299. }