Room.php 3.9 KB

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