| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace addons\fastchat\library;
- use think\Db;
- use think\Exception;
- /**
- *
- */
- class Chat
- {
- /**
- * @var object 对象实例
- */
- protected static $instance;
- // 服务端连接实例
- protected $connection;
- // 发送消息使用的服务号
- protected $service_user;
- // 接受消息的用户
- protected $user;
- // 接受消息的管理员
- protected $admin;
- // 接受消息的助教
- protected $massager;
- // 接受消息的用户组
- protected $user_group;
- // 接受消息的管理组
- protected $admin_group;
- // 错误消息
- protected $error;
- function __construct($service_user)
- {
- $this->service_user = $service_user;
- $fastchat_config = get_addon_config('fastchat');
- $this->connection = @stream_socket_client('tcp://127.0.0.1:' . ($fastchat_config['register_port'] + 100));
- if (!$this->connection) {
- $this->error = '内部推送通道连接失败,请确保Workerman服务已经启动!';
- }
- }
- /**
- * 初始化
- * @access public
- * @return Chat
- */
- public static function init($service_user = 1)
- {
- if (is_null(self::$instance)) {
- self::$instance = new static($service_user);
- }
- return self::$instance;
- }
- public function user($data)
- {
- $this->user = $this->array_to_string($data);
- return $this;
- }
- /**
- * 解析数组为字符串,方便传输
- * @access public
- * @return string
- */
- public function array_to_string($data)
- {
- if (is_array($data)) {
- if (is_array(current($data))) {
- $this->error = '数组参数只能是一维数组!如 array(1,2,3)';
- return '';
- } else {
- $data = implode(',', $data);
- }
- }
- return trim($data, ',');
- }
- public function admin($data)
- {
- $this->admin = $this->array_to_string($data);
- return $this;
- }
- public function massager($data)
- {
- $this->massager = $this->array_to_string($data);
- return $this;
- }
- public function user_group($data)
- {
- $this->user_group = $this->array_to_string($data);
- return $this;
- }
- public function admin_group($data)
- {
- $this->admin_group = $this->array_to_string($data);
- return $this;
- }
- public function send($content)
- {
- if ($this->error) {
- return array('errcode' => -1, 'errmsg' => $this->error);
- }
- $service_user = Db::name('fastchat_service_user')
- ->where('id', $this->service_user)
- ->where('status', '1')
- ->where('deletetime', NULL)
- ->find();
- if (!$service_user) {
- return array('errcode' => -1, 'errmsg' => '服务账号不存在!');
- }
- // 准备服务号访问令牌
- $service_user['token'] = \fast\Random::uuid();
- // 加密 token
- $config = \think\Config::get('token');
- $token = hash_hmac($config['hashalgo'], $service_user['token'], $config['key']);
- Db::name('fastchat_service_user')
- ->where('id', $service_user['id'])
- ->update(['token' => $token]);
- $send_text = [
- 'c' => 'Message',
- 'a' => 'push_message',
- 'data' => [
- 'service_user' => $service_user['id'],
- 'content' => $content,
- 'user' => $this->user,
- 'admin' => $this->admin,
- 'massager' => $this->massager,
- 'user_group' => $this->user_group,
- 'admin_group' => $this->admin_group,
- 'token' => $service_user['token']
- ]
- ];
- $send_text = json_encode($send_text) . "\n";
- if (fwrite($this->connection, $send_text) !== false) {
- fclose($this->connection);
- return array('errcode' => 0);
- }
- }
- }
|