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 = ['*'];
- /**
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * 余额提现检查
- */
- public function abkd() {
- $weekend = strtotime('monday this week');
- exit;
- // $user_id = $this->request->request("user_id");
- // if(!$user_id) return false;
- // $users = \app\common\model\User::field("id,money")->select();
- // if($users) foreach($users as $k => $v) {
- // $user_id = $v["id"];
- // // 统计用户所有兑换记录和提现记录 是否与余额匹配
- // $moneylog = \app\common\model\UserMoneyLog::where(["user_id"=>$user_id,"mode"=>"+"])->sum("money");
- // $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id"=>$user_id,"status"=>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<br/>";
- // } else {
- // echo $v['id']."用户余额:".$usermoney;
- // echo "记录和:".$sum."<br/>";
- //
- //// $money = bcsub($usermoney,$sum,2);
- //// $time = time();
- //// $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)");
- //
- // }
- // }
- $user_id = 319;
- // 统计用户所有兑换记录和提现记录 是否与余额匹配
- $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum('value');
- $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() {
- // $user_id = $this->request->request("user_id");
- // if(!$user_id) return false;
- $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/>";
- }
- }
- // return true;
- }
- }
- public function getGiftInfo() {
- $type_id = $this->request->get('type_id'); // type
- $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++;
- // $list[$k]["price"] = $v["price"]/100;
- }
- $res = [];
- $res["count"] = $count;
- $res["sum"] = \app\common\model\EggGift::where(["jackpot_id"=>$type_id,"is_use"=>0])->sum("price");
- // $res["list"] = $list;
- $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();
- // // 获取用户信息
- // $userModel = new \app\common\model\User();
- // $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
- // if(!$userInfo) $this->error("用户信息获取失败!");
- // 判断直播间是否存在
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- if(!$sqlLiveInfo) { // sql中不存在直播间信息
- $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");
- // 创建直播间ID (临时ID四位,直播间数不超过8999)
- $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"); // 每页显示条数0=不做分页
- $type_id = $this->request->request('type_id'); // type
- $is_recommend = $this->request->request('is_recommend'); // 推荐0=否1=是
- $all = $this->request->request('all'); // 全部分类0=否1=是
- $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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['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");// 直播间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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- $res = [];
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取本月第一天
- $monthday = date("Ym01");
- $userModel = new \app\common\model\User();
- // 获取条数
- $num = 50;
- // 获取50条财富排行周记录
- $getweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$weekday,0,$num-1,true);
- $res['getRankListWeek'] = $userModel->rankList($getweek);
- // 获取50条贡献排行周记录
- $toweek = $redis->zRevRange("live_jewel_to_".$live_id.":".$weekday,0,$num-1,true);
- $res['toRankListWeek'] = $userModel->rankList($toweek);
- // 获取50条财富排行月记录
- $toweek = $redis->zRevRange("live_jewel_get_".$live_id.":".$monthday,0,$num-1,true);
- $res['getRankListMonth'] = $userModel->rankList($toweek);
- // 获取50条贡献排行周记录
- $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"); // 直播间ID
- $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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- // 更新热度
- $redis->zIncrBy("liveRank", $live_hot, $live_id);
- return $this->success("更新成功!");
- }
- /**
- * 加入直播间(这里只做记录)
- */
- public function joinLive() {
- $live_id = $this->request->request('live_id',0,"intval"); // 直播间ID
- 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"); // 直播间ID
- 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"); // 直播间ID
- 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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取redis 中 用户排行榜前五名
- 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"); // 直播间ID
- 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"); // 直播间ID
- $live_pass = $this->request->request('live_pass'); // 直播间密码
- $live_type = $this->request->request('live_type'); // 使用场景
- $is_screen = $this->request->request('is_screen'); // 是否关闭公屏:1=是,0=否
- $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"); // 直播间ID
- $user_id = $this->request->request('user_id',0,"intval"); // 用户ID
- $item = $this->request->request('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- $time = $this->request->request('time'); // 限制时间(单位:秒):0=永久
- 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"); // 直播间ID
- $item = $this->request->request('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
- 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"); // 设置ID
- 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"); // 直播间ID
- $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");// 礼物ID
- $live_id = $this->request->request("live_id");// 直播间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."钻石!",9);
- // 添加礼物赠送记录表
- $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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- // 获取本周第一天
- $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
- // 获取本月第一天
- $monthday = date("Ym01");
- // 添加redis记录做财富排行榜周榜用
- $redis->zIncrBy("live_jewel_get_".$live_id.":".$weekday,$giftValue,$user_id);
- // 添加redis记录做财富排行榜月榜用
- $redis->zIncrBy("live_jewel_get_".$live_id.":".$monthday,$giftValue,$user_id);
- // 添加redis记录做贡献排行榜周榜用
- $redis->zIncrBy("live_jewel_to_".$live_id.":".$weekday,$giftValue,$this->auth->id);
- // 添加redis记录做贡献排行榜月榜用
- $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($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['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]);
- }
- }
- }
- }
|