LiveService.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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.is_online'=>1,'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. $page = request()->post('page', 1);
  277. $liveId = isset($params['live_id'])? $params['live_id'] : 0;
  278. $cachekey = "caches:live:gift_{$liveId}_{$userId}_{$page}";
  279. $datas = RedisService::get($cachekey);
  280. if($datas || RedisService::exists($cachekey)){
  281. return $datas? $datas : [
  282. 'pageSize'=> $pageSize,
  283. 'total' => 0,
  284. 'list' => []
  285. ];
  286. }
  287. $list = LiveGiftModel::from('live_gift as a')
  288. ->where(['a.status'=>1,'a.mark'=>1])
  289. ->select(['a.id','a.name','a.code','a.money','a.icon','a.file_url','a.sort'])
  290. ->orderBy('a.sort','desc')
  291. ->orderBy('a.create_time','asc')
  292. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  293. $list = $list ? $list->toArray() : [];
  294. if ($list && $list['data']) {
  295. foreach ($list['data'] as &$item) {
  296. $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : get_image_url('/images/gift/gift.png');
  297. $item['file_url'] = isset($item['file_url']) && $item['file_url']? get_image_url($item['file_url']) : '';
  298. }
  299. RedisService::set($cachekey, [
  300. 'pageSize' => $pageSize,
  301. 'total' => isset($list['total']) ? $list['total'] : 0,
  302. 'list' => isset($list['data']) ? $list['data'] : []
  303. ], rand(5,10));
  304. }
  305. return [
  306. 'pageSize' => $pageSize,
  307. 'total' => isset($list['total']) ? $list['total'] : 0,
  308. 'list' => isset($list['data']) ? $list['data'] : []
  309. ];
  310. }
  311. /**
  312. * 更新播放浏览历史
  313. * @param $userId 用户ID
  314. * @param $id 视频ID
  315. * @return false
  316. */
  317. public function updatePlay($userId, $id)
  318. {
  319. // 浏览历史
  320. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1,2)){
  321. $info = $this->model->from('live as a')
  322. ->where(['a.id'=> $id,'a.mark'=>1])
  323. ->select(['a.id','a.category','a.user_id'])
  324. ->first();
  325. if(empty($info)){
  326. return false;
  327. }
  328. $data = [
  329. 'user_id'=> $userId,
  330. 'type'=> 1,
  331. 'source_type'=> 2,
  332. 'collect_id'=> $id,
  333. 'category_id'=> isset($info['category'])? $info['category'] : 0,
  334. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  335. 'create_time'=> time(),
  336. 'status'=> 1,
  337. ];
  338. VideoCollectModel::insert($data);
  339. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_2", $data, rand(10,30));
  340. RedisService::clear("caches:videos:recommend:{$userId}_1_2");
  341. RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
  342. RedisService::clear("caches:live:users_{$id}_{$userId}");
  343. }
  344. // 更新播放量
  345. if(!RedisService::get("caches:player:live:{$userId}_{$id}")){
  346. $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  347. RedisService::set("caches:player:live:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(6*3600, 86400));
  348. }
  349. $this->error = 1010;
  350. return true;
  351. }
  352. /**
  353. * 缓存数据
  354. * @param $id
  355. * @param int $status
  356. * @return array|mixed
  357. */
  358. public function getCacheInfo($id, $status=0)
  359. {
  360. $cacheKey = "caches:live:info:{$id}_{$status}";
  361. $info = RedisService::get($cacheKey);
  362. if($info){
  363. return $info;
  364. }
  365. $where = ['id' => $id, 'mark' => 1];
  366. if($status){
  367. $where['status'] = $status;
  368. }
  369. $info = $this->model->where($where)
  370. ->select(['id','user_id','create_time','end_time','views','reward_num','reward_total','like_num'])
  371. ->first();
  372. $info = $info? $info->toArray() : [];
  373. if($info){
  374. RedisService::set($cacheKey, $info, rand(3, 5));
  375. }
  376. return $info;
  377. }
  378. /**
  379. * 状态设置
  380. * @return bool
  381. */
  382. public function status()
  383. {
  384. $id = request()->post('id', 0);
  385. $status = request()->post('status', 2);
  386. if ($id && !$info = $this->getCacheInfo($id)) {
  387. $this->error = 2981;
  388. return false;
  389. }
  390. $updateData = ['status'=>$status, 'update_time'=> time()];
  391. if($status == 2){
  392. $updateData['end_time'] = time();
  393. }
  394. if($this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
  395. $this->error = 1002;
  396. $time = $info['end_time']? intval($info['end_time'] - $info['create_time']) : intval(time() - $info['create_time']);
  397. $info['status'] = $status;
  398. $info['live_hour'] = $time > 3600 ? intval($time/3600) : 0;
  399. $info['live_minute'] = $time%3600? intval($time%3600/60) : 0;
  400. $info['time_text'] = date('H:i', $info['create_time']);
  401. $info['end_time_text'] = date('H:i', $info['end_time']? $info['end_time'] : time());
  402. $info['fans_num'] = MemberCollectService::make()->getViewFansCountByType($info['user_id'], $id, 2);
  403. $info['new_fans'] = MemberCollectService::make()->getNewFansCount($info['user_id'], $id,2, $info['create_time']);
  404. return $info;
  405. }
  406. $this->error = 1003;
  407. return true;
  408. }
  409. /**
  410. * 获取直播推流/拉流地址
  411. * @param $streamName
  412. * @param string $appName
  413. * @param string $playType
  414. * @param int $expireTime
  415. * @return array
  416. */
  417. public function getLiveUrl($streamName, $appName = 'xlapp', $playType = 'rtmp', $expireTime = 1440)
  418. {
  419. $cachekey = "caches:live:urls:{$streamName}_{$appName}_{$playType}";
  420. $datas = RedisService::get($cachekey);
  421. if($datas){
  422. return $datas;
  423. }
  424. $playUrls = [];
  425. //未开启鉴权Key的情况下
  426. $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
  427. $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
  428. $pushKey = ConfigService::make()->getConfigByCode('push_url_key');
  429. $playKey = ConfigService::make()->getConfigByCode('play_url_key');
  430. $timeStamp = time() + $expireTime * 60;
  431. if ($pushKey == '') {
  432. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName;
  433. } else {
  434. $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $pushKey;
  435. $md5hash = md5($sstring);
  436. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
  437. }
  438. if ($playKey == '') {
  439. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName;
  440. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv';
  441. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8';
  442. } else {
  443. $rtmpSstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $playKey;
  444. $rtmpMd5hash = md5($rtmpSstring);
  445. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmpMd5hash;
  446. $flvSstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $playKey;
  447. $flvMd5hash = md5($flvSstring);
  448. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flvMd5hash;
  449. $hlsSstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $playKey;
  450. $hlsMd5hash = md5($hlsSstring);
  451. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hlsMd5hash;
  452. }
  453. $datas = [
  454. 'push_url' => $pushUrl,
  455. 'play_url' => isset($playUrls[$playType]) ? $playUrls[$playType] : '',
  456. 'play_urls' => $playUrls,
  457. ];
  458. RedisService::set($cachekey, $datas, 1800);
  459. return $datas;
  460. }
  461. /**
  462. * 创建直播间
  463. * @param $userId 直逼用户
  464. * @param $params
  465. * @return array|false
  466. */
  467. public function create($userId, $params)
  468. {
  469. $liveLevel = ConfigService::make()->getConfigByCode('live_open_level');
  470. $liveLevel = $liveLevel > 0 ? $liveLevel : 0;
  471. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->select(['id', 'nickname', 'member_level', 'status'])->first();
  472. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  473. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  474. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : 0;
  475. if (empty($userInfo) || $status != 1) {
  476. $this->error = 2024;
  477. return false;
  478. }
  479. if($memberLevel < $liveLevel){
  480. $this->error = 2040;
  481. return false;
  482. }
  483. // 验证是否有开播中断播的继续播
  484. $data = [
  485. 'type' => isset($params['type']) ? intval($params['type']) : 1,
  486. 'user_id' => $userId,
  487. 'title' => isset($params['title']) && $params['title'] ? trim($params['title']) : $nickname.'正在直播',
  488. 'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : lang('我正在直播,快来看看吧'),
  489. 'category' => isset($params['category']) ? intval($params['category']) : 0,
  490. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 0,
  491. 'visible_users' => isset($params['visible_users']) ? trim($params['visible_users']) : '',
  492. 'chat_type' => isset($params['chat_type']) ? intval($params['chat_type']) : 0,
  493. 'chat_status' => isset($params['chat_status']) ? intval($params['chat_status']) : 1,
  494. 'pay_status' => isset($params['pay_status']) ? intval($params['pay_status']) : 1,
  495. 'open_area' => isset($params['open_area']) ? intval($params['open_area']) : 1,
  496. 'view_allow' => isset($params['view_allow']) ? intval($params['view_allow']) : 1,
  497. 'push_url' => isset($params['push_url']) ? trim($params['push_url']) : '',
  498. 'play_url' => isset($params['play_url']) ? trim($params['play_url']) : '',
  499. 'update_time'=> time(),
  500. 'status' =>1,
  501. 'mark'=>1,
  502. ];
  503. if($liveId = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id')){
  504. if(!$this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update($data)){
  505. $this->error = 2042;
  506. return false;
  507. }
  508. }else{
  509. $data['create_time'] = time();
  510. $this->model->where(['user_id'=> $userId,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  511. if(!$liveId = $this->model->insertGetId($data)){
  512. $this->error = 2042;
  513. return false;
  514. }
  515. }
  516. $data['id'] = $liveId;
  517. $data['member'] = MemberModel::where(['id'=> $userId])->select(['id','nickname','avatar','status'])->first();
  518. $data['member'] = $data['member']? $data['member'] : [];
  519. $data['member']['is_follow'] = 1;
  520. if(isset($data['member']['avatar'])){
  521. $data['member']['avatar'] = $data['member']['avatar']? $data['member']['avatar'] : '/images/member/logo.png';
  522. $data['member']['avatar'] = get_image_url($data['member']['avatar']);
  523. }
  524. $this->error = 2041;
  525. return $data;
  526. }
  527. }