BroadcastRoomRepository.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. namespace app\common\repositories\store\broadcast;
  12. use app\common\dao\store\broadcast\BroadcastRoomDao;
  13. use app\common\model\store\broadcast\BroadcastRoom;
  14. use app\common\repositories\BaseRepository;
  15. use crmeb\jobs\SendSmsJob;
  16. use crmeb\services\DownloadImageService;
  17. use crmeb\services\MiniProgramService;
  18. use crmeb\services\SwooleTaskService;
  19. use EasyWeChat\Core\Exceptions\HttpException;
  20. use Exception;
  21. use FormBuilder\Exception\FormBuilderException;
  22. use FormBuilder\Factory\Elm;
  23. use FormBuilder\Form;
  24. use think\db\exception\DataNotFoundException;
  25. use think\db\exception\DbException;
  26. use think\db\exception\ModelNotFoundException;
  27. use think\exception\ValidateException;
  28. use think\facade\Db;
  29. use think\facade\Queue;
  30. use think\facade\Route;
  31. /**
  32. * Class BroadcastRoomRepository
  33. * @package app\common\repositories\store\broadcast
  34. * @author xaboy
  35. * @day 2020/7/29
  36. * @mixin BroadcastRoomDao
  37. */
  38. class BroadcastRoomRepository extends BaseRepository
  39. {
  40. /**
  41. * @var BroadcastRoomDao
  42. */
  43. protected $dao;
  44. /**
  45. * BroadcastRoomRepository constructor.
  46. * @param BroadcastRoomDao $dao
  47. */
  48. public function __construct(BroadcastRoomDao $dao)
  49. {
  50. $this->dao = $dao;
  51. }
  52. public function getList($merId, array $where, $page, $limit)
  53. {
  54. $where['mer_id'] = $merId;
  55. $query = $this->dao->search($where)->order('create_time DESC');
  56. $count = $query->count();
  57. $list = $query->page($page, $limit)->select();
  58. return compact('count', 'list');
  59. }
  60. public function userList(array $where, $page, $limit)
  61. {
  62. $where['show_tag'] = 1;
  63. $query = $this->dao->search($where)->with([
  64. 'broadcast' => function($query) {
  65. $query->where('on_sale',1);
  66. $query->with('goods');
  67. }
  68. ])->where('room_id', '>', 0)
  69. ->whereNotIn('live_status', [107])->order('star DESC, sort DESC, create_time DESC');
  70. $count = $query->count();
  71. $list = $query->page($page, $limit)->select();
  72. foreach ($list as $item) {
  73. $item->show_time = date('m/d H:i', strtotime($item->start_time));
  74. }
  75. return compact('count', 'list');
  76. }
  77. public function adminList(array $where, $page, $limit)
  78. {
  79. $query = $this->dao->search($where)->with(['merchant' => function ($query) {
  80. $query->field('mer_name,mer_id,is_trader');
  81. }])->order('BroadcastRoom.star DESC, BroadcastRoom.sort DESC, BroadcastRoom.create_time DESC');
  82. $count = $query->count();
  83. $list = $query->page($page, $limit)->select();
  84. return compact('count', 'list');
  85. }
  86. /**
  87. * @return Form
  88. * @throws FormBuilderException
  89. * @author xaboy
  90. * @day 2020/7/29
  91. */
  92. public function createForm()
  93. {
  94. return Elm::createForm(Route::buildUrl('merchantBroadcastRoomCreate')->build(), [
  95. Elm::input('name', '直播间名字')->required(),
  96. Elm::frameImage('cover_img', '背景图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=cover_img&type=1')
  97. ->info('建议像素1080*1920,大小不超过2M')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false])->required(),
  98. Elm::frameImage('share_img', '分享图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=share_img&type=1')
  99. ->info('建议像素800*640,大小不超过1M')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false])->required(),
  100. Elm::frameImage('feeds_img', '封面图', '/' . config('admin.merchant_prefix') . '/setting/uploadPicture?field=feeds_img&type=1')
  101. ->info('建议像素800*800,大小不超过1M')->modal(['modal' => false])->width('896px')->height('480px')->props(['footer' => false])->required(),
  102. Elm::input('anchor_name', '主播昵称')->required()->placeholder('请输入主播昵称,主播需通过小程序直播认证,否则会提交失败。'),
  103. Elm::input('anchor_wechat', '主播微信号')->required()->placeholder('请输入主播微信号,主播需通过小程序直播认证,否则会提交失败。'),
  104. Elm::input('phone', '联系电话')->required(),
  105. Elm::dateTimeRange('start_time', '直播时间')->value([])->required(),
  106. Elm::radio('type', '直播间类型', 0)->options([['value' => 0, 'label' => '手机直播'],['value' => 1, 'label' => '推流']]),
  107. Elm::radio('screen_type', '显示样式', 0)->options([['value' => 0, 'label' => '竖屏'], ['value' => 1, 'label' => '横屏']]),
  108. Elm::switches('close_like', '是否开启点赞', 0)
  109. ->activeValue(0)->inactiveValue(1)
  110. ->activeText('开')->inactiveText('关'),
  111. Elm::switches('close_goods', '是否开启货架', 0)
  112. ->activeValue(0)->inactiveValue(1)
  113. ->activeText('开')->inactiveText('关'),
  114. Elm::switches('close_comment', '是否开启评论', 0)
  115. ->activeValue(0)->inactiveValue(1)
  116. ->activeText('开')->inactiveText('关'),
  117. Elm::switches('replay_status', '是否开启回放', 0)
  118. ->activeValue(1)->inactiveValue(0)
  119. ->activeText('开')->inactiveText('关'),
  120. Elm::switches('close_share', '是否开启分享', 0)
  121. ->activeValue(0)->inactiveValue(1)
  122. ->activeText('开')->inactiveText('关'),
  123. Elm::switches('close_kf', '是否开启客服', 0)
  124. ->activeValue(0)->inactiveValue(1)
  125. ->activeText('开')->inactiveText('关'),
  126. Elm::switches('is_feeds_public', '是否开启官方收录', 1)
  127. ->activeValue(1)->inactiveValue(0)
  128. ->activeText('开')->inactiveText('关'),
  129. ])->setTitle('创建直播间');
  130. }
  131. public function updateForm($id)
  132. {
  133. $data = $this->dao->get($id)->toArray();
  134. $data['start_time'] = [$data['start_time'], $data['end_time']];
  135. return $this->createForm()->setAction(Route::buildUrl('merchantBroadcastRoomUpdate', compact('id'))->build())->formData($data)->setTitle('编辑直播间');
  136. }
  137. public function create($merId, array $data)
  138. {
  139. $data['status'] = request()->merchant()->is_bro_room == 1 ? 0 : 1;
  140. $data['mer_id'] = $merId;
  141. return Db::transaction(function () use ($data) {
  142. $room = $this->dao->create($data);
  143. if ($data['status'] == 1) {
  144. $room->room_id = $this->wxCreate($room);
  145. $room->status = 2;
  146. $room->save();
  147. } else {
  148. SwooleTaskService::admin('notice', [
  149. 'type' => 'new_broadcast',
  150. 'data' => [
  151. 'title' => '新直播间申请',
  152. 'message' => '您有1个新的直播间审核,请及时处理!',
  153. 'id' => $room->broadcast_room_id
  154. ]
  155. ]);
  156. }
  157. return $room;
  158. });
  159. }
  160. public function updateRoom($merId, $id, array $data)
  161. {
  162. $data['status'] = 0;
  163. $room = $this->dao->getWhere(['mer_id' => $merId, 'broadcast_room_id' => $id]);
  164. $room->save($data);
  165. SwooleTaskService::admin('notice', [
  166. 'type' => 'new_broadcast',
  167. 'data' => [
  168. 'title' => '新直播间申请',
  169. 'message' => '您有1个新的直播间审核,请及时处理!',
  170. 'id' => $room->broadcast_room_id
  171. ]
  172. ]);
  173. }
  174. public function applyForm($id)
  175. {
  176. return Elm::createForm(Route::buildUrl('systemBroadcastRoomApply', compact('id'))->build(), [
  177. Elm::radio('status', '审核状态', '1')->options([['value' => '-1', 'label' => '未通过'], ['value' => '1', 'label' => '通过']])->control([
  178. ['value' => -1, 'rule' => [
  179. Elm::textarea('msg', '未通过原因', '信息有误,请完善')->required()
  180. ]]
  181. ]),
  182. ])->setTitle('审核直播间');
  183. }
  184. public function apply($id, $status, $msg = '')
  185. {
  186. $room = $this->dao->get($id);
  187. Db::transaction(function () use ($msg, $status, $room) {
  188. $room->status = $status;
  189. if ($status == -1)
  190. $room->error_msg = $msg;
  191. else {
  192. $room_id = $this->wxCreate($room);
  193. $room->room_id = $room_id;
  194. $room->status = 2;
  195. if ($room->type) {
  196. $path = MiniProgramService::create()->miniBroadcast()->getPushUrl($room_id);
  197. $room->push_url = $path->pushAddr;
  198. }
  199. }
  200. $room->save();
  201. SwooleTaskService::merchant('notice', [
  202. 'type' => 'broadcast_status_' . ($status == -1 ? 'fail' : 'success'),
  203. 'data' => [
  204. 'title' => '直播间审核通知',
  205. 'message' => $status == -1 ? '您的直播间审核未通过!' : '您的直播间审核已通过',
  206. 'id' => $room->broadcast_room_id
  207. ]
  208. ], $room->mer_id);
  209. if ($status == -1) {
  210. Queue::push(SendSmsJob::class, [
  211. 'tempId' => 'BROADCAST_ROOM_FAIL',
  212. 'id' => $room['broadcast_room_id']
  213. ]);
  214. }
  215. });
  216. }
  217. public function wxCreate(BroadcastRoom $room)
  218. {
  219. if ($room['room_id'])
  220. throw new ValidateException('直播间已创建');
  221. $room = $room->toArray();
  222. $miniProgramService = MiniProgramService::create();
  223. $coverImg = './public' . app()->make(DownloadImageService::class)->downloadImage($room['cover_img'])['path'];
  224. $shareImg = './public' . app()->make(DownloadImageService::class)->downloadImage($room['share_img'])['path'];
  225. $feedsImg = './public' . app()->make(DownloadImageService::class)->downloadImage($room['feeds_img'])['path'];
  226. $data = [
  227. 'startTime' => strtotime($room['start_time']),
  228. 'endTime' => strtotime($room['end_time']),
  229. 'name' => $room['name'],
  230. 'anchorName' => $room['anchor_name'],
  231. 'anchorWechat' => $room['anchor_wechat'],
  232. 'screenType' => $room['screen_type'],
  233. 'closeGoods' => $room['close_goods'],
  234. 'closeLike' => $room['close_like'],
  235. 'closeComment' => $room['close_comment'],
  236. 'closeShare' => $room['close_share'],
  237. 'closeKf' => $room['close_kf'],
  238. 'closeReplay' => $room['replay_status'] == 1 ? 0 : 1,
  239. 'isFeedsPublic' => $room['is_feeds_public'] == 1 ? 0 : 1,
  240. 'coverImg' => $miniProgramService->material()->uploadImage($coverImg)->media_id,
  241. 'shareImg' => $miniProgramService->material()->uploadImage($shareImg)->media_id,
  242. 'feedsImg' => $miniProgramService->material()->uploadImage($feedsImg)->media_id,
  243. ];
  244. @unlink($coverImg);
  245. @unlink($shareImg);
  246. @unlink($feedsImg);
  247. try {
  248. $roomId = $miniProgramService->miniBroadcast()->createLiveRoom($data)->roomId;
  249. } catch (Exception $e) {
  250. throw new ValidateException($e->getMessage());
  251. }
  252. Queue::push(SendSmsJob::class, [
  253. 'tempId' => 'BROADCAST_ROOM_CODE',
  254. 'id' => $room['broadcast_room_id']
  255. ]);
  256. return $roomId;
  257. }
  258. public function isShow($id, $isShow, bool $admin = false)
  259. {
  260. return $this->dao->update($id, [($admin ? 'is_show' : 'is_mer_show') => $isShow]);
  261. }
  262. public function mark($id, $mark)
  263. {
  264. return $this->dao->update($id, compact('mark'));
  265. }
  266. /**
  267. * @param $merId
  268. * @param array $ids
  269. * @param $roomId
  270. * @throws DataNotFoundException
  271. * @throws DbException
  272. * @throws ModelNotFoundException
  273. * @author xaboy
  274. * @day 2020/7/31
  275. */
  276. public function exportGoods($merId, array $ids, $roomId)
  277. {
  278. $broadcastGoodsRepository = app()->make(BroadcastGoodsRepository::class);
  279. if (count($ids) != count($goods = $broadcastGoodsRepository->goodsList($merId, $ids)))
  280. throw new ValidateException('请选择正确的直播商品');
  281. if (!$room = $this->dao->validRoom($roomId, $merId))
  282. throw new ValidateException('直播间状态有误');
  283. $broadcastRoomGoodsRepository = app()->make(BroadcastRoomGoodsRepository::class);
  284. $goodsId = $broadcastRoomGoodsRepository->goodsId($room->broadcast_room_id);
  285. $ids = [];
  286. $data = [];
  287. foreach ($goods as $item) {
  288. if (!in_array($item->broadcast_goods_id, $goodsId)) {
  289. $data[] = [
  290. 'broadcast_room_id' => $room->broadcast_room_id,
  291. 'broadcast_goods_id' => $item->broadcast_goods_id
  292. ];
  293. $ids[] = $item->goods_id;
  294. }
  295. }
  296. if (!count($ids)) return;
  297. Db::transaction(function () use ($ids, $broadcastRoomGoodsRepository, $goods, $room, $data) {
  298. $broadcastRoomGoodsRepository->insertAll($data);
  299. MiniProgramService::create()->miniBroadcast()->addGoods(['roomId' => $room->room_id, 'ids' => $ids]);
  300. });
  301. }
  302. public function rmExportGoods($merId, $roomId, $id)
  303. {
  304. if (!$this->dao->merExists($roomId, $merId))
  305. throw new ValidateException('直播间不存在');
  306. app()->make(BroadcastRoomGoodsRepository::class)->rmGoods($id, $roomId);
  307. }
  308. /**
  309. * @throws HttpException
  310. * @throws DbException
  311. * @author xaboy
  312. * @day 2020/7/31
  313. */
  314. public function syncRoomStatus()
  315. {
  316. $start = 0;
  317. $limit = 50;
  318. $client = MiniProgramService::create()->miniBroadcast();
  319. do {
  320. $data = $client->getRooms($start, $limit)->room_info;
  321. $start += 50;
  322. $rooms = $this->getRooms(array_column($data, 'roomid'));
  323. foreach ($data as $room) {
  324. if (isset($rooms[$room['roomid']]) && $room['live_status'] != $rooms[$room['roomid']]['live_status']) {
  325. $this->dao->update($rooms[$room['roomid']]['broadcast_room_id'], ['live_status' => $room['live_status']]);
  326. }
  327. }
  328. } while (count($data) >= $limit);
  329. }
  330. public function merDelete($id)
  331. {
  332. // $room = $this->dao->get($id);
  333. // if ($room && ($room->status == -1 || $room->status == 0 || $room->live_status == 107 || $room->live_status == 103)) {
  334. return $this->dao->merDelete($id);
  335. // }
  336. // throw new ValidateException('状态有误,删除失败');
  337. }
  338. public function closeInfo($id, string $type, int $status, $check = true, $data = [])
  339. {
  340. $room = $this->dao->get($id);
  341. if ($room->status !== 2) throw new ValidateException('直播间还未审核通过,无法修改');
  342. if (!$room) throw new ValidateException('数据不存在');
  343. if ($check && $room[$type] == -1) {
  344. throw new ValidateException('平台已关闭,您无法修改');
  345. }
  346. Db::transaction(function () use ($room, $id, $type, $status,$data) {
  347. $client = MiniProgramService::create()->miniBroadcast();
  348. switch ($type) {
  349. case 'close_kf':
  350. $client->closeKf($room->room_id, $status);
  351. $room->close_kf = $status;
  352. break;
  353. case 'close_comment':
  354. $client->banComment($room->room_id, $status);
  355. $room->close_comment = $status;
  356. break;
  357. case 'is_feeds_public':
  358. $client->updateFeedPublic($room->room_id,$status);
  359. $room->is_feeds_public = $status;
  360. break;
  361. case 'on_sale':
  362. $ret = app()->make(BroadcastRoomGoodsRepository::class)->getWhere([
  363. 'broadcast_room_id' => $id,
  364. 'broadcast_goods_id' => $data['goods_id'],
  365. ],'*',['goods']);
  366. if (!isset($ret['goods']['goods_id'])) throw new ValidateException('数据不存在');
  367. $ret->on_sale = $status;
  368. $ret->save();
  369. $client->goodsOnsale($room->room_id,$ret['goods']['goods_id'],$status);
  370. $room->is_feeds_public = $status;
  371. break;
  372. }
  373. $room->save();
  374. });
  375. }
  376. public function assistantForm(int $id, int $merId)
  377. {
  378. $make = app()->make(BroadcastAssistantRepository::class);
  379. $get = $this->dao->get($id);
  380. if ($get->status !== 2) throw new ValidateException('直播间还未审核通过,无法操作');
  381. $data = $make->options($merId);
  382. $has = $make->intersection($get->assistant_id, $merId);
  383. return Elm::createForm(Route::buildUrl('merchantBroadcastAddAssistant', compact('id'))->build(),
  384. [
  385. Elm::selectMultiple('assistant_id', '小助手')->options(function () use ($data) {
  386. $options = [];
  387. if ($data) {
  388. foreach ($data as $value => $label) {
  389. $options[] = compact('value', 'label');
  390. }
  391. }
  392. return $options;
  393. })
  394. ])->setTitle('修改小助手');
  395. }
  396. public function editAssistant(int $id, int $merId, array $data)
  397. {
  398. $make = app()->make(BroadcastAssistantRepository::class);
  399. $make->existsAll($data, $merId);
  400. Db::transaction(function() use($id, $data){
  401. $get = $this->dao->get($id);
  402. $old = explode(',', $get->assistant_id);
  403. $remove = array_diff($old, $data);
  404. $add = array_diff($data, $old);
  405. $this->addAssistant($get->room_id, $add);
  406. $this->removeAssistant($get->room_id, $remove);
  407. $get->assistant_id = implode(',', $data);
  408. $get->save();
  409. });
  410. }
  411. public function removeAssistant($roomId, array $ids)
  412. {
  413. $make = app()->make(BroadcastAssistantRepository::class);
  414. $data = $make->getSearch(['assistant_ids' => $ids])->select();
  415. foreach ($data as $datum) {
  416. MiniProgramService::create()->miniBroadcast()->removeAssistant($roomId,$datum->username);
  417. }
  418. }
  419. public function addAssistant($roomId, array $ids)
  420. {
  421. $make = app()->make(BroadcastAssistantRepository::class);
  422. $data = $make->getSearch(['assistant_ids' => $ids])->column('username,nickname');
  423. $params = [
  424. 'roomId' => $roomId,
  425. 'users' => $data
  426. ];
  427. MiniProgramService::create()->miniBroadcast()->addAssistant($params);
  428. }
  429. public function pushMessage(int $id)
  430. {
  431. $get = $this->dao->get($id);
  432. $make = MiniProgramService::create()->miniBroadcast();
  433. $page_break = '';
  434. do{
  435. $data = $make->getFollowers($page_break);
  436. $restult = [];
  437. if ($data['errcode'] !== 0) throw new ValidateException($data['errmsg']);
  438. foreach ($data['followers'] as $datum) {
  439. if ($datum['room_id'] == $get->room_id) {
  440. $restult[] = $datum['openid'];
  441. }
  442. }
  443. if ($restult) {
  444. $make->pushMessage($get->room_id, $restult);
  445. }
  446. $page_break = $data['page_break'] ?? '';
  447. }while($page_break);
  448. }
  449. }