LiveService.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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\AccountLogModel;
  15. use App\Models\LiveGiftModel;
  16. use App\Models\LiveModel;
  17. use App\Models\MemberModel;
  18. use App\Models\RewardOrderModel;
  19. use App\Models\VideoCollectModel;
  20. use App\Services\Api\MemberCollectService;
  21. use App\Services\Api\MessageService;
  22. use App\Services\Api\TaskService;
  23. use App\Services\Api\VideoCollectService;
  24. use Darabonba\OpenApi\Models\Config;
  25. use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
  26. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  27. use Illuminate\Support\Facades\DB;
  28. /**
  29. * 在线直播服务管理-服务类
  30. * @author laravel开发员
  31. * @since 2020/11/11
  32. * Class LiveService
  33. * @package App\Services
  34. */
  35. class LiveService extends BaseService
  36. {
  37. // 静态对象
  38. protected static $instance = null;
  39. /**
  40. * 构造函数
  41. * @author laravel开发员
  42. * @since 2020/11/11
  43. * ConfigService constructor.
  44. */
  45. public function __construct()
  46. {
  47. $this->model = new LiveModel();
  48. }
  49. /**
  50. * 静态入口
  51. * @return SmsService|static|null
  52. */
  53. public static function make()
  54. {
  55. if (!self::$instance) {
  56. self::$instance = new static();
  57. }
  58. return self::$instance;
  59. }
  60. /**
  61. * 列表数据
  62. * @param $params
  63. * @param int $pageSize
  64. * @return array
  65. */
  66. public function getDataList($params, $pageSize = 18, $field = '', $userId=0)
  67. {
  68. $where = ['a.mark' => 1,'a.status'=>1,'b.mark'=>1];
  69. $field = $field? $field : 'lev_a.*';
  70. $order = 'rand()';
  71. $model = $this->model->with(['member'])->from('live as a')
  72. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  73. ->where($where)
  74. ->where(function ($query) use ($params) {
  75. $type = isset($params['type']) ? $params['type'] : 0;
  76. if ($type > 0) {
  77. $query->where('a.type', $type);
  78. }
  79. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  80. if ($categoryId > 0) {
  81. $query->where('a.category_id', $categoryId);
  82. }
  83. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  84. if ($uid > 0) {
  85. $query->where('a.user_id', $uid);
  86. }
  87. })
  88. ->where(function ($query) use ($params) {
  89. $keyword = isset($params['kw']) ? $params['kw'] : '';
  90. if ($keyword) {
  91. $query->where('a.title', 'like', "%{$keyword}%")
  92. ->orWhere('a.description', 'like', "%{$keyword}%")
  93. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  94. }
  95. })->where(function ($query) use ($userId) {
  96. if ($userId) {
  97. $query->where('a.visible_users', '=', '')
  98. ->orWhere('a.visible_users', 'like',"%{$userId},%")
  99. ->orWhere('a.manage_users', 'like',"%{$userId},%");
  100. }
  101. });
  102. // 推荐的数据
  103. $countModel = clone $model;
  104. $total = $countModel->where(function($query) use($params, $userId){
  105. // 推荐视频数据
  106. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  107. if ($isRecommend > 0) {
  108. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  109. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  110. $category = isset($recommendData['category'])? $recommendData['category'] : []; // 按标签推荐
  111. if($uids){
  112. $query->orWhere(function($query) use($uids){
  113. $query->whereIn('a.user_id', $uids);
  114. });
  115. }
  116. if($category){
  117. $query->orWhere(function($query) use($category){
  118. $query->whereIn('a.category_id', $category);
  119. });
  120. }
  121. }
  122. })->count('a.id');
  123. if($total > 0){
  124. // 关联推荐数据
  125. $list = $countModel->selectRaw($field)
  126. ->orderByRaw($order)
  127. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  128. }else{
  129. // 默认推荐数据
  130. $list = $model->selectRaw($field)
  131. ->orderByRaw($order)
  132. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  133. }
  134. $list = $list ? $list->toArray() : [];
  135. if ($list && $list['data']) {
  136. foreach ($list['data'] as &$item) {
  137. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  138. $member = isset($item['member'])? $item['member'] : [];
  139. if($member){
  140. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  141. }
  142. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  143. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  144. $item['member'] = $member;
  145. }
  146. }
  147. return [
  148. 'pageSize' => $pageSize,
  149. 'total' => isset($list['total']) ? $list['total'] : 0,
  150. 'list' => isset($list['data']) ? $list['data'] : []
  151. ];
  152. }
  153. /**
  154. * 首页推荐数据
  155. * @param $params
  156. * @param int $pageSize
  157. * @param string $field
  158. * @param int $userId
  159. * @return array
  160. */
  161. public function getIndexList($params, $pageSize = 18, $field = '', $userId=0)
  162. {
  163. $where = ['a.mark' => 1,'a.status'=>1,'b.mark'=>1];
  164. $field = $field? $field : 'lev_a.*';
  165. $order = 'rand()';
  166. $model = $this->model->with(['member'])->from('live as a')
  167. ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
  168. ->where($where)
  169. ->where(function ($query) use ($params) {
  170. $type = isset($params['type']) ? $params['type'] : 0;
  171. if ($type > 0) {
  172. $query->where('a.type', $type);
  173. }
  174. $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
  175. if ($categoryId > 0) {
  176. $query->where('a.category_id', $categoryId);
  177. }
  178. $uid = isset($params['user_id']) ? $params['user_id'] : 0;
  179. if ($uid > 0) {
  180. $query->where('a.user_id', $uid);
  181. }
  182. })
  183. ->where(function ($query) use ($params) {
  184. $keyword = isset($params['kw']) ? $params['kw'] : '';
  185. if ($keyword) {
  186. $query->where('a.title', 'like', "%{$keyword}%")
  187. ->orWhere('a.description', 'like', "%{$keyword}%")
  188. ->orWhere('b.nickname', 'like', "%{$keyword}%");
  189. }
  190. })->where(function ($query) use ($userId) {
  191. if ($userId) {
  192. $query->where('a.visible_users', '=', '')
  193. ->orWhere('a.visible_users', 'like',"%{$userId},%")
  194. ->orWhere('a.manage_users', 'like',"%{$userId},%");
  195. }
  196. });
  197. // 推荐的数据
  198. $countModel = clone $model;
  199. $total = $countModel->where(function($query) use($params, $userId){
  200. // 推荐视频数据
  201. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  202. if ($isRecommend > 0) {
  203. $recommendData = VideoCollectService::make()->getRecommendData($userId);
  204. $uids = isset($recommendData['uids'])? $recommendData['uids'] : []; // 按用户推荐
  205. $category = isset($recommendData['category'])? $recommendData['category'] : []; // 按标签推荐
  206. if($uids){
  207. $query->orWhere(function($query) use($uids){
  208. $query->whereIn('a.user_id', $uids);
  209. });
  210. }
  211. if($category){
  212. $query->orWhere(function($query) use($category){
  213. $query->whereIn('a.category_id', $category);
  214. });
  215. }
  216. }
  217. })->count('a.id');
  218. if($total > 0){
  219. // 关联推荐数据
  220. $list = $countModel->selectRaw($field)
  221. ->orderByRaw($order)
  222. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  223. $list = $list ? $list->toArray() : [];
  224. $list = isset($list['data']) && $list['data']? $list['data'] :[];
  225. }
  226. if($total < $pageSize){
  227. // 默认推荐数据
  228. $list1 = $model->where(function($query) use($countModel){
  229. $ids = $countModel->pluck('a.id');
  230. if($ids){
  231. $query->whereNotIn('a.id', $ids);
  232. }
  233. })->selectRaw($field)
  234. ->orderByRaw($order)
  235. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  236. $list1 = $list1 ? $list1->toArray() : [];
  237. $list1 = isset($list1['data']) && $list1['data']? $list1['data'] :[];
  238. $list = array_merge($list, $list1);
  239. }
  240. if ($list) {
  241. foreach ($list as &$item) {
  242. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  243. $member = isset($item['member'])? $item['member'] : [];
  244. if($member){
  245. $member['avatar'] = isset($member['avatar']) && $member['avatar']? get_image_url($member['avatar']) : get_image_url('/images/member/logo.png');
  246. }
  247. $item['like_num'] = isset($item['like_num']) && $item['like_num']? intval($item['like_num']) : 0;
  248. $item['views'] = isset($item['views']) && $item['views']? intval($item['views']) : 0;
  249. $item['member'] = $member;
  250. }
  251. }
  252. return [
  253. 'pageSize' => $pageSize,
  254. 'total' => count($list),
  255. 'list' => $list? $list : []
  256. ];
  257. }
  258. /**
  259. * 详情
  260. * @param $id
  261. * @param $userId
  262. * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
  263. */
  264. public function getInfo($id, $userId)
  265. {
  266. $where = ['mark'=>1];
  267. if($id>0){
  268. $where['id'] = $id;
  269. }
  270. $info = $this->model->with(['member'])->where($where)->orderBy('create_time','desc')->first();
  271. $info = $info? $info->toArray() : [];
  272. if($info && isset($info['member'])){
  273. $id = $info['id'];
  274. if(isset($info['member']['avatar'])){
  275. $info['member']['avatar'] = $info['member']['avatar']? $info['member']['avatar'] : '/images/member/logo.png';
  276. $info['member']['avatar'] = get_image_url($info['member']['avatar']);
  277. }
  278. if($info['user_id'] == $userId){
  279. $info['is_follow'] = 1;
  280. }else{
  281. $checkFollow = MemberCollectService::make()->checkCollect($userId, $info['user_id'], 1);
  282. $info['is_follow'] = $checkFollow? 1 : 0;
  283. $info['member']['is_follow'] = $checkFollow? 1 : 0;
  284. }
  285. // 用户资料
  286. $liveUserId = isset($info['user_id'])? $info['user_id'] : 0;
  287. $info['member']['fans'] = (int)MemberCollectService::make()->getCount($liveUserId, 1,1); // 粉丝数量(被关注)
  288. $info['member']['follow'] = (int)MemberCollectService::make()->getCount($liveUserId, 1,2); // 关注数量
  289. // 观看权限
  290. $info['view_limit'] = 0;
  291. if($info['status'] == 2){
  292. $createTime = $info['create_time']? strtotime($info['create_time']) : time();
  293. $time = $info['end_time']? intval($info['end_time'] - $createTime) : intval(time() - $createTime);
  294. $info['end_time'] = $info['end_time']? $info['end_time'] : time();
  295. $info['live_hour'] = $time > 3600 ? intval($time/3600) : 0;
  296. $info['live_minute'] = $time%3600? intval($time%3600/60) : 0;
  297. $info['time_text'] = $createTime<(time() - 86400)? date('m-d H:i', $createTime) : date('H:i', $createTime);
  298. $info['end_time_text'] = $info['end_time']<(time() - 86400)? date('m-d H:i', $info['end_time']) : date('H:i', $info['end_time']);
  299. $info['fans_num'] = MemberCollectService::make()->getViewFansCountByType($info['user_id'], $id, 2);
  300. $info['new_fans'] = MemberCollectService::make()->getNewFansCount($info['user_id'], $id,2, $createTime);
  301. }
  302. // 浏览历史
  303. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1, 2)){
  304. $data = [
  305. 'user_id'=> $userId,
  306. 'type'=> 1,
  307. 'source_type'=> 2,
  308. 'collect_id'=> $id,
  309. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  310. 'tags'=> isset($info['tags'])? $info['tags'] : '',
  311. 'create_time'=> time(),
  312. 'status'=> 1,
  313. ];
  314. VideoCollectModel::insert($data);
  315. RedisService::clear("caches:videos:recommend:{$userId}_2");
  316. RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
  317. RedisService::clear("caches:live:users_{$id}_{$userId}");
  318. }
  319. // 更新播放量
  320. if(!RedisService::get("caches:live:player:{$userId}_{$id}")){
  321. $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  322. RedisService::set("caches:live:player:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(600, 1800));
  323. $info['views'] += 1;
  324. // 观看直播任务
  325. if($liveUserId != $userId){
  326. TaskService::make()->updateTask($userId,1, $id,1);
  327. }
  328. }
  329. // 结束直播
  330. $createTime = $info['create_time']? strtotime($info['create_time']) : time();
  331. if($createTime <= time() - 2 * 86400 && $info['status'] == 1){
  332. $this->model->where(['id'=> $id])->update(['status'=>2,'end_time'=>time(),'update_time'=>time()]);
  333. $info['status'] = 2;
  334. }
  335. }
  336. return $info;
  337. }
  338. /**
  339. * 获取直播间在线观众数据
  340. * @param $params
  341. * @param $pageSize
  342. * @param int $userId
  343. * @return array|mixed
  344. */
  345. public function getUserList($params, $pageSize=200, $userId=0)
  346. {
  347. $liveId = isset($params['live_id'])? $params['live_id'] : 0;
  348. $cachekey = "caches:live:users_{$liveId}_{$userId}";
  349. $datas = RedisService::get($cachekey);
  350. if($datas || RedisService::exists($cachekey)){
  351. return $datas? $datas : [
  352. 'current' => ['user_id'=> $userId,'rank'=>0,'reward_total'=>0],
  353. 'rank_limit'=> 200,
  354. 'total' => 0,
  355. 'list' => []
  356. ];
  357. }
  358. $rankNum = ConfigService::make()->getConfigByCode('live_show_rank', 200);
  359. $rankNum = $rankNum>20 && $rankNum <= 300? $rankNum : 200;
  360. $model = MemberModel::from('member as a')
  361. ->leftJoin('video_collect as b',function($join) use($liveId){
  362. $join->on('a.id','=','b.user_id')
  363. ->where(['b.type'=>1,'b.collect_id'=> $liveId,'b.source_type'=>2,'b.is_online'=>1,'b.status'=>1,'b.mark'=>1]);
  364. })
  365. ->where(['a.status'=>1,'a.mark'=>1])
  366. ->where('b.id','>', 0)
  367. ->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']);
  368. $countModel = clone $model;
  369. $total = $countModel->distinct('a.id')->count('a.id');
  370. $datas = $model->groupBy('a.id')
  371. ->orderBy('b.reward_total','desc')
  372. ->orderBy('b.create_time','asc')
  373. ->limit($rankNum)->get();
  374. $datas = $datas? $datas->toArray() : [];
  375. $rankData = ['user_id'=> $userId,'rank'=>0,'reward_total'=>0];
  376. if ($datas) {
  377. foreach ($datas as $k=> &$item) {
  378. $item['time_text'] = isset($item['create_time']) ? dateFormat($item['create_time'], 'Y-m-d H:i') : '';
  379. $item['avatar'] = isset($item['avatar']) && $item['avatar']? get_image_url($item['avatar']) : get_image_url('/images/member/logo.png');
  380. $item['reward_total'] = isset($item['reward_total']) && $item['reward_total']? format_num(intval($item['reward_total'])) : 0;
  381. if($item['id'] == $userId){
  382. $rankData['rank'] = ($k+1)>$rankNum? 0 : ($k+1);
  383. $rankData['reward_total'] = $item['reward_total']? format_num(intval($item['reward_total'])) : 0;
  384. }
  385. }
  386. // 用户排名在200名外的数据
  387. if($rankData['rank'] <= 0 && $rankData['reward_total']<=0 && count($datas) > $rankNum){
  388. $rewardTotal = VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'collect_id'=> $liveId,'source_type'=>2,'status'=>1,'mark'=>1])
  389. ->value('reward_total');
  390. $rankData['reward_total'] = $rewardTotal? format_num(intval($rewardTotal)) : 0;
  391. }
  392. $totalText = $total>=10000 && $total<=100000? intval($total/10000).'万' : intval($total);
  393. RedisService::set($cachekey, ['current' => $rankData,'rank_limit'=> $rankNum,'total'=> $total,'total_text'=>$totalText,'list'=> $datas], rand(3,5));
  394. }
  395. return [
  396. 'current' => $rankData,
  397. 'rank_limit'=> $rankNum,
  398. 'total' => $total,
  399. 'total_text' => $total>=10000 && $total<=100000? intval($total/10000).'万' : intval($total),
  400. 'list' => $datas
  401. ];
  402. }
  403. /**
  404. * 获取直播间礼物列表
  405. * @param $params
  406. * @param $pageSize
  407. * @param int $userId
  408. * @return array|mixed
  409. */
  410. public function getGiftList($params, $pageSize=30, $userId=0)
  411. {
  412. $page = request()->post('page', 1);
  413. $liveId = isset($params['live_id'])? $params['live_id'] : 0;
  414. $cachekey = "caches:live:gift_{$liveId}_{$userId}_{$page}";
  415. $datas = RedisService::get($cachekey);
  416. if($datas || RedisService::exists($cachekey)){
  417. return $datas? $datas : [
  418. 'pageSize'=> $pageSize,
  419. 'total' => 0,
  420. 'list' => []
  421. ];
  422. }
  423. $list = LiveGiftModel::from('live_gift as a')
  424. ->where(['a.status'=>1,'a.mark'=>1])
  425. ->select(['a.id','a.name','a.code','a.money','a.icon','a.file_url','a.sort'])
  426. ->orderBy('a.sort','desc')
  427. ->orderBy('a.create_time','asc')
  428. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  429. $list = $list ? $list->toArray() : [];
  430. if ($list && $list['data']) {
  431. foreach ($list['data'] as &$item) {
  432. $item['icon'] = isset($item['icon']) && $item['icon']? get_image_url($item['icon']) : get_image_url('/images/gift/gift.png');
  433. $item['file_url'] = isset($item['file_url']) && $item['file_url']? get_image_url($item['file_url']) : '';
  434. }
  435. RedisService::set($cachekey, [
  436. 'pageSize' => $pageSize,
  437. 'total' => isset($list['total']) ? $list['total'] : 0,
  438. 'list' => isset($list['data']) ? $list['data'] : []
  439. ], rand(5,10));
  440. }
  441. return [
  442. 'pageSize' => $pageSize,
  443. 'total' => isset($list['total']) ? $list['total'] : 0,
  444. 'list' => isset($list['data']) ? $list['data'] : []
  445. ];
  446. }
  447. /**
  448. * 更新播放浏览历史
  449. * @param $userId 用户ID
  450. * @param $id 视频ID
  451. * @return false
  452. */
  453. public function updatePlay($userId, $id)
  454. {
  455. // 浏览历史
  456. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $id, 1,2)){
  457. $data = [
  458. 'user_id'=> $userId,
  459. 'type'=> 1,
  460. 'source_type'=> 2,
  461. 'collect_id'=> $id,
  462. 'category_id'=> isset($info['category'])? $info['category'] : 0,
  463. 'collect_uid'=> isset($info['user_id'])? $info['user_id'] : 0,
  464. 'create_time'=> time(),
  465. 'status'=> 1,
  466. ];
  467. VideoCollectModel::insert($data);
  468. RedisService::set("caches:videos:collect:temp_{$userId}_{$id}_1_2", $data, rand(10,30));
  469. RedisService::clear("caches:videos:recommend:{$userId}_1_2");
  470. RedisService::clear("caches:member:fans:{$userId}_{$id}_2");
  471. RedisService::clear("caches:live:users_{$id}_{$userId}");
  472. }
  473. // 更新播放量
  474. if(!RedisService::get("caches:player:live:{$userId}_{$id}")){
  475. $info = $this->model->from('live as a')
  476. ->where(['a.id'=> $id,'a.mark'=>1])
  477. ->select(['a.id','a.category','a.user_id'])
  478. ->first();
  479. if(empty($info)){
  480. return false;
  481. }
  482. $this->model->where(['id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  483. RedisService::set("caches:player:live:{$userId}_{$id}", ['user_id'=> $userId,'id'=>$id], rand(6*3600, 86400));
  484. // 观看直播任务
  485. if($info['user_id'] != $userId){
  486. TaskService::make()->updateTask($userId,1, $id, 1);
  487. }
  488. }
  489. $this->error = 1010;
  490. return true;
  491. }
  492. /**
  493. * 缓存数据
  494. * @param $id
  495. * @param int $status
  496. * @return array|mixed
  497. */
  498. public function getCacheInfo($id, $status=0)
  499. {
  500. $cacheKey = "caches:live:info:{$id}_{$status}";
  501. $info = RedisService::get($cacheKey);
  502. if($info){
  503. return $info;
  504. }
  505. $where = ['id' => $id, 'mark' => 1];
  506. if($status){
  507. $where['status'] = $status;
  508. }
  509. $info = $this->model->where($where)
  510. ->select(['id','user_id','create_time','end_time','views','reward_num','reward_total','like_num'])
  511. ->first();
  512. $info = $info? $info->toArray() : [];
  513. if($info){
  514. RedisService::set($cacheKey, $info, rand(3, 5));
  515. }
  516. return $info;
  517. }
  518. /**
  519. * 状态设置
  520. * @return bool
  521. */
  522. public function status()
  523. {
  524. $id = request()->post('id', 0);
  525. $status = request()->post('status', 2);
  526. if ($id && !$info = $this->getCacheInfo($id)) {
  527. $this->error = 2981;
  528. return false;
  529. }
  530. $updateData = ['status'=>$status, 'update_time'=> time()];
  531. if($status == 2){
  532. $updateData['end_time'] = time();
  533. }
  534. if($this->model->where(['id'=> $id,'mark'=>1])->update($updateData)){
  535. $this->error = 1002;
  536. if($status == 2){
  537. $createTime = $info['create_time']? strtotime($info['create_time']) : time();
  538. $time = intval(time() - $createTime);
  539. $info['status'] = $status;
  540. $info['end_time'] = time();
  541. $info['live_hour'] = $time > 3600 ? intval($time/3600) : 0;
  542. $info['live_minute'] = $time%3600? intval($time%3600/60) : 0;
  543. $info['time_text'] = $createTime<(time() - 86400)? date('m-d H:i', $createTime) : date('H:i', $createTime);
  544. $info['end_time_text'] = $info['end_time']<(time() - 86400)? date('m-d H:i', $info['end_time']) : date('H:i', $info['end_time']);
  545. $info['fans_num'] = MemberCollectService::make()->getViewFansCountByType($info['user_id'], $id, 2);
  546. $info['new_fans'] = MemberCollectService::make()->getNewFansCount($info['user_id'], $id,2, $createTime);
  547. }
  548. return $info;
  549. }
  550. $this->error = 1003;
  551. return true;
  552. }
  553. /**
  554. * 离开直播间
  555. * @return bool
  556. */
  557. public function leave($userId)
  558. {
  559. $id = request()->post('id', 0);
  560. if ($id && !$info = $this->getCacheInfo($id)) {
  561. $this->error = 2981;
  562. return false;
  563. }
  564. $updateData = ['is_online'=> 2, 'update_time'=> time()];
  565. VideoCollectModel::where(['user_id'=>$userId,'type'=>1,'source_type'=>2,'collect_id'=> $id,'mark'=>1])->update($updateData);
  566. $this->error = 1003;
  567. return true;
  568. }
  569. /**
  570. * 点赞
  571. * @return bool
  572. */
  573. public function like($userId, $params)
  574. {
  575. $liveId = isset($params['live_id'])? intval($params['live_id']) : 0;
  576. $num = isset($params['num'])? intval($params['num']) : 0;
  577. if($liveId<=0 || $num<=0){
  578. return false;
  579. }
  580. $info = $this->getCacheInfo($liveId);
  581. if(empty($info)){
  582. return false;
  583. }
  584. if($this->model->where(['id'=> $liveId])->update(['like_num'=>DB::raw("like_num + {$num}"),'update_time'=>time()])){
  585. $this->error = 1017;
  586. return ['id'=>$liveId,'like_num'=>intval($info['like_num'] + $num),'nun'=>$num];
  587. }
  588. return false;
  589. }
  590. /**
  591. * 获取直播推流/拉流地址
  592. * @param $streamName
  593. * @param string $appName
  594. * @param string $playType
  595. * @param int $expireTime
  596. * @return array
  597. */
  598. public function getLiveUrl($streamName, $appName = 'xlapp', $playType = 'rtmp', $expireTime = 1440)
  599. {
  600. $cachekey = "caches:live:urls:{$streamName}_{$appName}_{$playType}";
  601. $datas = RedisService::get($cachekey);
  602. if($datas){
  603. return $datas;
  604. }
  605. $playUrls = [];
  606. //未开启鉴权Key的情况下
  607. $pushDomain = ConfigService::make()->getConfigByCode('live_push_url');
  608. $playDomain = ConfigService::make()->getConfigByCode('live_play_url');
  609. $pushKey = ConfigService::make()->getConfigByCode('push_url_key');
  610. $playKey = ConfigService::make()->getConfigByCode('play_url_key');
  611. $timeStamp = time() + $expireTime * 60;
  612. if ($pushKey == '') {
  613. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName;
  614. } else {
  615. $sstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $pushKey;
  616. $md5hash = md5($sstring);
  617. $pushUrl = 'rtmp://' . $pushDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $md5hash;
  618. }
  619. if ($playKey == '') {
  620. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName;
  621. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv';
  622. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8';
  623. } else {
  624. $rtmpSstring = '/' . $appName . '/' . $streamName . '-' . $timeStamp . '-0-0-' . $playKey;
  625. $rtmpMd5hash = md5($rtmpSstring);
  626. $playUrls['rtmp'] = 'rtmp://' . $playDomain . '/' . $appName . '/' . $streamName . '?auth_key=' . $timeStamp . '-0-0-' . $rtmpMd5hash;
  627. $flvSstring = '/' . $appName . '/' . $streamName . '.flv-' . $timeStamp . '-0-0-' . $playKey;
  628. $flvMd5hash = md5($flvSstring);
  629. $playUrls['flv'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.flv?auth_key=' . $timeStamp . '-0-0-' . $flvMd5hash;
  630. $hlsSstring = '/' . $appName . '/' . $streamName . '.m3u8-' . $timeStamp . '-0-0-' . $playKey;
  631. $hlsMd5hash = md5($hlsSstring);
  632. $playUrls['hls'] = 'http://' . $playDomain . '/' . $appName . '/' . $streamName . '.m3u8?auth_key=' . $timeStamp . '-0-0-' . $hlsMd5hash;
  633. }
  634. $datas = [
  635. 'push_url' => $pushUrl,
  636. 'play_url' => isset($playUrls[$playType]) ? $playUrls[$playType] : '',
  637. 'play_urls' => $playUrls,
  638. ];
  639. RedisService::set($cachekey, $datas, 1800);
  640. return $datas;
  641. }
  642. /**
  643. * 创建直播间
  644. * @param $userId 直逼用户
  645. * @param $params
  646. * @return array|false
  647. */
  648. public function create($userId, $params)
  649. {
  650. $liveLevel = ConfigService::make()->getConfigByCode('live_open_level');
  651. $liveLevel = $liveLevel > 0 ? $liveLevel : 0;
  652. $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])->select(['id', 'nickname', 'member_level', 'status'])->first();
  653. $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
  654. $nickname = isset($userInfo['nickname']) ? $userInfo['nickname'] : '';
  655. $memberLevel = isset($userInfo['member_level']) ? $userInfo['member_level'] : 0;
  656. if (empty($userInfo) || $status != 1) {
  657. $this->error = 2024;
  658. return false;
  659. }
  660. if($memberLevel < $liveLevel){
  661. $this->error = 2040;
  662. return false;
  663. }
  664. // 验证是否有开播中断播的继续播
  665. $data = [
  666. 'type' => isset($params['type']) ? intval($params['type']) : 1,
  667. 'user_id' => $userId,
  668. 'title' => isset($params['title']) && $params['title'] ? trim($params['title']) : $nickname.'正在直播',
  669. 'description' => isset($params['description']) && $params['description'] ? trim($params['description']) : lang('我正在直播,快来看看吧'),
  670. 'category' => isset($params['category']) ? intval($params['category']) : 0,
  671. 'visible_type' => isset($params['visible_type']) ? intval($params['visible_type']) : 0,
  672. 'visible_users' => isset($params['visible_users']) ? trim($params['visible_users']) : '',
  673. 'chat_type' => isset($params['chat_type']) ? intval($params['chat_type']) : 0,
  674. 'chat_status' => isset($params['chat_status']) ? intval($params['chat_status']) : 1,
  675. 'pay_status' => isset($params['pay_status']) ? intval($params['pay_status']) : 1,
  676. 'open_area' => isset($params['open_area']) ? intval($params['open_area']) : 1,
  677. 'view_allow' => isset($params['view_allow']) ? intval($params['view_allow']) : 1,
  678. 'push_url' => isset($params['push_url']) ? trim($params['push_url']) : '',
  679. 'play_url' => isset($params['play_url']) ? trim($params['play_url']) : '',
  680. 'update_time'=> time(),
  681. 'status' =>1,
  682. 'mark'=>1,
  683. ];
  684. if($liveId = $this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->orderBy('create_time','desc')->value('id')){
  685. if(!$this->model->where(['user_id'=> $userId,'status'=>1,'mark'=>1])->update($data)){
  686. $this->error = 2042;
  687. return false;
  688. }
  689. }else{
  690. $data['create_time'] = time();
  691. $this->model->where(['user_id'=> $userId,'mark'=>1])->update(['status'=>2,'update_time'=>time()]);
  692. if(!$liveId = $this->model->insertGetId($data)){
  693. $this->error = 2042;
  694. return false;
  695. }
  696. }
  697. $data['id'] = $liveId;
  698. $data['member'] = MemberModel::where(['id'=> $userId])->select(['id','nickname','avatar','status'])->first();
  699. $data['member'] = $data['member']? $data['member'] : [];
  700. $data['member']['is_follow'] = 1;
  701. if(isset($data['member']['avatar'])){
  702. $data['member']['avatar'] = $data['member']['avatar']? $data['member']['avatar'] : '/images/member/logo.png';
  703. $data['member']['avatar'] = get_image_url($data['member']['avatar']);
  704. }
  705. $this->error = 2041;
  706. return $data;
  707. }
  708. /**
  709. * 直播打赏
  710. * @param $userId 打赏用户ID
  711. * @param $params 参数
  712. * @return bool
  713. */
  714. public function reward($userId, $params)
  715. {
  716. $liveId = isset($params['live_id'])? intval($params['live_id']) : 0;
  717. $giftId = isset($params['gift_id'])? intval($params['gift_id']) : 0;
  718. $liveInfo = $this->model->with(['member'])->where(['id'=> $liveId,'mark'=>1])
  719. ->select(['id','user_id','category','pay_status','reward_num','status'])
  720. ->first();
  721. $status = isset($liveInfo['status'])? $liveInfo['status'] : 0;
  722. $payStatus = isset($liveInfo['pay_status'])? $liveInfo['pay_status'] : 0;
  723. $liveUserId = isset($liveInfo['user_id'])? $liveInfo['user_id'] : 0;
  724. $liveMember = isset($liveInfo['member'])? $liveInfo['member'] : [];
  725. $liveUserUsdt = isset($liveMember['usdt'])? $liveMember['usdt'] : 0;
  726. $bonusUserId = isset($liveMember['parent_id'])? $liveMember['parent_id'] : 0;
  727. if(empty($liveInfo) || empty($liveMember) || $status != 1){
  728. $this->error = 2301;
  729. return false;
  730. }
  731. if($payStatus!=1){
  732. $this->error = 2302;
  733. return false;
  734. }
  735. $giftInfo = LiveGiftModel::where(['id'=> $giftId,'status'=>1,'mark'=>1])
  736. ->select(['id','name','money'])
  737. ->first();
  738. $money = isset($giftInfo['money'])? intval($giftInfo['money']) : 0;
  739. if(empty($giftInfo) || $money<=0){
  740. $this->error = 2303;
  741. return false;
  742. }
  743. // 用户信息
  744. $userInfo = MemberModel::where(['id'=> $userId,'mark'=>1])
  745. ->select(['id','nickname','parent_id','point_id','balance','status'])
  746. ->first();
  747. $balance = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0;
  748. if($balance < $money){
  749. $this->error = 2304;
  750. return false;
  751. }
  752. // 直播推荐人(经纪人)
  753. $bounsUserInfo = [];
  754. $bonusUserLevel = 0;
  755. $bonusUserUsdt = 0;
  756. $bonusUserWaitScore = 0;
  757. if($bonusUserId){
  758. $bounsUserInfo = MemberModel::where(['id'=> $bonusUserId,'status'=>1,'mark'=>1])
  759. ->select(['id','nickname','parent_id','point_id','member_level','usdt','balance','wait_score','status'])
  760. ->first();
  761. $bonusUserLevel = isset($bounsUserInfo['member_level'])? $bounsUserInfo['member_level'] : 0;
  762. $bonusUserUsdt = isset($bounsUserInfo['usdt'])? $bounsUserInfo['usdt'] : 0;
  763. $bonusUserWaitScore = isset($bounsUserInfo['wait_score'])? $bounsUserInfo['wait_score'] : 0;
  764. }
  765. // TODO 打赏订单
  766. $orderNo = get_order_num('RW');
  767. $awardLevel = ConfigService::make()->getConfigByCode('live_award_level',5); // 推荐奖励等级
  768. $awardLevel = $awardLevel>=1 && $awardLevel<=30? $awardLevel : 5;
  769. $awardRate = ConfigService::make()->getConfigByCode('award_rewad_rate',10); // 推荐奖励待返积分比例%
  770. $awardRate = $awardRate>=0 && $awardLevel<100? $awardRate : 10;
  771. $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100); // 星豆价格
  772. $xdPrice = $xdPrice>0.01 && $xdPrice <=10000? $xdPrice : 100;
  773. $usdt = moneyFormat($money/$xdPrice,2);
  774. // 推荐佣金
  775. $bonusWaitScore = $awardRate? moneyFormat($money * $awardRate/100, 4) : 0;
  776. DB::beginTransaction();
  777. $order = [
  778. 'live_id'=> $liveId,
  779. 'user_id'=> $userId,
  780. 'live_user_id'=> $liveUserId,
  781. 'gift_id'=> $giftId,
  782. 'order_no'=> $orderNo,
  783. 'type'=> 1,
  784. 'money'=> $money,
  785. 'usdt'=> $usdt,
  786. 'bonus_rate'=> $awardRate? $awardRate : 0,
  787. 'bonus_wait_score'=> $bonusUserId>0? $bonusWaitScore : 0,
  788. 'bonus_uid'=> $bonusUserId,
  789. 'bonus_status'=> $bonusUserLevel >= $awardLevel? 1 : 2,
  790. 'create_time'=> time(),
  791. 'status'=> 1,
  792. 'mark'=>1
  793. ];
  794. if(!$orderId = RewardOrderModel::insertGetId($order)){
  795. DB::rollBack();
  796. $this->error = 2305;
  797. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'创建订单失败'], 600);
  798. return false;
  799. }
  800. // 扣除打赏用户余额
  801. $updateData = ['balance'=>DB::raw("balance - {$money}"),'update_time'=>time()];
  802. if(!MemberModel::where(['id'=> $userId,'mark'=>1])->update($updateData)){
  803. DB::rollBack();
  804. $this->error = 2305;
  805. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'账户扣款失败'], 600);
  806. return false;
  807. }
  808. // 明细
  809. $log = [
  810. 'user_id'=> $userId,
  811. 'source_id'=> $liveId,
  812. 'source_order_no'=> $orderNo,
  813. 'user_type'=> 1,
  814. 'type'=>1,
  815. 'coin_type'=> 2,
  816. 'money'=> -$usdt,
  817. 'date'=> date('Y-m-d'),
  818. 'actual_money'=> -$money,
  819. 'balance'=> $balance,
  820. 'create_time'=> time(),
  821. 'remark'=> '礼物打赏消费',
  822. 'status'=> 1,
  823. 'mark'=> 1,
  824. ];
  825. if(!AccountLogModel::insertGetId($log)){
  826. DB::rollBack();
  827. $this->error = 2305;
  828. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'账户明细处理失败'], 600);
  829. return false;
  830. }
  831. // TODO 推荐奖励(等级需要超过)
  832. $realUsdt = $usdt;
  833. $dateTime = date('Y-m-d H:i:s');
  834. $liveName = isset($liveMember['nickname']) && $liveMember['nickname']? $liveMember['nickname'] : $liveUserId;
  835. $userNickname = isset($userInfo['nickname']) && $userInfo['nickname']? $userInfo['nickname'] : $userId;
  836. if($bonusUserId>0 && $bonusUserLevel >= $awardLevel && $bonusWaitScore>0){
  837. // 账户变动
  838. $updateData = ['wait_score'=>DB::raw("wait_score + {$bonusWaitScore}"),'update_time'=>time()];
  839. if(!MemberModel::where(['id'=> $bonusUserId,'mark'=>1])->update($updateData)){
  840. DB::rollBack();
  841. $this->error = 2305;
  842. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'update'=>$updateData,'wait_score'=>$bonusWaitScore,'error'=>'主播推荐奖励处理失败'], 600);
  843. return false;
  844. }
  845. // 明细
  846. $log = [
  847. 'user_id'=> $bonusUserId,
  848. 'source_id'=> $liveId,
  849. 'source_order_no'=> $orderNo,
  850. 'user_type'=> 1,
  851. 'type'=> 9,
  852. 'coin_type'=> 5,
  853. 'money'=> $bonusWaitScore,
  854. 'date'=> date('Y-m-d'),
  855. 'actual_money'=> $bonusWaitScore,
  856. 'balance'=> $bonusUserWaitScore,
  857. 'create_time'=> time(),
  858. 'remark'=> '打赏推荐奖励',
  859. 'status'=> 1,
  860. 'mark'=> 1,
  861. ];
  862. if(!AccountLogModel::insertGetId($log)){
  863. DB::rollBack();
  864. $this->error = 2305;
  865. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'主播推荐奖励明细处理失败'], 600);
  866. return false;
  867. }
  868. // 消息
  869. MessageService::make()->pushMessage($userId, lang('打赏推荐奖励通知'), lang('打赏推荐奖励通知内容',['time'=> $dateTime,'score'=>$bonusWaitScore,'live_name'=>$liveName]),3);
  870. //$realUsdt = moneyFormat($usdt - $bonusUsdt, 4);
  871. }
  872. // 被打赏主播账户USDT账户进账
  873. $updateData = ['usdt'=>DB::raw("usdt + {$realUsdt}"),'update_time'=>time()];
  874. if(!MemberModel::where(['id'=> $liveUserId,'mark'=>1])->update($updateData)){
  875. DB::rollBack();
  876. $this->error = 2305;
  877. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'打赏进账处理失败'], 600);
  878. return false;
  879. }
  880. // 明细
  881. $log = [
  882. 'user_id'=> $liveUserId,
  883. 'source_id'=> $liveId,
  884. 'source_order_no'=> $orderNo,
  885. 'user_type'=> 1,
  886. 'type'=> 1,
  887. 'coin_type'=> 1,
  888. 'money'=> $realUsdt,
  889. 'date'=> date('Y-m-d'),
  890. 'actual_money'=> $realUsdt,
  891. 'balance'=> $liveUserUsdt,
  892. 'create_time'=> time(),
  893. 'remark'=> '礼物打赏收入',
  894. 'status'=> 1,
  895. 'mark'=> 1,
  896. ];
  897. if(!AccountLogModel::insertGetId($log)){
  898. DB::rollBack();
  899. $this->error = 2305;
  900. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'打赏进账明细处理失败'], 600);
  901. return false;
  902. }
  903. // TODO 浏览用户更新打赏统计
  904. if(!VideoCollectService::make()->getCollectCacheInfo($userId, $liveId, 1,2)){
  905. $data = [
  906. 'user_id'=> $userId,
  907. 'type'=> 1,
  908. 'source_type'=> 2,
  909. 'collect_id'=> $liveId,
  910. 'reward_total'=> $money,
  911. 'category_id'=> isset($liveInfo['category'])? $liveInfo['category'] : 0,
  912. 'collect_uid'=> isset($liveInfo['user_id'])? $liveInfo['user_id'] : 0,
  913. 'create_time'=> time(),
  914. 'status'=> 1,
  915. ];
  916. if(!VideoCollectModel::insertGetId($data)){
  917. DB::rollBack();
  918. $this->error = 2305;
  919. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'更新打赏统计失败'], 600);
  920. return false;
  921. }
  922. RedisService::set("caches:videos:collect:temp_{$userId}_{$liveId}_1_2", $data, rand(10,30));
  923. RedisService::clear("caches:videos:recommend:{$userId}_1_2");
  924. RedisService::clear("caches:member:fans:{$userId}_{$liveId}_2");
  925. RedisService::clear("caches:live:users_{$liveId}_{$userId}");
  926. }
  927. // 更新
  928. else if (!VideoCollectModel::where(['user_id'=> $userId,'type'=>1,'source_type'=>2, 'collect_id'=>$liveId])->update(['reward_total'=> DB::raw("reward_total + {$money}"),'update_time'=>time()])){
  929. DB::rollBack();
  930. $this->error = 2305;
  931. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'更新打赏统计失败1'], 600);
  932. return false;
  933. }
  934. // TODO 直播间打赏统计
  935. $updateData = ['reward_total'=>DB::raw("reward_total + {$money}"),'update_time'=>time()];
  936. if(!$this->checkUserReward($userId, $liveId) || $liveInfo['reward_num']<=0){
  937. // 打赏人数统计
  938. $updateData['reward_num'] = DB::raw("reward_num + 1");
  939. }
  940. if(!$this->model->where(['id'=> $liveId])->update($updateData)){
  941. DB::rollBack();
  942. $this->error = 2305;
  943. RedisService::set("caches:live:reward:error_{$userId}", ['data'=> $order,'error'=>'更新打赏统计失败2'], 600);
  944. return false;
  945. }
  946. DB::commit();
  947. // TODO 推送站内消息
  948. MessageService::make()->pushMessage($userId, lang('打赏消费通知'), lang('打赏消费通知内容',['time'=> $dateTime,'money'=>$money,'live_name'=>$liveName]),3);
  949. MessageService::make()->pushMessage($liveUserId, lang('打赏通知'), lang('打赏通知内容',['time'=> $dateTime,'money'=>$money,'nickname'=>$userNickname]),3);
  950. // TODO 打赏直播任务
  951. if($userId != $liveUserId){
  952. TaskService::make()->updateTask($userId,7, $liveId);
  953. }
  954. $this->error = 2306;
  955. return ['id'=>$orderId];
  956. }
  957. /**
  958. * 验证是否打赏过
  959. * @param $userId 打赏用户ID
  960. * @param $liveId 直播间ID
  961. * @return array|mixed
  962. */
  963. public function checkUserReward($userId, $liveId)
  964. {
  965. $cacheKey = "caches:live:reward:{$userId}_{$liveId}";
  966. $data = RedisService::get($cacheKey);
  967. if($data || RedisService::exists($cacheKey)){
  968. return $data;
  969. }
  970. $data = RewardOrderModel::where(['user_id'=> $userId,'live_id'=> $liveId])->value('id');
  971. if($data){
  972. RedisService::set($cacheKey, $data, rand(3600, 7200));
  973. }
  974. return $data;
  975. }
  976. }