123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- # 聊天服务
- namespace Community\Src;
- use Dever;
- use Collection\Lib\Core;
- Dever::apply('lib/gateway', 'im_gateway_client');
- use Im_gateway_client\Lib\Gateway;
- class Chat extends Core
- {
- public function __construct()
- {
- parent::__construct();
- # 测试用
- $uid = Dever::input('uid');
- if ($uid == 1) {
- $this->uid = 2;
- }
- }
- private function connect()
- {
- Gateway::$registerAddress = 'dl.shemic.com:1238';
- }
- # 初始化,绑定client_id
- public function init()
- {
- $this->connect();
- $client_id = Dever::input('client_id');
- if ($client_id) {
- Gateway::bindUid($client_id, $this->uid);
- }
- return 'ok';
- }
- # 加入分组
- public function joinGroup()
- {
-
- }
- # 发送消息
- public function send()
- {
- $uid = Dever::input('uid');
- $msg = Dever::input('msg');
- $pic = Dever::input('pic');
- if (!$uid || $uid == $this->uid) {
- Dever::alert('你要发给谁?');
- }
- if (!$msg && !$pic) {
- Dever::alert('没有任何消息');
- }
- Dever::load('community/lib/chat')->add($this->uid, $uid, $this->id, $msg, $pic, $this->day);
- $this->connect();
- # 谁发给谁,发的什么
- $this->push($this->uid, $uid, $msg, $pic);
- $data['msg'] = $this->msg($this->uid, $this->uid, $msg, $pic);
- return $data;
- }
- # 获取聊天记录
- public function getList()
- {
- $uid = Dever::input('uid');
- if (!$uid || $uid == $this->uid) {
- Dever::alert('你要发给谁?');
- }
- $data = Dever::load('community/lib/chat')->getInfo($this->uid, $uid, $this->id, $this->times);
- $info = $this->checkInfo();
- if ($info) {
- $button = $this->button($info);
- $data['bgcolor'] = $button['bgcolor'];
- }
- return $data;
- }
- # 发送消息给用户
- public function push($from_uid, $uid, $msg, $pic)
- {
- if (Gateway::isUidOnline($uid)) {
- $send = $this->msg($from_uid, $uid, $msg, $pic);
- $send = Dever::json_encode($send);
- Gateway::sendToUid($uid, $send);
- }
- return $this;
- }
- # 获取消息体
- public function msg($from_uid, $uid, $msg, $pic)
- {
- $self = false;
- if ($from_uid == $uid) {
- $self = true;
- }
- $send = array();
- $send['uid'] = $from_uid;
- $send['self'] = $self;
- $send['type'] = 'msg';
- $send['text'] = $msg;
- $send['pic'] = $pic;
- $send['date'] = Dever::load('collection/lib/times')->getDate($this->day, time(), $this->times);
- return $send;
- }
- }
|