You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Room.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\webscoket;
  3. use Swoole\Table as SwooleTable;
  4. use think\swoole\Table;
  5. /**
  6. * 房间管理
  7. * Class Room
  8. * @package app\webscoket
  9. */
  10. class Room
  11. {
  12. /**
  13. * 类型 只有kefu和admin有区别
  14. * @var string
  15. */
  16. protected $type = '';
  17. /**
  18. * fd前缀
  19. * @var string
  20. */
  21. protected $tableFdPrefix = 'ws_fd_';
  22. /**
  23. *
  24. * @var array
  25. */
  26. protected $room = [];
  27. /**
  28. * @var \Redis
  29. */
  30. protected $cache;
  31. /**
  32. *
  33. */
  34. const USER_INFO_FD_PRE = 'socket_user_list';
  35. const TYPE_NAME = 'socket_user_type';
  36. /**
  37. * 设置缓存
  38. * @param $cache
  39. * @return $this
  40. */
  41. public function setCache($cache)
  42. {
  43. $this->cache = $cache;
  44. return $this;
  45. }
  46. /**
  47. * 设置表
  48. * @param string $type
  49. * @return $this
  50. */
  51. public function type(string $type)
  52. {
  53. $this->type = $type;
  54. return $this;
  55. }
  56. /**
  57. * 获取表实例
  58. * @return SwooleTable
  59. */
  60. public function getTable()
  61. {
  62. return app()->make(Table::class)->get('user');
  63. }
  64. /**
  65. * 添加fd
  66. * @param string $key fd
  67. * @param int $uid 用户uid
  68. * @param int $to_uid 当前聊天人的uid
  69. * @param int $tourist 是否为游客
  70. * @return mixed
  71. */
  72. public function add(string $key, int $uid, int $to_uid = 0, int $tourist = 0)
  73. {
  74. $nowkey = $this->tableFdPrefix . $key;
  75. $data = ['fd' => $key, 'type' => $this->type ?: 'user', 'uid' => $uid, 'to_uid' => $to_uid, 'tourist' => $tourist];
  76. $res = $this->getTable()->set($nowkey, $data);
  77. return $res;
  78. }
  79. /**
  80. * 修改数据
  81. * @param string $key
  82. * @param null $field
  83. * @param null $value
  84. * @return bool|mixed
  85. */
  86. public function update(string $key, $field = null, $value = null)
  87. {
  88. $nowkey = $this->tableFdPrefix . $key;
  89. $res = true;
  90. if (is_array($field)) {
  91. $res = $this->getTable()->set($nowkey, $field);
  92. } else if (!is_array($field) && $value !== null) {
  93. $data = $this->getTable()->get($nowkey);
  94. if (!$data) {
  95. return false;
  96. }
  97. $data[$field] = $value;
  98. $res = $this->getTable()->set($nowkey, $data);
  99. }
  100. return $res;
  101. }
  102. /**
  103. * 重置
  104. * @return $this
  105. */
  106. public function reset()
  107. {
  108. $this->type = $this->typeReset;
  109. return $this;
  110. }
  111. /**
  112. * 删除
  113. * @param string $key
  114. * @return mixed
  115. */
  116. public function del(string $key)
  117. {
  118. $nowkey = $this->tableFdPrefix . $key;
  119. return $this->getTable()->del($nowkey);
  120. }
  121. /**
  122. * 是否存在
  123. * @param string $key
  124. * @return mixed
  125. */
  126. public function exist(string $key)
  127. {
  128. return $this->getTable()->exist($this->tableFdPrefix . $key);
  129. }
  130. /**
  131. * 获取fd的所有信息
  132. * @param string $key
  133. * @return array|bool|mixed
  134. */
  135. public function get(string $key, string $field = null)
  136. {
  137. return $this->getTable()->get($this->tableFdPrefix . $key, $field);
  138. }
  139. /**
  140. * fd 获取 uid
  141. * @param $key
  142. * @return mixed
  143. */
  144. public function fdByUid($key)
  145. {
  146. return $this->getTable()->get($this->tableFdPrefix . $key, 'uid');
  147. }
  148. }