Chat.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\fastchat\library;
  3. use think\Db;
  4. use think\Exception;
  5. /**
  6. *
  7. */
  8. class Chat
  9. {
  10. /**
  11. * @var object 对象实例
  12. */
  13. protected static $instance;
  14. // 服务端连接实例
  15. protected $connection;
  16. // 发送消息使用的服务号
  17. protected $service_user;
  18. // 接受消息的用户
  19. protected $user;
  20. // 接受消息的管理员
  21. protected $admin;
  22. // 接受消息的助教
  23. protected $massager;
  24. // 接受消息的用户组
  25. protected $user_group;
  26. // 接受消息的管理组
  27. protected $admin_group;
  28. // 错误消息
  29. protected $error;
  30. function __construct($service_user)
  31. {
  32. $this->service_user = $service_user;
  33. $fastchat_config = get_addon_config('fastchat');
  34. $this->connection = @stream_socket_client('tcp://127.0.0.1:' . ($fastchat_config['register_port'] + 100));
  35. if (!$this->connection) {
  36. $this->error = '内部推送通道连接失败,请确保Workerman服务已经启动!';
  37. }
  38. }
  39. /**
  40. * 初始化
  41. * @access public
  42. * @return Chat
  43. */
  44. public static function init($service_user = 1)
  45. {
  46. if (is_null(self::$instance)) {
  47. self::$instance = new static($service_user);
  48. }
  49. return self::$instance;
  50. }
  51. public function user($data)
  52. {
  53. $this->user = $this->array_to_string($data);
  54. return $this;
  55. }
  56. /**
  57. * 解析数组为字符串,方便传输
  58. * @access public
  59. * @return string
  60. */
  61. public function array_to_string($data)
  62. {
  63. if (is_array($data)) {
  64. if (is_array(current($data))) {
  65. $this->error = '数组参数只能是一维数组!如 array(1,2,3)';
  66. return '';
  67. } else {
  68. $data = implode(',', $data);
  69. }
  70. }
  71. return trim($data, ',');
  72. }
  73. public function admin($data)
  74. {
  75. $this->admin = $this->array_to_string($data);
  76. return $this;
  77. }
  78. public function massager($data)
  79. {
  80. $this->massager = $this->array_to_string($data);
  81. return $this;
  82. }
  83. public function user_group($data)
  84. {
  85. $this->user_group = $this->array_to_string($data);
  86. return $this;
  87. }
  88. public function admin_group($data)
  89. {
  90. $this->admin_group = $this->array_to_string($data);
  91. return $this;
  92. }
  93. public function send($content)
  94. {
  95. if ($this->error) {
  96. return array('errcode' => -1, 'errmsg' => $this->error);
  97. }
  98. $service_user = Db::name('fastchat_service_user')
  99. ->where('id', $this->service_user)
  100. ->where('status', '1')
  101. ->where('deletetime', NULL)
  102. ->find();
  103. if (!$service_user) {
  104. return array('errcode' => -1, 'errmsg' => '服务账号不存在!');
  105. }
  106. // 准备服务号访问令牌
  107. $service_user['token'] = \fast\Random::uuid();
  108. // 加密 token
  109. $config = \think\Config::get('token');
  110. $token = hash_hmac($config['hashalgo'], $service_user['token'], $config['key']);
  111. Db::name('fastchat_service_user')
  112. ->where('id', $service_user['id'])
  113. ->update(['token' => $token]);
  114. $send_text = [
  115. 'c' => 'Message',
  116. 'a' => 'push_message',
  117. 'data' => [
  118. 'service_user' => $service_user['id'],
  119. 'content' => $content,
  120. 'user' => $this->user,
  121. 'admin' => $this->admin,
  122. 'massager' => $this->massager,
  123. 'user_group' => $this->user_group,
  124. 'admin_group' => $this->admin_group,
  125. 'token' => $service_user['token']
  126. ]
  127. ];
  128. $send_text = json_encode($send_text) . "\n";
  129. if (fwrite($this->connection, $send_text) !== false) {
  130. fclose($this->connection);
  131. return array('errcode' => 0);
  132. }
  133. }
  134. }