LiveService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use AlibabaCloud\Tea\Exception\TeaUnableRetryError;
  13. use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
  14. use App\Models\LiveGiftModel;
  15. use App\Models\LiveModel;
  16. use App\Models\MemberModel;
  17. use App\Models\VideoCollectModel;
  18. use App\Services\Api\MemberCollectService;
  19. use App\Services\Api\VideoCollectService;
  20. use Darabonba\OpenApi\Models\Config;
  21. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  22. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  23. use Illuminate\Support\Facades\DB;
  24. /**
  25. * 在线直播服务管理-服务类
  26. * @author laravel开发员
  27. * @since 2020/11/11
  28. * Class LiveService
  29. * @package App\Services
  30. */
  31. class LiveService extends BaseService
  32. {
  33. // 静态对象
  34. protected static $instance = null;
  35. /**
  36. * 构造函数
  37. * @author laravel开发员
  38. * @since 2020/11/11
  39. * ConfigService constructor.
  40. */
  41. public function __construct()
  42. {
  43. $this->model = new LiveModel();
  44. }
  45. /**
  46. * 静态入口
  47. * @return SmsService|static|null
  48. */
  49. public static function make()
  50. {
  51. if (!self::$instance) {
  52. self::$instance = new static();
  53. }
  54. return self::$instance;
  55. }
  56. /**
  57. * 列表数据
  58. * @param $params
  59. * @param int $pageSize
  60. * @return array
  61. */
  62. public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
  63. {
  64. $where = ['a.mark' => 1,'a.status'=>1,'b.mark'=>1];
  65. $field = $field? $field : 'lev_a.*';
  66. $order = 'rand()';
  67. $model = $this->model->with(['member'])->from('live as a')
  68. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  69. ->where($where)
  70. ->where(function ($query) use ($params) {
  71. $type = isset($params['type']) ? $params['type'] : 0;
  72. if ($type > 0) {
  73. $query->where('a.type', $type);
  74. }
  75. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  76. if ($categoryId > 0) {
  77. $query->where('a.category_id', $categoryId);
  78. }
  79. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  80. if ($uid > 0) {
  81. $query->where('a.user_id', $uid);
  82. }
  83. })
  84. ->where(function ($query) use ($params) {
  85. $keyword = isset($params['kw']) ? $params['kw'] : '';
  86. if ($keyword) {
  87. $query->where('a.title', 'like', "%{$keyword}%")
  88. ->orWhere('a.description', 'like', "%{$keyword}%")
  89. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  90. }
  91. })->where(function ($query) use ($userId) {
  92. if ($userId) {
  93. $query->where('a.visible_users', '=', '')
  94. ->orWhere('a.visible_users', 'like',"%{$userId},%")
  95. ->orWhere('a.manage_users', 'like',"%{$userId},%");
  96. }
  97. });
  98. // 推荐的数据
  99. $countModel = clone $model;
  100. $total = $countModel->where(function($query) use($params, $userId){
  101. // 推荐视频数据
  102. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  103. if ($isRecommend > 0) {
  104. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  105. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  106. $category = isset($recommendData['category'])? $recommendData['category'] : []; // 按标签推荐
  107. if($uids){
  108. $query->orWhere(function($query) use($uids){
  109. $query->whereIn('a.user_id', $uids);
  110. });
  111. }
  112. if($category){
  113. $query->orWhere(function($query) use($category){
  114. $query->whereIn('a.category_id', $category);
  115. });
  116. }
  117. }
  118. })->count('a.id');
  119. if($total > 0){
  120. // 关联推荐数据
  121. $list = $countModel->selectRaw($field)
  122. ->orderByRaw($order)
  123. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  124. }else{
  125. // 默认推荐数据
  126. $list = $model->selectRaw($field)
  127. ->orderByRaw($order)
  128. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  129. }
  130. $list = $list ? $list->toArray() : [];
  131. if ($list && $list['data']) {
  132. foreach ($list['data'] as &$item) {
  133. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  134. $member = isset($item['member'])? $item['member'] : [];
  135. if($member){
  136. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  137. }
  138. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  139. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  140. $item['member'] = $member;
  141. }
  142. }
  143. return [
  144. 'pageSize' => $pageSize,
  145. 'total' => isset($list['total']) ? $list['total'] : 0,
  146. 'list' => isset($list['data']) ? $list['data'] : []
  147. ];
  148. }
  149. /**
  150. * 详情
  151. * @param $id
  152. * @param $userId
  153. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  154. */
  155. public function getInfo($id, $userId)
  156. {
  157. $info = $this->model->with(['member'])->where(['id'=> $id,'mark'=>1])->first();
  158. if($info && isset($info['member'])){
  159. if(isset($info['member']['avatar'])){
  160. $info['member']['avatar'] = $info['member']['avatar']? $info['member']['avatar'] : '/images/member/logo.png';
  161. $info['member']['avatar'] = get_image_url($info['member']['avatar']);
  162. }
  163. if($info['user_id'] == $userId){
  164. $info['is_follow'] = 1;
  165. }else{
  166. $checkFollow = MemberCollectService::make()->checkCollect($userId, $info['user_id'], 1);
  167. $info['is_follow'] = $checkFollow? 1 : 0;
  168. }
  169. // 观看权限
  170. $info['view_limit'] = 0;
  171. // 浏览历史
  172. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1, 2)){
  173. $data = [
  174. 'user_id'=> $userId,
  175. 'type'=> 1,
  176. 'source_type'=> 2,
  177. 'collect_id'=> $id,
  178. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  179. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  180. 'create_time'=> time(),
  181. 'status'=> 1,
  182. ];
  183. VideoCollectModel::insert($data);
  184. RedisService::clear("caches:videos:recommend:{$userId}_2");
  185. RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
  186. RedisService::clear("caches:live:users_{$id}_{$userId}");
  187. }
  188. // 更新播放量
  189. if(!RedisService::get("caches:live:player:{$userId}_{$id}")){
  190. $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  191. RedisService::set("caches:live:player:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(600, 1800));
  192. $info['views'] += 1;
  193. }
  194. // 结束直播
  195. /*if($info['create_time'] <= time() - 2 * 86400 && $info['status'] == 1){
  196. $this->model->where(['id'=> $id])->update(['status'=>2,'update_time'=>time()]);
  197. $info['status'] = 2;
  198. }*/
  199. }
  200. return $info;
  201. }
  202. /**
  203. * 获取直播间在线观众数据
  204. * @param $params
  205. * @param $pageSize
  206. * @param int $userId
  207. * @return array|mixed
  208. */
  209. public function getUserList($params, $pageSize=200, $userId=0)
  210. {
  211. $liveId = isset($params['live_id'])? $params['live_id'] : 0;
  212. $cachekey = "caches:live:users_{$liveId}_{$userId}";
  213. $datas = RedisService::get($cachekey);
  214. if($datas || RedisService::exists($cachekey)){
  215. return $datas? $datas : [
  216. 'current' => ['user_id'=> $userId,'rank'=>0,'reward_total'=>0],
  217. 'rank_limit'=> 200,
  218. 'total' => 0,
  219. 'list' => []
  220. ];
  221. }
  222. $rankNum = ConfigService::make()->getConfigByCode('live_show_rank', 200);
  223. $rankNum = $rankNum>20 && $rankNum <= 300? $rankNum : 200;
  224. $model = MemberModel::from('member as a')
  225. ->leftJoin('video_collect as b',function($join) use($liveId){
  226. $join->on('a.id','=','b.user_id')
  227. ->where(['b.type'=>1,'b.collect_id'=> $liveId,'b.source_type'=>2,'b.status'=>1,'b.mark'=>1]);
  228. })
  229. ->where(['a.status'=>1,'a.mark'=>1])
  230. ->where('b.id','>', 0)
  231. ->select(['a.id','a.nickname','a.avatar','a.member_level','a.gender','a.intro','a.status','b.create_time','b.reward_total','b.collect_id as live_id']);
  232. $countModel = clone $model;
  233. $total = $countModel->distinct('a.id')->count('a.id');
  234. $datas = $model->groupBy('a.id')
  235. ->orderBy('b.reward_total','desc')
  236. ->orderBy('b.create_time','asc')
  237. ->limit($rankNum)->get();
  238. $datas = $datas? $datas->toArray() : [];
  239. $rankData = ['user_id'=> $userId,'rank'=>0,'reward_total'=>0];
  240. if ($datas) {
  241. foreach ($datas as $k=> &$item) {
  242. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  243. $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : get_image_url('/images/member/logo.png');
  244. $item['reward_total'] = isset($item['reward_total']) && $item['reward_total']? format_num(intval($item['reward_total'])) : 0;
  245. if($item['id'] == $userId){
  246. $rankData['rank'] = ($k+1)>$rankNum? 0 : ($k+1);
  247. $rankData['reward_total'] = $item['reward_total']? format_num(intval($item['reward_total'])) : 0;
  248. }
  249. }
  250. // 用户排名在200名外的数据
  251. if($rankData['rank'] <= 0 && $rankData['reward_total']<=0 && count($datas) > $rankNum){
  252. $rewardTotal = VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'collect_id'=> $liveId,'source_type'=>2,'status'=>1,'mark'=>1])
  253. ->value('reward_total');
  254. $rankData['reward_total'] = $rewardTotal? format_num(intval($rewardTotal)) : 0;
  255. }
  256. $totalText = $total>=10000 && $total<=100000? intval($total/10000).'万' : intval($total);
  257. RedisService::set($cachekey, ['current' => $rankData,'rank_limit'=> $rankNum,'total'=> $total,'total_text'=>$totalText,'list'=> $datas], rand(3,5));
  258. }
  259. return [
  260. 'current' => $rankData,
  261. 'rank_limit'=> $rankNum,
  262. 'total' => $total,
  263. 'total_text' => $total>=10000 && $total<=100000? intval($total/10000).'万' : intval($total),
  264. 'list' => $datas
  265. ];
  266. }
  267. /**
  268. * 获取直播间礼物列表
  269. * @param $params
  270. * @param $pageSize
  271. * @param int $userId
  272. * @return array|mixed
  273. */
  274. public function getGiftList($params, $pageSize=30, $userId=0)
  275. {
  276. $liveId = isset($params['live_id'])? $params['live_id'] : 0;
  277. $cachekey = "caches:live:gift_{$liveId}_{$userId}";
  278. $datas = RedisService::get($cachekey);
  279. if($datas || RedisService::exists($cachekey)){
  280. return $datas? $datas : [
  281. 'pageSize'=> $pageSize,
  282. 'total' => 0,
  283. 'list' => []
  284. ];
  285. }
  286. $list = LiveGiftModel::from('live_gift as a')
  287. ->where(['a.status'=>1,'a.mark'=>1])
  288. ->select(['a.id','a.name','a.code','a.money','a.icon','a.file_url','a.sort'])
  289. ->orderBy('a.sort','desc')
  290. ->orderBy('a.create_time','asc')
  291. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  292. $list = $list ? $list->toArray() : [];
  293. if ($list && $list['data']) {
  294. foreach ($list['data'] as &$item) {
  295. $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : get_image_url('/images/gift/gift.png');
  296. $item['file_url'] = isset($item['file_url']) && $item['file_url']? get_image_url($item['file_url']) : '';
  297. }
  298. RedisService::set($cachekey, [
  299. 'pageSize' => $pageSize,
  300. 'total' => isset($list['total']) ? $list['total'] : 0,
  301. 'list' => isset($list['data']) ? $list['data'] : []
  302. ], rand(5,10));
  303. }
  304. return [
  305. 'pageSize' => $pageSize,
  306. 'total' => isset($list['total']) ? $list['total'] : 0,
  307. 'list' => isset($list['data']) ? $list['data'] : []
  308. ];
  309. }
  310. /**
  311. * 更新播放浏览历史
  312. * @param $userId 用户ID
  313. * @param $id 视频ID
  314. * @return false
  315. */
  316. public function updatePlay($userId, $id)
  317. {
  318. // 浏览历史
  319. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1,2)){
  320. $info = $this->model->from('live as a')
  321. ->where(['a.id'=> $id,'a.mark'=>1])
  322. ->select(['a.id','a.category','a.user_id'])
  323. ->first();
  324. if(empty($info)){
  325. return false;
  326. }
  327. $data = [
  328. 'user_id'=> $userId,
  329. 'type'=> 1,
  330. 'source_type'=> 2,
  331. 'collect_id'=> $id,
  332. 'category_id'=> isset($info['category'])? $info['category'] : 0,
  333. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  334. 'create_time'=> time(),
  335. 'status'=> 1,
  336. ];
  337. VideoCollectModel::insert($data);
  338. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_2", $data, rand(10,30));
  339. RedisService::clear("caches:videos:recommend:{$userId}_1_2");
  340. RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
  341. RedisService::clear("caches:live:users_{$id}_{$userId}");
  342. }
  343. // 更新播放量
  344. if(!RedisService::get("caches:player:live:{$userId}_{$id}")){
  345. $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  346. RedisService::set("caches:player:live:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(6*3600, 86400));
  347. }
  348. $this->error = 1010;
  349. return true;
  350. }
  351. /**
  352. * 缓存数据
  353. * @param $id
  354. * @param int $status
  355. * @return array|mixed
  356. */
  357. public function getCacheInfo($id, $status=0)
  358. {
  359. $cacheKey = "caches:live:info:{$id}_{$status}";
  360. $info = RedisService::get($cacheKey);
  361. if($info){
  362. return $info;
  363. }
  364. $where = ['id' => $id, 'mark' => 1];
  365. if($status){
  366. $where['status'] = $status;
  367. }
  368. $info = $this->model->where($where)
  369. ->select(['id','user_id','create_time','end_time','views','reward_num','reward_total','like_num'])
  370. ->first();
  371. $info = $info? $info->toArray() : [];
  372. if($info){
  373. RedisService::set($cacheKey, $info, rand(3, 5));
  374. }
  375. return $info;
  376. }
  377. /**
  378. * 状态设置
  379. * @return bool
  380. */
  381. public function status()
  382. {
  383. $id = request()->post('id', 0);
  384. $status = request()->post('status', 2);
  385. if ($id && !$info = $this->getCacheInfo($id)) {
  386. $this->error = 2981;
  387. return false;
  388. }
  389. $updateData = ['status'=>$status, 'update_time'=> time()];
  390. if($status == 2){
  391. $updateData['end_time'] = time();
  392. }
  393. if($this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
  394. $this->error = 1002;
  395. $time = $info['end_time']? intval($info['end_time'] - $info['create_time']) : intval(time() - $info['create_time']);
  396. $info['status'] = $status;
  397. $info['live_hour'] = $time > 3600 ? intval($time/3600) : 0;
  398. $info['live_minute'] = $time%3600? intval($time%3600/60) : 0;
  399. $info['time_text'] = date('H:i', $info['create_time']);
  400. $info['end_time_text'] = date('H:i', $info['end_time']? $info['end_time'] : time());
  401. $info['fans_num'] = MemberCollectService::make()->getViewFansCountByType($info['user_id'], $id, 2);
  402. $info['new_fans'] = MemberCollectService::make()->getNewFansCount($info['user_id'], $id,2, $info['create_time']);
  403. return $info;
  404. }
  405. $this->error = 1003;
  406. return true;
  407. }
  408. /**
  409. * 获取直播推流/拉流地址
  410. * @param $streamName
  411. * @param string $appName
  412. * @param string $playType
  413. * @param int $expireTime
  414. * @return array
  415. */
  416. public function getLiveUrl($streamName, $appName = 'xlapp', $playType = 'rtmp', $expireTime = 1440)
  417. {
  418. $cachekey = "caches:live:urls:{$streamName}_{$appName}_{$playType}";
  419. $datas = RedisService::get($cachekey);
  420. if($datas){
  421. return $datas;
  422. }
  423. $playUrls = [];
  424. //未开启鉴权Key的情况下
  425. $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
  426. $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
  427. $pushKey = ConfigService::make()->getConfigByCode('push_url_key');
  428. $playKey = ConfigService::make()->getConfigByCode('play_url_key');
  429. $timeStamp = time() + $expireTime * 60;
  430. if ($pushKey == '') {
  431. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName;
  432. } else {
  433. $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $pushKey;
  434. $md5hash = md5($sstring);
  435. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
  436. }
  437. if ($playKey == '') {
  438. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName;
  439. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv';
  440. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8';
  441. } else {
  442. $rtmpSstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $playKey;
  443. $rtmpMd5hash = md5($rtmpSstring);
  444. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmpMd5hash;
  445. $flvSstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $playKey;
  446. $flvMd5hash = md5($flvSstring);
  447. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flvMd5hash;
  448. $hlsSstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $playKey;
  449. $hlsMd5hash = md5($hlsSstring);
  450. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hlsMd5hash;
  451. }
  452. $datas = [
  453. 'push_url' => $pushUrl,
  454. 'play_url' => isset($playUrls[$playType]) ? $playUrls[$playType] : '',
  455. 'play_urls' => $playUrls,
  456. ];
  457. RedisService::set($cachekey, $datas, 1800);
  458. return $datas;
  459. }
  460. /**
  461. * 创建直播间
  462. * @param $userId 直逼用户
  463. * @param $params
  464. * @return array|false
  465. */
  466. public function create($userId, $params)
  467. {
  468. $liveLevel = ConfigService::make()->getConfigByCode('live_open_level');
  469. $liveLevel = $liveLevel > 0 ? $liveLevel : 0;
  470. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->select(['id', 'nickname', 'member_level', 'status'])->first();
  471. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  472. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  473. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : 0;
  474. if (empty($userInfo) || $status != 1) {
  475. $this->error = 2024;
  476. return false;
  477. }
  478. if($memberLevel < $liveLevel){
  479. $this->error = 2040;
  480. return false;
  481. }
  482. // 验证是否有开播中断播的继续播
  483. $data = [
  484. 'type' => isset($params['type']) ? intval($params['type']) : 1,
  485. 'user_id' => $userId,
  486. 'title' => isset($params['title']) && $params['title'] ? trim($params['title']) : $nickname.'正在直播',
  487. 'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : lang('我正在直播,快来看看吧'),
  488. 'category' => isset($params['category']) ? intval($params['category']) : 0,
  489. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 0,
  490. 'visible_users' => isset($params['visible_users']) ? trim($params['visible_users']) : '',
  491. 'chat_type' => isset($params['chat_type']) ? intval($params['chat_type']) : 0,
  492. 'chat_status' => isset($params['chat_status']) ? intval($params['chat_status']) : 1,
  493. 'pay_status' => isset($params['pay_status']) ? intval($params['pay_status']) : 1,
  494. 'open_area' => isset($params['open_area']) ? intval($params['open_area']) : 1,
  495. 'view_allow' => isset($params['view_allow']) ? intval($params['view_allow']) : 1,
  496. 'push_url' => isset($params['push_url']) ? trim($params['push_url']) : '',
  497. 'play_url' => isset($params['play_url']) ? trim($params['play_url']) : '',
  498. 'update_time'=> time(),
  499. 'status' =>1,
  500. 'mark'=>1,
  501. ];
  502. if($liveId = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id')){
  503. if(!$this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update($data)){
  504. $this->error = 2042;
  505. return false;
  506. }
  507. }else{
  508. $data['create_time'] = time();
  509. $this->model->where(['user_id'=> $userId,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  510. if(!$liveId = $this->model->insertGetId($data)){
  511. $this->error = 2042;
  512. return false;
  513. }
  514. }
  515. $data['id'] = $liveId;
  516. $data['member'] = MemberModel::where(['id'=> $userId])->select(['id','nickname','avatar','status'])->first();
  517. $data['member'] = $data['member']? $data['member'] : [];
  518. $data['member']['is_follow'] = 1;
  519. if(isset($data['member']['avatar'])){
  520. $data['member']['avatar'] = $data['member']['avatar']? $data['member']['avatar'] : '/images/member/logo.png';
  521. $data['member']['avatar'] = get_image_url($data['member']['avatar']);
  522. }
  523. $this->error = 2041;
  524. return $data;
  525. }
  526. }