Live.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\controller\Common;
  4. use app\common\model\LiveJoin;
  5. use fast\Random;
  6. use app\common\controller\RedisLeaderboard;
  7. use Redis;
  8. use think\Db;
  9. /**
  10. * 直播间接口
  11. */
  12. class Live extends Common
  13. {
  14. protected $noNeedLogin = ['updateLiveInfo','getLiveType','abkd','abab','getGiftInfo'];
  15. protected $noNeedRight = ['*'];
  16. /**
  17. * @throws \think\db\exception\DataNotFoundException
  18. * @throws \think\db\exception\ModelNotFoundException
  19. * @throws \think\exception\DbException
  20. * 余额提现检查
  21. */
  22. public function abkd() {
  23. $weekend = strtotime('monday this week');
  24. exit;
  25. // $user_id = $this->request->request("user_id");
  26. // if(!$user_id) return false;
  27. // $users = \app\common\model\User::field("id,money")->select();
  28. // if($users) foreach($users as $k => $v) {
  29. // $user_id = $v["id"];
  30. // // 统计用户所有兑换记录和提现记录 是否与余额匹配
  31. // $moneylog = \app\common\model\UserMoneyLog::where(["user_id"=>$user_id,"mode"=>"+"])->sum("money");
  32. // $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id"=>$user_id,"status"=>1])->sum("money");
  33. // $withdraw = bcdiv($withdraw,100,2);
  34. // $sum = bcsub($moneylog, $withdraw,2);
  35. // $usermoney = \app\common\model\User::where(["id"=>$user_id])->value("money");
  36. // if($usermoney == $sum) {
  37. // echo "1<br/>";
  38. // } else {
  39. // echo $v['id']."用户余额:".$usermoney;
  40. // echo "记录和:".$sum."<br/>";
  41. //
  42. //// $money = bcsub($usermoney,$sum,2);
  43. //// $time = time();
  44. //// $money>0 && Db::execute("INSERT INTO `hx_user_money_log` (`user_id` , `money` , `mode`, `before` , `after` , `detail`, `createtime`) VALUES ($user_id , $money , '+', $usermoney, $usermoney, '系统补足', $time)");
  45. //
  46. // }
  47. // }
  48. $user_id = 319;
  49. // 统计用户所有兑换记录和提现记录 是否与余额匹配
  50. $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum('value');
  51. $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id" => $user_id, "status" => ["in", [0, 1]]])->sum("money");
  52. $withdraw = bcdiv($withdraw, 100,2);
  53. $sum = bcsub($moneylog, $withdraw,2);
  54. $usermoney = \app\common\model\User::where(["id" => $user_id])->value("money");
  55. if ($usermoney == $sum) {
  56. echo 1;
  57. } else {
  58. echo 'f'.$moneylog;
  59. }
  60. exit;
  61. }
  62. /**
  63. * 声币兑换余额检查
  64. */
  65. public function abab() {
  66. // $user_id = $this->request->request("user_id");
  67. // if(!$user_id) return false;
  68. $users = \app\common\model\User::field("id")->select();
  69. if($users) foreach($users as $k => $v) {
  70. $user_id = $v["id"];
  71. // 获取某个用户所有声币兑换记录
  72. $limit = 5;
  73. $coin = \app\common\model\UserSoundcoinLog::field("id,value,mode,balance,before")->where(["user_id" => $user_id])->limit($limit)->order("createtime", "desc")->select();
  74. if ($coin) {
  75. $coinjian = 0;
  76. $coinjia = 0;
  77. $countkey = count($coin) - 1;
  78. foreach ($coin as $k => $v) {
  79. if ($v["mode"] == "-") {
  80. $coinjian = $coinjian + $v["value"];
  81. } elseif ($v["mode"] == "+") {
  82. $coinjia = $coinjia + $v["value"];
  83. }
  84. $k == 0 && $first = $v["balance"];
  85. $k == $countkey && $last = $v["before"];
  86. }
  87. $usercoin = \app\common\model\User::where(["id" => $user_id])->value("sound_coin");
  88. $change = $coinjian - $coinjia;
  89. if ($first + $change == $last) {
  90. echo "1<br/>";
  91. } else {
  92. echo "用户余额:" . $usercoin;
  93. echo "最初值:" . $first;
  94. echo "改变值:" . $change;
  95. echo "最后余额:" . $last . "<br/>";
  96. }
  97. }
  98. // return true;
  99. }
  100. }
  101. public function getGiftInfo() {
  102. $type_id = $this->request->get('type_id'); // type
  103. $list = \app\common\model\EggGift::field("id,gift_id,price")->where(["jackpot_id"=>$type_id,"is_use"=>0])->select();
  104. $count = 0;
  105. $sum = 0;
  106. if($list) foreach($list as $k => $v) {
  107. $count++;
  108. // $list[$k]["price"] = $v["price"]/100;
  109. }
  110. $res = [];
  111. $res["count"] = $count;
  112. $res["sum"] = \app\common\model\EggGift::where(["jackpot_id"=>$type_id,"is_use"=>0])->sum("price");
  113. // $res["list"] = $list;
  114. $this->success("获取成功!",$res);
  115. }
  116. /**
  117. * 创建直播间
  118. */
  119. public function createLive() {
  120. // 先判断用户主播申请是否已经通过
  121. $anchorModel = new \app\common\model\UserAnchor();
  122. $where = [];
  123. $where["user_id"] = $this->auth->id;
  124. $where["status"] = 1;
  125. $anchorInfo = $anchorModel->where($where)->find();
  126. if(!$anchorInfo) $this->error("请先提交主播申请,并等待审核通过!");
  127. $liveModel = new \app\common\model\Live();
  128. $where = [];
  129. $where["user_id"] = $this->auth->id;
  130. $sqlLiveInfo = $liveModel->where($where)->find();
  131. // // 获取用户信息
  132. // $userModel = new \app\common\model\User();
  133. // $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
  134. // if(!$userInfo) $this->error("用户信息获取失败!");
  135. // 判断直播间是否存在
  136. $redis = new Redis();
  137. $redisconfig = config("redis");
  138. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  139. if ($redisconfig['redis_pwd']) {
  140. $redis->auth($redisconfig['redis_pwd']);
  141. }
  142. if($redisconfig['redis_selectdb'] > 0){
  143. $redis->select($redisconfig['redis_selectdb']);
  144. }
  145. if(!$sqlLiveInfo) { // sql中不存在直播间信息
  146. $live_type = $anchorInfo["type_id"]; // 直播间类型(情感类娱乐类 等)
  147. $live_notice = "Hi!欢迎加入我的直播间"; // 直播间公告
  148. $live_notice_detail = "Hi!欢迎加入我的直播间Hi!欢迎加入我的直播间"; // 直播间公告详情
  149. if (!$live_type || !$live_notice) {
  150. $this->error(__('Invalid parameters'));
  151. }
  152. $live_ids = $liveModel->column("live_id");
  153. // 创建直播间ID (临时ID四位,直播间数不超过8999)
  154. $live_id = $this->auth->getUinqueId(4,$live_ids);
  155. if($live_id > 9999) {
  156. $this->error("直播间超限,请联系客服");
  157. }
  158. $data = [];
  159. $data["user_id"] = $this->auth->id;
  160. $data["live_id"] = $live_id;
  161. $data["live_hot"] = 0;
  162. $data["live_type"] = $live_type;
  163. $data["live_notice"] = $live_notice;
  164. $data["live_notice_detail"] = $live_notice_detail;
  165. $data["is_online"] = 0;
  166. $data["status"] = 1;
  167. $data["is_recommend"] = 0;
  168. $data["createtime"] = time();
  169. $id = $liveModel->insertGetId($data);
  170. if(!$id) {
  171. $this->error("直播间创建失败,请稍后重试!");
  172. }
  173. $data["id"] = $id;
  174. $where = [];
  175. $where["a.id"] = $id;
  176. // 获取数据
  177. $getredisLiveInfo = $this->alias("a")
  178. ->field("a.*,b.id as live_id,b.name as live_type_name,u.nickname,u.avatar,at.name as anchor_type_name")
  179. ->join("hx_live_type b","a.live_type = b.id","left")
  180. ->join("hx_user u","a.user_id = u.id","left")
  181. ->join("hx_user_anchor an","a.user_id = an.user_id","left")
  182. ->join("hx_user_anchor_type at","an.type_id = an.type_id")
  183. ->where($where)->find();
  184. $redis->set("live_".$live_id,json_encode($getredisLiveInfo));
  185. $liveInfo = $liveModel->get(["user_id"=>$this->auth->id]);
  186. } else {
  187. $redisLiveInfo = $redis->get('live_'.$sqlLiveInfo["live_id"]);
  188. if(!$redisLiveInfo) {
  189. // 加入缓存排序
  190. $redis->zAdd("liveRank", $sqlLiveInfo['live_hot'], $sqlLiveInfo["live_id"]);
  191. // 加入缓存
  192. $redis->set("live_".$sqlLiveInfo["live_id"],json_encode($sqlLiveInfo));
  193. }
  194. $liveInfo = $sqlLiveInfo;
  195. }
  196. $this->success("获取成功!",$liveInfo);
  197. }
  198. /**
  199. * 获取直播间列表排序
  200. */
  201. public function getLiveRankList() {
  202. $thispage = $this->request->request('thispage',1,"intval"); // 当前页数
  203. $pagenum = $this->request->request('pagenum',10,"intval"); // 每页显示条数0=不做分页
  204. $type_id = $this->request->request('type_id'); // type
  205. $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
  206. $all = $this->request->request('all'); // 全部分类0=否1=是
  207. $start = ($thispage-1)*$pagenum;
  208. $end = $start+($pagenum-1);
  209. // 获取排序
  210. $redis = new Redis();
  211. $redisconfig = config("redis");
  212. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  213. if ($redisconfig['redis_pwd']) {
  214. $redis->auth($redisconfig['redis_pwd']);
  215. }
  216. if($redisconfig['redis_selectdb'] > 0){
  217. $redis->select($redisconfig['redis_selectdb']);
  218. }
  219. $redisLiveRankList = $redis->zRange("liveRank",$start,$end);
  220. $liveModel = new \app\common\model\Live();
  221. if(!$redisLiveRankList) {
  222. // 直接从数据库获取所有数据
  223. $where = [];
  224. $where["a.status"] = 1;
  225. $sqlLiveList = $liveModel->alias("a")
  226. ->field("a.*,b.id as live_type,b.name as live_type_name,u.nickname,u.avatar,at.name as anchor_type_name")
  227. ->join("hx_live_type b","a.live_type = b.id","left")
  228. ->join("hx_user u","a.user_id = u.id","left")
  229. ->join("hx_user_anchor an","a.user_id = an.user_id","left")
  230. ->join("hx_user_anchor_type at","an.type_id = an.type_id")
  231. ->where($where)->select();
  232. if($sqlLiveList) {
  233. foreach($sqlLiveList as $k => $v) {
  234. // 加入缓存排序
  235. $redis->zAdd("liveRank", $v['live_hot'], $v["live_id"]);
  236. // 加入缓存
  237. $redis->set("live_".$v["live_id"],json_encode($v));
  238. }
  239. $redisLiveRankList = $redis->zRange("liveRank",$start,$end);
  240. }
  241. }
  242. if(!$redisLiveRankList) {
  243. $this->error("数据为空!");
  244. }
  245. $resultInfo = $liveModel->getLiveInfoByLiveId($redisLiveRankList,$type_id,$is_recommend,$all);
  246. $this->success("获取成功!",$resultInfo);
  247. }
  248. /**
  249. * 获取派对类型
  250. */
  251. public function getLiveType() {
  252. $livetypeModel = new \app\common\model\LiveType();
  253. $livetypeList = $livetypeModel->select();
  254. $this->success("获取成功!",$livetypeList);
  255. }
  256. /**
  257. * 获取直播间用户排序
  258. */
  259. public function getLiveUserRank() {
  260. $live_id = $this->request->request("live_id");// 直播间ID
  261. if (!$live_id) $this->error(__('Invalid parameters'));
  262. $redis = new Redis();
  263. $redisconfig = config("redis");
  264. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  265. if ($redisconfig['redis_pwd']) {
  266. $redis->auth($redisconfig['redis_pwd']);
  267. }
  268. if($redisconfig['redis_selectdb'] > 0){
  269. $redis->select($redisconfig['redis_selectdb']);
  270. }
  271. $res = [];
  272. // 获取本周第一天
  273. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  274. // 获取本月第一天
  275. $monthday = date("Ym01");
  276. $userModel = new \app\common\model\User();
  277. // 获取条数
  278. $num = 50;
  279. // 获取50条财富排行周记录
  280. $getweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$weekday,0,$num-1,true);
  281. $res['getRankListWeek'] = $userModel->rankList($getweek);
  282. // 获取50条贡献排行周记录
  283. $toweek = $redis->zRevRange("live_jewel_to_".$live_id.":".$weekday,0,$num-1,true);
  284. $res['toRankListWeek'] = $userModel->rankList($toweek);
  285. // 获取50条财富排行月记录
  286. $toweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$monthday,0,$num-1,true);
  287. $res['getRankListMonth'] = $userModel->rankList($toweek);
  288. // 获取50条贡献排行周记录
  289. $toweek = $redis->zRevRange("live_jewel_to_".$live_id.":".$monthday,0,$num-1,true);
  290. $res['toRankListMonth'] = $userModel->rankList($toweek);
  291. return $this->success("获取成功!",$res);
  292. }
  293. /**
  294. * 直播间热度更新
  295. */
  296. public function changeUserLivehot() {
  297. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  298. $live_hot = $this->request->request('live_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  299. if (!$live_id || !$live_hot) {
  300. $this->error(__('Invalid parameters'));
  301. }
  302. $redis = new Redis();
  303. $redisconfig = config("redis");
  304. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  305. if ($redisconfig['redis_pwd']) {
  306. $redis->auth($redisconfig['redis_pwd']);
  307. }
  308. if($redisconfig['redis_selectdb'] > 0){
  309. $redis->select($redisconfig['redis_selectdb']);
  310. }
  311. // 更新热度
  312. $redis->zIncrBy("liveRank", $live_hot, $live_id);
  313. return $this->success("更新成功!");
  314. }
  315. /**
  316. * 加入直播间(这里只做记录)
  317. */
  318. public function joinLive() {
  319. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  320. if (!$live_id) {
  321. $this->error(__('Invalid parameters'));
  322. }
  323. $livejoinModel = new \app\common\model\LiveJoin();
  324. // 添加记录
  325. $data = [];
  326. $data["user_id"] = $this->auth->id;
  327. $data["live_id"] = $live_id;
  328. $data["createtime"] = time();
  329. $res = $livejoinModel->insert($data);
  330. return $this->success("添加成功!",$res);
  331. }
  332. /**
  333. * 退出直播间(这里只做记录)
  334. */
  335. public function outLive() {
  336. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  337. if (!$live_id) {
  338. $this->error(__('Invalid parameters'));
  339. }
  340. $livejoinModel = new \app\common\model\LiveJoin();
  341. // 删除记录
  342. $where = [];
  343. $where["live_id"] = $live_id;
  344. $where["user_id"] = $this->auth->id;
  345. $res = $livejoinModel->where($where)->delete();
  346. return $this->success("退出成功!",$res);
  347. }
  348. /**
  349. * 直播间收藏
  350. */
  351. public function cellectionLive() {
  352. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  353. if (!$live_id) {
  354. $this->error(__('Invalid parameters'));
  355. }
  356. $livecellectionModel = new \app\common\model\LiveCellection();
  357. // 添加记录
  358. $data = [];
  359. $data["user_id"] = $this->auth->id;
  360. $data["live_id"] = $live_id;
  361. $data["createtime"] = time();
  362. $res = $livecellectionModel->insert($data);
  363. return $this->success("收藏成功!",$res);
  364. }
  365. /**
  366. * 直播间收藏列表
  367. */
  368. public function cellectionLiveList() {
  369. $page = $this->request->request('page',1); // 分页
  370. $pageNum = $this->request->request('pageNum',10); // 分页
  371. // 分页搜索构建
  372. $pageStart = ($page-1)*$pageNum;
  373. $livecellectionModel = new \app\common\model\LiveCellection();
  374. $userModel = new \app\common\model\User();
  375. $where = [];
  376. $where["a.user_id"] = $this->auth->id;
  377. $list = $livecellectionModel->alias("a")
  378. ->field("a.live_id,u.avatar,r.live_id as r_id,r.live_name,t.name as live_type")
  379. ->where($where)
  380. ->join("hx_live r","a.live_id = r.id")
  381. ->join("hx_live_type t","t.id = r.live_type")
  382. ->join("hx_user u","u.id = r.user_id")
  383. ->limit($pageStart,$pageNum)
  384. ->select();
  385. if($list) {
  386. $redis = new Redis();
  387. $redisconfig = config("redis");
  388. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  389. if ($redisconfig['redis_pwd']) {
  390. $redis->auth($redisconfig['redis_pwd']);
  391. }
  392. if($redisconfig['redis_selectdb'] > 0){
  393. $redis->select($redisconfig['redis_selectdb']);
  394. }
  395. // 获取本周第一天
  396. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  397. // 获取redis 中 用户排行榜前五名
  398. foreach($list as $k => $v) {
  399. $getweek = $redis->zRevRange("jewel_get_".$v["live_id"].":".$weekday,0,4,true);
  400. $userlist = $userModel->rankList($getweek);
  401. if($userlist) {
  402. $users = [];
  403. foreach($userlist as $m => $n) {
  404. $users[] = $n["avatar"];
  405. }
  406. $list[$k]["users"] = $users;
  407. } else {
  408. $list[$k]["users"] = [];
  409. }
  410. }
  411. } else {
  412. return $this->success("数据为空!",[]);
  413. }
  414. return $this->success("获取成功!",$list);
  415. }
  416. /**
  417. * 获取直播间信息
  418. */
  419. public function getLiveInfo() {
  420. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  421. if (!$live_id) {
  422. $this->error(__('Invalid parameters'));
  423. }
  424. $liveModel = new \app\common\model\Live();
  425. $userModel = new \app\common\model\User();
  426. // 获取主体信息
  427. $where = [];
  428. $where["a.id"] = $live_id;
  429. $liveInfo = $liveModel->alias("a")
  430. ->field("a.id,a.user_id,a.live_id,a.live_name,a.live_logo,rt.name as live_type,a.live_notice")
  431. ->join("hx_live_type rt","rt.id = a.live_type")
  432. ->where($where)
  433. ->find();
  434. if(!$liveInfo) {
  435. return $this->error("数据为空!",[]);
  436. }
  437. // 获取房主信息
  438. $where = [];
  439. $where["id"] = $liveInfo["user_id"];
  440. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  441. // 获取技能信息
  442. $skillList = Model("ViewUserSkill")->getSkillInfo($liveInfo["user_id"]);
  443. $userInfo["skill"] = implode("/",$skillList);
  444. $liveInfo["userInfo"] = $userInfo;
  445. $this->success("获取成功!",$liveInfo);
  446. }
  447. /**
  448. * 直播间设置
  449. */
  450. public function setLive() {
  451. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  452. $live_pass = $this->request->request('live_pass'); // 直播间密码
  453. $live_type = $this->request->request('live_type'); // 使用场景
  454. $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
  455. $background = $this->request->request('background'); // 直播间背景
  456. if (!$live_id || (!$live_pass && !$live_type && !$is_screen && !$background)) {
  457. $this->error(__('Invalid parameters'));
  458. }
  459. $liveModel = new \app\common\model\Live();
  460. $data = [];
  461. $live_pass && $data["live_pass"] = $live_pass;
  462. $live_type && $data["live_type"] = $live_type;
  463. $is_screen && $data["is_screen"] = $is_screen;
  464. $background && $data["background"] = $background;
  465. $where = [];
  466. $where["id"] = $live_id;
  467. $res = $liveModel->update($data,$where);
  468. if($res) {
  469. $this->success("更新成功!",$data);
  470. } else {
  471. $this->error("网络错误,请稍后重试!");
  472. }
  473. }
  474. /**
  475. * 直播间管理设置
  476. */
  477. public function liveManageSet() {
  478. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  479. $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
  480. $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  481. $time = $this->request->request('time'); // 限制时间(单位:秒):0=永久
  482. if (!$live_id || !$user_id || !$item || ($item>1 && $time<=0)) {
  483. $this->error(__('Invalid parameters'));
  484. }
  485. $livemanageModel = new \app\common\model\LiveManage();
  486. $data = [];
  487. $data["user_id"] = $user_id;
  488. $data["live_id"] = $live_id;
  489. $data["item"] = $item;
  490. $time && $data["time"] = $time+time();
  491. $data["createtime"] = time();
  492. $res = $livemanageModel->insertGetId($data);
  493. if($res) {
  494. $this->success("设置成功!",$data);
  495. } else {
  496. $this->error("网络错误,请稍后重试!");
  497. }
  498. }
  499. /**
  500. * 直播间管理设置列表
  501. */
  502. public function liveManageSetList() {
  503. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  504. $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  505. if (!$live_id) {
  506. $this->error(__('Invalid parameters'));
  507. }
  508. $livemanageModel = new \app\common\model\LiveManage();
  509. $where = [];
  510. $where["a.live_id"] = $live_id;
  511. $where["a.item"] = $item;
  512. $list = $livemanageModel->alias("a")
  513. ->field("a.id,a.item,u.avatar,u.nickname,u.level,u.gender,a.time")
  514. ->join("hx_user u", "u.id = a.user_id")
  515. ->where($where)
  516. ->select();
  517. if(!$list) $this->success("数据为空!",[]);
  518. foreach($list as $k => &$v) $v["time"] = date("Y-m-d H:i:s",$v["time"]);
  519. $this->success("获取成功!",$list);
  520. }
  521. /**
  522. * 直播间管理设置移除
  523. */
  524. public function liveManageSetDel() {
  525. $id = $this->request->request('id',0,"intval"); // 设置ID
  526. if (!$id) {
  527. $this->error(__('Invalid parameters'));
  528. }
  529. $livemanageModel = new \app\common\model\LiveManage();
  530. $where = [];
  531. $where["id"] = $id;
  532. $res = $livemanageModel->where($where)->delete();
  533. if($res !== false) {
  534. $this->success("移除成功!",$res);
  535. } else {
  536. $this->error("网络错误,请稍后重试!");
  537. }
  538. }
  539. /**
  540. * 更新直播间公告
  541. */
  542. public function saveLiveNotice() {
  543. $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
  544. $live_notice = $this->request->request('live_notice'); // 公告标题
  545. if (!$live_id || !$live_notice) {
  546. $this->error(__('Invalid parameters'));
  547. }
  548. $liveModel = new \app\common\model\Live();
  549. $where = [];
  550. $where["id"] = $live_id;
  551. $data = [];
  552. $data["live_notice"] = $live_notice;
  553. $res = $liveModel->update($data,$where);
  554. if($res) {
  555. $this->success("更新成功!",$res);
  556. } else {
  557. $this->error("网络错误,请稍后重试!");
  558. }
  559. }
  560. /**
  561. * 赠送礼物
  562. */
  563. public function giveGiftToYou() {
  564. $user_id = $this->request->request("user_id");// 赠送对象
  565. $gift_id = $this->request->request("gift_id");// 礼物ID
  566. $live_id = $this->request->request("live_id");// 直播间ID
  567. $number = $this->request->request("number");// 赠送数量
  568. if (!$user_id || !$gift_id || !$live_id || !$number) $this->error(__('Invalid parameters'));
  569. // 不可以赠送给自己
  570. if($user_id == $this->auth->id) $this->error("不可以赠送给自己!");
  571. // 获取礼物信息
  572. $giftModel = new \app\common\model\Gift();
  573. $where = [];
  574. $where["id"] = $gift_id;
  575. $giftInfo = $giftModel->where($where)->find();
  576. if (!$giftInfo) $this->error("礼物信息查询失败!");
  577. $giftValue = $giftInfo["value"] * $number;
  578. $userModel = new \app\common\model\User();
  579. // 判断当前用户余额
  580. $where = [];
  581. $where["id"] = $this->auth->id;
  582. $userInfo = $userModel->where($where)->find();
  583. if (!$userInfo) $this->error("用户信息查询失败!");
  584. if($userInfo["jewel"] < $giftValue) $this->error("您的钻石余额不足!");
  585. // 获取赠送用户信息
  586. $where = [];
  587. $where["id"] = $user_id;
  588. $touserInfo = $userModel->where($where)->find();
  589. // 事务处理余额与记录信息
  590. $userjewellogModel = new \app\common\model\UserJewelLog();
  591. $giftuserliveModel = new \app\common\model\GiftUserLive();
  592. Db::startTrans();
  593. try{
  594. // 扣除当前用户钻石余额
  595. $where = [];
  596. $where["id"] = $this->auth->id;
  597. $res1 = $userModel->where($where)->setDec("jewel",$giftValue);
  598. // 添加当前用户钻石流水记录
  599. $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3);
  600. // 添加赠送用户钻石余额
  601. $where = [];
  602. $where["id"] = $user_id;
  603. $res3 = $userModel->where($where)->setInc("jewel",$giftValue);
  604. // 添加赠送用户钻石流水记录
  605. $res4 = $userjewellogModel->addUserJewelLog($user_id,$giftValue,"+",$touserInfo["jewel"],"获赠礼物:'".$giftInfo["name"]."',增加".$giftValue."钻石!",9);
  606. // 添加礼物赠送记录表
  607. $data = [];
  608. $data["user_id"] = $this->auth->id;
  609. $data["user_to_id"] = $user_id;
  610. $data["live_id"] = $live_id;
  611. $data["gift_id"] = $gift_id;
  612. $data["number"] = $number;
  613. $data["price"] = $giftInfo["value"];
  614. $data["value"] = $giftValue;
  615. $data["createtime"] = time();
  616. $res5 = $giftuserliveModel->insertGetId($data);
  617. if($res1 && $res2 && $res3 && $res4 && $res5) {
  618. Db::commit();
  619. $redis = new Redis();
  620. $redisconfig = config("redis");
  621. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  622. if ($redisconfig['redis_pwd']) {
  623. $redis->auth($redisconfig['redis_pwd']);
  624. }
  625. if($redisconfig['redis_selectdb'] > 0){
  626. $redis->select($redisconfig['redis_selectdb']);
  627. }
  628. // 获取本周第一天
  629. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  630. // 获取本月第一天
  631. $monthday = date("Ym01");
  632. // 添加redis记录做财富排行榜周榜用
  633. $redis->zIncrBy("live_jewel_get_".$live_id.":".$weekday,$giftValue,$user_id);
  634. // 添加redis记录做财富排行榜月榜用
  635. $redis->zIncrBy("live_jewel_get_".$live_id.":".$monthday,$giftValue,$user_id);
  636. // 添加redis记录做贡献排行榜周榜用
  637. $redis->zIncrBy("live_jewel_to_".$live_id.":".$weekday,$giftValue,$this->auth->id);
  638. // 添加redis记录做贡献排行榜月榜用
  639. $redis->zIncrBy("live_jewel_to_".$live_id.":".$monthday,$giftValue,$this->auth->id);
  640. $this->success("操作成功!");
  641. }
  642. }catch (ValidateException $e) {
  643. Db::rollback();
  644. $this->error($e->getMessage());
  645. } catch (PDOException $e) {
  646. Db::rollback();
  647. $this->error($e->getMessage());
  648. } catch (Exception $e) {
  649. Db::rollback();
  650. $this->error($e->getMessage());
  651. }
  652. }
  653. //============================定时任务==========================//
  654. /**
  655. * 更新直播间信息(热度等)
  656. */
  657. public function updateLiveInfo() {
  658. $redis = new Redis();
  659. $redisconfig = config("redis");
  660. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  661. if ($redisconfig['redis_pwd']) {
  662. $redis->auth($redisconfig['redis_pwd']);
  663. }
  664. if($redisconfig['redis_selectdb'] > 0){
  665. $redis->select($redisconfig['redis_selectdb']);
  666. }
  667. // 获取直播间热度信息
  668. $redisLiveRankList = $redis->zRevRange("liveRank",0,-1,true);
  669. $liveModel = new \app\common\model\Live();
  670. if($redisLiveRankList) {
  671. foreach($redisLiveRankList as $k => $v) {
  672. $liveModel->update(["live_hot"=>$v],["live_id"=>$k]);
  673. }
  674. }
  675. }
  676. }