RoomService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\common\service;
  3. use think\Db;
  4. use think\Exception;
  5. use Redis;
  6. class RoomService
  7. {
  8. private $model = '';
  9. private $redis = '';
  10. private $roomTypeArr = [];
  11. /**
  12. * 初始化方法
  13. */
  14. public function __construct()
  15. {
  16. $this->model = new \app\common\model\Party();
  17. $redis = new Redis();
  18. $redisconfig = config("redis");
  19. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  20. if ($redisconfig['redis_pwd']) {
  21. $redis->auth($redisconfig['redis_pwd']);
  22. }
  23. if($redisconfig['redis_selectdb'] > 0){
  24. $redis->select($redisconfig['redis_selectdb']);
  25. }
  26. $this->redis = $redis;
  27. $this->roomTypeArr = [1=>"party",2=>"live"];
  28. }
  29. /**
  30. * 房间设置缓存
  31. * @return void
  32. */
  33. public function roomRedis($params=[])
  34. {
  35. $result = [
  36. 'status' => 1,
  37. 'msg' => '',
  38. 'data' => [],
  39. ];
  40. try {
  41. $room_type = isset($params['room_type']) ? $params['room_type'] : 0;
  42. if (!empty($room_type)) {
  43. //$userModel = new \app\common\model\User();
  44. // 直接从数据库获取所有数据
  45. $where = [];
  46. $where["a.status"] = 1;
  47. // $where["a.is_online"] = 1;
  48. $where["a.room_type"] = $room_type;
  49. $sqlPartyList = $this->model->alias("a")->field("a.*,b.name as type_name")
  50. ->join("hx_party_type b","a.party_type = b.id","left")->where($where)->select();
  51. /*$userList = $userModel->field("id,nickname,avatar")->where(["status"=>1])->select();
  52. $userInfoArr = [];
  53. if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;*/
  54. if($sqlPartyList) {
  55. foreach($sqlPartyList as $k => $v) {
  56. // 加入缓存排序
  57. //rediskey:派对热度
  58. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
  59. // 设置冠名
  60. //$sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
  61. // 设置房主头像
  62. //$sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
  63. // 加入缓存
  64. //rediskey:派对详情
  65. $this->redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
  66. }
  67. //rediskey:派对热度
  68. $redisPartyRankList = $this->redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  69. $result['data'] = $redisPartyRankList;
  70. }
  71. }
  72. } catch (Exception $e) {
  73. $result['status'] = 0;
  74. $result['msg'] = $e->getMessage();
  75. }
  76. return $result;
  77. }
  78. /**
  79. * 房间用户信息
  80. * @return void
  81. */
  82. public function getPartyUserList($params=[])
  83. {
  84. $result = [
  85. 'status' => 1,
  86. 'msg' => '',
  87. 'data' => [],
  88. ];
  89. try {
  90. $partyId = isset($params['party_id']) ? $params['party_id'] : 0;
  91. //rediskey:派对用户列表
  92. // $userIds = $this->redis->zRange("party_user_".$partyId,0,-1);
  93. $userIds = $this->redis->hGetAll("online_" . $partyId);
  94. $field = 'id,avatar,nickname';
  95. $userWhere['id'] = ['in', $userIds];
  96. // $userWhere['is_stealth'] = 0;
  97. $userModel = new \app\common\model\User();
  98. $userData = $userModel->field($field)->where($userWhere)->limit(20)->select();
  99. $userDatas = [];
  100. if (!empty($userData)) {
  101. foreach($userData as $key => $value) {
  102. $userDatas[$key] = $value;
  103. }
  104. $userDatas = list_domain_image($userDatas,['avatar']);
  105. }
  106. $partyUserList = [
  107. 'member_list' => $userDatas,
  108. 'online_num' => count($userIds),
  109. ];
  110. $result['data'] = $partyUserList;
  111. } catch (Exception $e) {
  112. $result['status'] = 0;
  113. $result['msg'] = $e->getMessage();
  114. }
  115. return $result;
  116. }
  117. }