123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- <?php
- namespace app\api\controller;
- use app\api\controller\Common;
- use app\common\model\LiveJoin;
- use fast\Random;
- use app\common\controller\RedisLeaderboard;
- use Redis;
- use think\Db;
- class Live extends Common
- {
- protected $noNeedLogin = ['updateLiveInfo','getLiveType','abkd','abab','getGiftInfo'];
- protected $noNeedRight = ['*'];
-
- public function abkd() {
- $weekend = strtotime('monday this week');
- exit;
- $user_id = 319;
-
- $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum("money");
- $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id" => $user_id, "status" => ["in", [0, 1]]])->sum("money");
- $withdraw = bcdiv($withdraw, 100,2);
- $sum = bcsub($moneylog, $withdraw,2);
- $usermoney = \app\common\model\User::where(["id" => $user_id])->value("money");
- if ($usermoney == $sum) {
- echo 1;
- } else {
- echo 'f'.$moneylog;
- }
- exit;
- }
-
- public function abab() {
- $users = \app\common\model\User::field("id")->select();
- if($users) foreach($users as $k => $v) {
- $user_id = $v["id"];
-
- $limit = 5;
- $coin = \app\common\model\UserSoundcoinLog::field("id,value,mode,balance,before")->where(["user_id" => $user_id])->limit($limit)->order("createtime", "desc")->select();
- if ($coin) {
- $coinjian = 0;
- $coinjia = 0;
- $countkey = count($coin) - 1;
- foreach ($coin as $k => $v) {
- if ($v["mode"] == "-") {
- $coinjian = $coinjian + $v["value"];
- } elseif ($v["mode"] == "+") {
- $coinjia = $coinjia + $v["value"];
- }
- $k == 0 && $first = $v["balance"];
- $k == $countkey && $last = $v["before"];
- }
- $usercoin = \app\common\model\User::where(["id" => $user_id])->value("sound_coin");
- $change = $coinjian - $coinjia;
- if ($first + $change == $last) {
- echo "1<br/>";
- } else {
- echo "用户余额:" . $usercoin;
- echo "最初值:" . $first;
- echo "改变值:" . $change;
- echo "最后余额:" . $last . "<br/>";
- }
- }
- }
- }
- public function getGiftInfo() {
- $type_id = $this->request->get('type_id');
- $list = \app\common\model\EggGift::field("id,gift_id,price")->where(["jackpot_id"=>$type_id,"is_use"=>0])->select();
- $count = 0;
- $sum = 0;
- if($list) foreach($list as $k => $v) {
- $count++;
- }
- $res = [];
- $res["count"] = $count;
- $res["sum"] = \app\common\model\EggGift::where(["jackpot_id"=>$type_id,"is_use"=>0])->sum("price");
- $this->success("获取成功!",$res);
- }
-
- public function createLive() {
-
- $anchorModel = new \app\common\model\UserAnchor();
- $where = [];
- $where["user_id"] = $this->auth->id;
- $where["status"] = 1;
- $anchorInfo = $anchorModel->where($where)->find();
- if(!$anchorInfo) $this->error("请先提交主播申请,并等待审核通过!");
- $liveModel = new \app\common\model\Live();
- $where = [];
- $where["user_id"] = $this->auth->id;
- $sqlLiveInfo = $liveModel->where($where)->find();
-
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
- if(!$sqlLiveInfo) {
- $live_type = $anchorInfo["type_id"];
- $live_notice = "Hi!欢迎加入我的直播间";
- $live_notice_detail = "Hi!欢迎加入我的直播间Hi!欢迎加入我的直播间";
- if (!$live_type || !$live_notice) {
- $this->error(__('Invalid parameters'));
- }
- $live_ids = $liveModel->column("live_id");
-
- $live_id = $this->auth->getUinqueId(4,$live_ids);
- if($live_id > 9999) {
- $this->error("直播间超限,请联系客服");
- }
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["live_id"] = $live_id;
- $data["live_hot"] = 0;
- $data["live_type"] = $live_type;
- $data["live_notice"] = $live_notice;
- $data["live_notice_detail"] = $live_notice_detail;
- $data["is_online"] = 0;
- $data["status"] = 1;
- $data["is_recommend"] = 0;
- $data["createtime"] = time();
- $id = $liveModel->insertGetId($data);
- if(!$id) {
- $this->error("直播间创建失败,请稍后重试!");
- }
- $data["id"] = $id;
- $where = [];
- $where["a.id"] = $id;
-
- $getredisLiveInfo = $this->alias("a")
- ->field("a.*,b.id as live_id,b.name as live_type_name,u.nickname,u.avatar,at.name as anchor_type_name")
- ->join("hx_live_type b","a.live_type = b.id","left")
- ->join("hx_user u","a.user_id = u.id","left")
- ->join("hx_user_anchor an","a.user_id = an.user_id","left")
- ->join("hx_user_anchor_type at","an.type_id = an.type_id")
- ->where($where)->find();
- $redis->set("live_".$live_id,json_encode($getredisLiveInfo));
- $liveInfo = $liveModel->get(["user_id"=>$this->auth->id]);
- } else {
- $redisLiveInfo = $redis->get('live_'.$sqlLiveInfo["live_id"]);
- if(!$redisLiveInfo) {
-
- $redis->zAdd("liveRank", $sqlLiveInfo['live_hot'], $sqlLiveInfo["live_id"]);
-
- $redis->set("live_".$sqlLiveInfo["live_id"],json_encode($sqlLiveInfo));
- }
- $liveInfo = $sqlLiveInfo;
- }
- $this->success("获取成功!",$liveInfo);
- }
-
- public function getLiveRankList() {
- $thispage = $this->request->request('thispage',1,"intval");
- $pagenum = $this->request->request('pagenum',10,"intval");
- $type_id = $this->request->request('type_id');
- $is_recommend = $this->request->request('is_recommend');
- $all = $this->request->request('all');
- $start = ($thispage-1)*$pagenum;
- $end = $start+($pagenum-1);
-
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
- $redisLiveRankList = $redis->zRange("liveRank",$start,$end);
- $liveModel = new \app\common\model\Live();
- if(!$redisLiveRankList) {
-
- $where = [];
- $where["a.status"] = 1;
- $sqlLiveList = $liveModel->alias("a")
- ->field("a.*,b.id as live_type,b.name as live_type_name,u.nickname,u.avatar,at.name as anchor_type_name")
- ->join("hx_live_type b","a.live_type = b.id","left")
- ->join("hx_user u","a.user_id = u.id","left")
- ->join("hx_user_anchor an","a.user_id = an.user_id","left")
- ->join("hx_user_anchor_type at","an.type_id = an.type_id")
- ->where($where)->select();
- if($sqlLiveList) {
- foreach($sqlLiveList as $k => $v) {
-
- $redis->zAdd("liveRank", $v['live_hot'], $v["live_id"]);
-
- $redis->set("live_".$v["live_id"],json_encode($v));
- }
- $redisLiveRankList = $redis->zRange("liveRank",$start,$end);
- }
- }
- if(!$redisLiveRankList) {
- $this->error("数据为空!");
- }
- $resultInfo = $liveModel->getLiveInfoByLiveId($redisLiveRankList,$type_id,$is_recommend,$all);
- $this->success("获取成功!",$resultInfo);
- }
-
- public function getLiveType() {
- $livetypeModel = new \app\common\model\LiveType();
- $livetypeList = $livetypeModel->select();
- $this->success("获取成功!",$livetypeList);
- }
-
- public function getLiveUserRank() {
- $live_id = $this->request->request("live_id");
- if (!$live_id) $this->error(__('Invalid parameters'));
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
- $res = [];
-
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
-
- $monthday = date("Ym01");
- $userModel = new \app\common\model\User();
-
- $num = 50;
-
- $getweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$weekday,0,$num-1,true);
- $res['getRankListWeek'] = $userModel->rankList($getweek);
-
- $toweek = $redis->zRevRange("live_jewel_to_".$live_id.":".$weekday,0,$num-1,true);
- $res['toRankListWeek'] = $userModel->rankList($toweek);
-
- $toweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$monthday,0,$num-1,true);
- $res['getRankListMonth'] = $userModel->rankList($toweek);
-
- $toweek = $redis->zRevRange("live_jewel_to_".$live_id.":".$monthday,0,$num-1,true);
- $res['toRankListMonth'] = $userModel->rankList($toweek);
- return $this->success("获取成功!",$res);
- }
-
- public function changeUserLivehot() {
- $live_id = $this->request->request('live_id',0,"intval");
- $live_hot = $this->request->request('live_hot');
- if (!$live_id || !$live_hot) {
- $this->error(__('Invalid parameters'));
- }
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
-
- $redis->zIncrBy("liveRank", $live_hot, $live_id);
- return $this->success("更新成功!");
- }
-
- public function joinLive() {
- $live_id = $this->request->request('live_id',0,"intval");
- if (!$live_id) {
- $this->error(__('Invalid parameters'));
- }
- $livejoinModel = new \app\common\model\LiveJoin();
-
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["live_id"] = $live_id;
- $data["createtime"] = time();
- $res = $livejoinModel->insert($data);
- return $this->success("添加成功!",$res);
- }
-
- public function outLive() {
- $live_id = $this->request->request('live_id',0,"intval");
- if (!$live_id) {
- $this->error(__('Invalid parameters'));
- }
- $livejoinModel = new \app\common\model\LiveJoin();
-
- $where = [];
- $where["live_id"] = $live_id;
- $where["user_id"] = $this->auth->id;
- $res = $livejoinModel->where($where)->delete();
- return $this->success("退出成功!",$res);
- }
-
- public function cellectionLive() {
- $live_id = $this->request->request('live_id',0,"intval");
- if (!$live_id) {
- $this->error(__('Invalid parameters'));
- }
- $livecellectionModel = new \app\common\model\LiveCellection();
-
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["live_id"] = $live_id;
- $data["createtime"] = time();
- $res = $livecellectionModel->insert($data);
- return $this->success("收藏成功!",$res);
- }
-
- public function cellectionLiveList() {
- $page = $this->request->request('page',1);
- $pageNum = $this->request->request('pageNum',10);
-
- $pageStart = ($page-1)*$pageNum;
- $livecellectionModel = new \app\common\model\LiveCellection();
- $userModel = new \app\common\model\User();
- $where = [];
- $where["a.user_id"] = $this->auth->id;
- $list = $livecellectionModel->alias("a")
- ->field("a.live_id,u.avatar,r.live_id as r_id,r.live_name,t.name as live_type")
- ->where($where)
- ->join("hx_live r","a.live_id = r.id")
- ->join("hx_live_type t","t.id = r.live_type")
- ->join("hx_user u","u.id = r.user_id")
- ->limit($pageStart,$pageNum)
- ->select();
- if($list) {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
-
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
-
- foreach($list as $k => $v) {
- $getweek = $redis->zRevRange("jewel_get_".$v["live_id"].":".$weekday,0,4,true);
- $userlist = $userModel->rankList($getweek);
- if($userlist) {
- $users = [];
- foreach($userlist as $m => $n) {
- $users[] = $n["avatar"];
- }
- $list[$k]["users"] = $users;
- } else {
- $list[$k]["users"] = [];
- }
- }
- } else {
- return $this->success("数据为空!",[]);
- }
- return $this->success("获取成功!",$list);
- }
-
- public function getLiveInfo() {
- $live_id = $this->request->request('live_id',0,"intval");
- if (!$live_id) {
- $this->error(__('Invalid parameters'));
- }
- $liveModel = new \app\common\model\Live();
- $userModel = new \app\common\model\User();
-
- $where = [];
- $where["a.id"] = $live_id;
- $liveInfo = $liveModel->alias("a")
- ->field("a.id,a.user_id,a.live_id,a.live_name,a.live_logo,rt.name as live_type,a.live_notice")
- ->join("hx_live_type rt","rt.id = a.live_type")
- ->where($where)
- ->find();
- if(!$liveInfo) {
- return $this->error("数据为空!",[]);
- }
-
- $where = [];
- $where["id"] = $liveInfo["user_id"];
- $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
-
- $skillList = Model("ViewUserSkill")->getSkillInfo($liveInfo["user_id"]);
- $userInfo["skill"] = implode("/",$skillList);
- $liveInfo["userInfo"] = $userInfo;
- $this->success("获取成功!",$liveInfo);
- }
-
- public function setLive() {
- $live_id = $this->request->request('live_id',0,"intval");
- $live_pass = $this->request->request('live_pass');
- $live_type = $this->request->request('live_type');
- $is_screen = $this->request->request('is_screen');
- $background = $this->request->request('background');
- if (!$live_id || (!$live_pass && !$live_type && !$is_screen && !$background)) {
- $this->error(__('Invalid parameters'));
- }
- $liveModel = new \app\common\model\Live();
- $data = [];
- $live_pass && $data["live_pass"] = $live_pass;
- $live_type && $data["live_type"] = $live_type;
- $is_screen && $data["is_screen"] = $is_screen;
- $background && $data["background"] = $background;
- $where = [];
- $where["id"] = $live_id;
- $res = $liveModel->update($data,$where);
- if($res) {
- $this->success("更新成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
-
- public function liveManageSet() {
- $live_id = $this->request->request('live_id',0,"intval");
- $user_id = $this->request->request('user_id',0,"intval");
- $item = $this->request->request('item');
- $time = $this->request->request('time');
- if (!$live_id || !$user_id || !$item || ($item>1 && $time<=0)) {
- $this->error(__('Invalid parameters'));
- }
- $livemanageModel = new \app\common\model\LiveManage();
- $data = [];
- $data["user_id"] = $user_id;
- $data["live_id"] = $live_id;
- $data["item"] = $item;
- $time && $data["time"] = $time+time();
- $data["createtime"] = time();
- $res = $livemanageModel->insertGetId($data);
- if($res) {
- $this->success("设置成功!",$data);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
-
- public function liveManageSetList() {
- $live_id = $this->request->request('live_id',0,"intval");
- $item = $this->request->request('item',1,"intval");
- if (!$live_id) {
- $this->error(__('Invalid parameters'));
- }
- $livemanageModel = new \app\common\model\LiveManage();
- $where = [];
- $where["a.live_id"] = $live_id;
- $where["a.item"] = $item;
- $list = $livemanageModel->alias("a")
- ->field("a.id,a.item,u.avatar,u.nickname,u.level,u.gender,a.time")
- ->join("hx_user u", "u.id = a.user_id")
- ->where($where)
- ->select();
- if(!$list) $this->success("数据为空!",[]);
- foreach($list as $k => &$v) $v["time"] = date("Y-m-d H:i:s",$v["time"]);
- $this->success("获取成功!",$list);
- }
-
- public function liveManageSetDel() {
- $id = $this->request->request('id',0,"intval");
- if (!$id) {
- $this->error(__('Invalid parameters'));
- }
- $livemanageModel = new \app\common\model\LiveManage();
- $where = [];
- $where["id"] = $id;
- $res = $livemanageModel->where($where)->delete();
- if($res !== false) {
- $this->success("移除成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
-
- public function saveLiveNotice() {
- $live_id = $this->request->request('live_id',0,"intval");
- $live_notice = $this->request->request('live_notice');
- if (!$live_id || !$live_notice) {
- $this->error(__('Invalid parameters'));
- }
- $liveModel = new \app\common\model\Live();
- $where = [];
- $where["id"] = $live_id;
- $data = [];
- $data["live_notice"] = $live_notice;
- $res = $liveModel->update($data,$where);
- if($res) {
- $this->success("更新成功!",$res);
- } else {
- $this->error("网络错误,请稍后重试!");
- }
- }
-
- public function giveGiftToYou() {
- $user_id = $this->request->request("user_id");
- $gift_id = $this->request->request("gift_id");
- $live_id = $this->request->request("live_id");
- $number = $this->request->request("number");
- if (!$user_id || !$gift_id || !$live_id || !$number) $this->error(__('Invalid parameters'));
-
- if($user_id == $this->auth->id) $this->error("不可以赠送给自己!");
-
- $giftModel = new \app\common\model\Gift();
- $where = [];
- $where["id"] = $gift_id;
- $giftInfo = $giftModel->where($where)->find();
- if (!$giftInfo) $this->error("礼物信息查询失败!");
- $giftValue = $giftInfo["value"] * $number;
- $userModel = new \app\common\model\User();
-
- $where = [];
- $where["id"] = $this->auth->id;
- $userInfo = $userModel->where($where)->find();
- if (!$userInfo) $this->error("用户信息查询失败!");
- if($userInfo["jewel"] < $giftValue) $this->error("您的钻石余额不足!");
-
- $where = [];
- $where["id"] = $user_id;
- $touserInfo = $userModel->where($where)->find();
-
- $userjewellogModel = new \app\common\model\UserJewelLog();
- $giftuserliveModel = new \app\common\model\GiftUserLive();
- Db::startTrans();
- try{
-
- $where = [];
- $where["id"] = $this->auth->id;
- $res1 = $userModel->where($where)->setDec("jewel",$giftValue);
-
- $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $giftValue, "-", $userInfo["jewel"], "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "钻石!", 3);
-
- $where = [];
- $where["id"] = $user_id;
- $res3 = $userModel->where($where)->setInc("jewel",$giftValue);
-
- $res4 = $userjewellogModel->addUserJewelLog($user_id,$giftValue,"+",$touserInfo["jewel"],"获赠礼物:'".$giftInfo["name"]."',增加".$giftValue."钻石!");
-
- $data = [];
- $data["user_id"] = $this->auth->id;
- $data["user_to_id"] = $user_id;
- $data["live_id"] = $live_id;
- $data["gift_id"] = $gift_id;
- $data["number"] = $number;
- $data["price"] = $giftInfo["value"];
- $data["value"] = $giftValue;
- $data["createtime"] = time();
- $res5 = $giftuserliveModel->insertGetId($data);
- if($res1 && $res2 && $res3 && $res4 && $res5) {
- Db::commit();
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
-
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
-
- $monthday = date("Ym01");
-
- $redis->zIncrBy("live_jewel_get_".$live_id.":".$weekday,$giftValue,$user_id);
-
- $redis->zIncrBy("live_jewel_get_".$live_id.":".$monthday,$giftValue,$user_id);
-
- $redis->zIncrBy("live_jewel_to_".$live_id.":".$weekday,$giftValue,$this->auth->id);
-
- $redis->zIncrBy("live_jewel_to_".$live_id.":".$monthday,$giftValue,$this->auth->id);
- $this->success("操作成功!");
- }
- }catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- }
-
-
- public function updateLiveInfo() {
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth(config('redis_pwd'));
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select(config('redis_selectdb'));
- }
-
- $redisLiveRankList = $redis->zRevRange("liveRank",0,-1,true);
- $liveModel = new \app\common\model\Live();
- if($redisLiveRankList) {
- foreach($redisLiveRankList as $k => $v) {
- $liveModel->update(["live_hot"=>$v],["live_id"=>$k]);
- }
- }
- }
- }
|