123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace app\common\service;
- use think\Db;
- use think\Exception;
- use Redis;
- use app\common\service\TenimService;
- class RoomService
- {
- private $model = '';
- private $redis = '';
- private $roomTypeArr = [];
- /**
- * 初始化方法
- */
- public function __construct()
- {
- $this->model = new \app\common\model\Party();
- $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']);
- }
- $this->redis = $redis;
- $this->roomTypeArr = [1=>"party",2=>"live"];
- }
- /**
- * 房间设置缓存
- * @return void
- */
- public function roomRedis($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
- $room_type = isset($params['room_type']) ? $params['room_type'] : 0;
- if (!empty($room_type)) {
- $userModel = new \app\common\model\User();
- // 直接从数据库获取所有数据
- $where = [];
- $where["a.status"] = 1;
- $where["a.is_online"] = 1;
- $where["a.room_type"] = $room_type;
- $sqlPartyList = $this->model->alias("a")->field("a.*,b.name as type_name")
- ->join("hx_party_type b","a.party_type = b.id","left")->where($where)->select();
- $userList = $userModel->field("id,nickname,avatar")->where(["status"=>"normal"])->select();
- $userInfoArr = [];
- if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
- if($sqlPartyList) {
- foreach($sqlPartyList as $k => $v) {
- // 加入缓存排序
- $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
- // 设置冠名
- $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
- // 设置房主头像
- $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
- // 加入缓存
- $this->redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
- }
- $redisPartyRankList = $this->redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
- $result['data'] = $redisPartyRankList;
- }
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 获取房间信息
- */
- public function getRoomList($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
- $partyIds = isset($params['ids']) ? $params['ids'] : [];
- $typeId = isset($params['type_id']) ? $params['type_id'] : 0;
- $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
- $all = isset($params['is_recommend']) ? $params['all'] : 0;
- $start = isset($params['start']) ? $params['start'] : 1;
- $end = isset($params['end']) ? $params['end'] : 1;
- $index = isset($params['index']) ? $params['index'] : 0;
- if(!$partyIds) throw new Exception('未获取到房间ID');;
- $redisPartyInfo = $resdata = [];
- $where['status'] = 1;
- //$where['is_online'] = 1;
- $where['is_close'] = 0;
- $where['id'] = ['in',$partyIds];
- $field = 'id,room_type,party_hot';
- $partyData = $this->model->field($field)->where($where)->order('party_hot desc')->select();
- if (!empty($partyData)) {
- //获取房间用户信息
- /*$tenimService = new TenimService();
- $userParams = ['party_ids' => $partyIds];
- $userImRes = $tenimService->getRoomUser($userParams);*/
- foreach($partyData as $k => $v) {
- $roomType = $v['room_type'];
- $roomTypeStr = isset($this->roomTypeArr[$roomType]) ? $this->roomTypeArr[$roomType] : '';
- $getredisPartyInfo = $this->redis->get($roomTypeStr.'_'.$v['id']);
- $foreachData = json_decode($getredisPartyInfo,true);
- $isClose = isset($foreachData["is_close"]) ? $foreachData["is_close"] : 0;
- $partyPass = isset($foreachData["party_pass"]) ? $foreachData["party_pass"] : '';
- if(!$foreachData || $isClose == 1) continue;
- //if(!$foreachData || $partyPass != '') continue;//设置密码的也显示
- $mod = isset($foreachData["party_type"])?intval($foreachData["party_type"])%5:1;
- if(isset($foreachData["type_name"]) && $foreachData["type_name"]) {
- $type_name = $foreachData["type_name"];
- } else {
- $type_name = "普通房";
- }
- $hasdata = false;
- if($all == 1) {
- if($isRecommend == 1) {
- if($foreachData["is_recommend"] == 1) $hasdata = true;
- } else {
- $hasdata = true;
- }
- } elseif($typeId > 0) {
- if($typeId == $foreachData["party_type"]) $hasdata = true;
- }
- if($foreachData["status"] != 1) $hasdata = false;
- $foreachData = info_domain_image($foreachData,['party_logo']);
- //$userIds = $this->redis->zRange("party_user_".$foreachData["id"],0,-1);
- if ($hasdata) {
- /*$field = 'user_id,avatar';
- $fieldArr = explode(',',$field);*/
- /*$userWhere['id'] = ['in', $userIds];
- $userModel = new \app\common\model\User();
- $userData = $userModel->field($field)->where($userWhere)->select();*/
- $userDataResult = $this->getPartyUserList(['party_id'=>$v['id']]);
- $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : [];
- $userData = isset($userDataRes['member_list']) ? $userDataRes['member_list'] : [];
- $userDatas = [];
- if (!empty($userData)) {
- $userDatas = $userData;
- $userDatas = list_domain_image($userDatas,['avatar']);
- }
- $partyUserList = [
- 'user_list' => $userDatas,
- 'online_num' => count($userData),
- ];
- $redisPartyInfo[] = [
- "id" => $foreachData["id"],
- "room_type" => $v['room_type'],
- "party_id" => $foreachData["party_id"],
- "r_id" => $foreachData["party_id"],
- "party_name" => $foreachData["party_name"],
- "party_pass" => $foreachData["party_pass"] ? $foreachData["party_pass"]:"",
- "party_hot" => $v['party_hot'],
- "party_logo" => $foreachData["party_logo"],
- "party_type" => $type_name,
- "avatar" => $roomType == 2 ? $foreachData["avatar"]:"",
- "party_type_color" => $mod == 0?5:$mod,
- "party_user" => $partyUserList,
- ];
- }
- }
- if($index == 1) {
- $num = $end - $start + 1;
- $countData = count($redisPartyInfo);
- if($num > $countData) $num = $countData;
- if($redisPartyInfo) {
- $dataRand = array_rand($redisPartyInfo,$num);
- $dataRand = is_array($dataRand)?$dataRand:[$dataRand];
- if($redisPartyInfo) {
- foreach($redisPartyInfo as $k => $v) {
- if(in_array($k,$dataRand)) $resdata[] = $v;
- }
- }
- }
- } else {
- if($redisPartyInfo) {
- foreach($redisPartyInfo as $k => $v){
- if($k >= $start && $k <= $end) $resdata[] = $v;
- }
- }
- }
- }
- $result['data'] = $resdata;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- /**
- * 房间用户信息
- * @return void
- */
- public function getPartyUserList($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
- $partyId = isset($params['party_id']) ? $params['party_id'] : 0;
- $userIds = $this->redis->zRange("party_user_".$partyId,0,-1);
- $field = 'id,avatar,nickname';
- $userWhere['id'] = ['in', $userIds];
- $userWhere['is_stealth'] = 0;
- $userModel = new \app\common\model\User();
- $userData = $userModel->field($field)->where($userWhere)->select();
- $userDatas = [];
- if (!empty($userData)) {
- foreach($userData as $key => $value) {
- $userDatas[$key] = $value;
- }
- $userDatas = list_domain_image($userDatas,['avatar']);
- }
- $partyUserList = [
- 'member_list' => $userDatas,
- 'online_num' => !empty($userDatas) ? count($userDatas) : 0,
- ];
- $result['data'] = $partyUserList;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- }
|