Room.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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)
  82. {
  83. $nowkey = $this->tableFdPrefix . $key;
  84. $data = ['fd' => $key, 'type' => $this->type ?: 'user', 'uid' => $uid, 'to_uid' => $to_uid];
  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. * @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. * @param string|null $field
  134. * @return array|bool|mixed
  135. */
  136. public function get(string $key, string $field = null)
  137. {
  138. return $this->getTable()->get($this->tableFdPrefix . $key, $field);
  139. }
  140. /**
  141. * fd 获取 uid
  142. * @param $key
  143. * @return mixed
  144. */
  145. public function fdByUid($key)
  146. {
  147. return $this->getTable()->get($this->tableFdPrefix . $key, 'uid');
  148. }
  149. }