BroadcastRoomRepository.php 14 KB

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