Room.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * @return mixed
  79. */
  80. public function add(string $key, int $uid, int $to_uid = 0)
  81. {
  82. $nowkey = $this->tableFdPrefix . $key;
  83. $data = ['fd' => $key, 'type' => $this->type ?: 'user', 'uid' => $uid, 'to_uid' => $to_uid, 'to_group' => 0];
  84. return $this->getTable()->set($nowkey, $data);
  85. }
  86. /**
  87. * 修改数据
  88. * @param string $key
  89. * @param null $field
  90. * @param null $value
  91. * @return bool|mixed
  92. */
  93. public function update(string $key, $field = null, $value = null)
  94. {
  95. $nowkey = $this->tableFdPrefix . $key;
  96. $res = true;
  97. if (is_array($field)) {
  98. $res = $this->getTable()->set($nowkey, $field);
  99. } else if (!is_array($field) && $value !== null) {
  100. $data = $this->getTable()->get($nowkey);
  101. if (!$data) {
  102. return false;
  103. }
  104. $data[$field] = $value;
  105. $res = $this->getTable()->set($nowkey, $data);
  106. }
  107. return $res;
  108. }
  109. /**
  110. * 删除
  111. * @param string $key
  112. * @return mixed
  113. */
  114. public function del(string $key)
  115. {
  116. $nowkey = $this->tableFdPrefix . $key;
  117. return $this->getTable()->del($nowkey);
  118. }
  119. /**
  120. * 是否存在
  121. * @param string $key
  122. * @return mixed
  123. */
  124. public function exist(string $key)
  125. {
  126. return $this->getTable()->exist($this->tableFdPrefix . $key);
  127. }
  128. /**
  129. * 获取fd的所有信息
  130. * @param string $key
  131. * @param string|null $field
  132. * @return array|bool|mixed
  133. */
  134. public function get(string $key, string $field = null)
  135. {
  136. return $this->getTable()->get($this->tableFdPrefix . $key, $field);
  137. }
  138. /**
  139. * fd 获取 uid
  140. * @param $key
  141. * @return mixed
  142. */
  143. public function fdByUid($key)
  144. {
  145. return $this->getTable()->get($this->tableFdPrefix . $key, 'uid');
  146. }
  147. }