123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace app\common\service;
- use think\Db;
- use think\Exception;
- use Redis;
- 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"=>1])->select();
- $userInfoArr = [];
- if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;*/
- if($sqlPartyList) {
- foreach($sqlPartyList as $k => $v) {
- // 加入缓存排序
- //rediskey:派对热度
- $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"]:[];
- // 加入缓存
- //rediskey:派对详情
- $this->redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
- }
- //rediskey:派对热度
- $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;
- }
- /**
- * 房间用户信息
- * @return void
- */
- public function getPartyUserList($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '',
- 'data' => [],
- ];
- try {
- $partyId = isset($params['party_id']) ? $params['party_id'] : 0;
- //rediskey:派对用户列表
- // $userIds = $this->redis->zRange("party_user_".$partyId,0,-1);
- $userIds = $this->redis->hGetAll("online_" . $partyId);
- $field = 'id,avatar,nickname';
- $userWhere['id'] = ['in', $userIds];
- // $userWhere['is_stealth'] = 0;
- $userModel = new \app\common\model\User();
- $userData = $userModel->field($field)->where($userWhere)->limit(20)->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' => count($userIds),
- ];
- $result['data'] = $partyUserList;
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- }
|