123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace Community\Lib;
- use Dever;
- class Chat
- {
- # 生成key
- private function getKey($uid, $to_uid)
- {
- if ($uid > $to_uid) {
- return $uid . '_' . $to_uid;
- } else {
- return $to_uid . '_' . $uid;
- }
- }
- # 加入聊天信息
- public function add($uid, $to_uid, $collection_id, $text, $pic, $day = false)
- {
- $data['uid'] = $uid;
- $data['to_uid'] = $to_uid;
- $data['collection_id'] = $collection_id;
- $user = Dever::db('community/chat_user')->one($data);
- $data['key'] = $this->getKey($uid, $to_uid);
- if (!$user) {
- Dever::db('community/chat_user')->insert($data);
- }
- $data['text'] = $text;
- $data['pic'] = $pic;
- if ($day) {
- $data['day'] = $day;
- }
- return Dever::db('community/chat_info')->insert($data);
- }
- # 获取聊天对象
- public function getUser($uid, $collection_id, $times = false)
- {
- $data['to_uid'] = $uid;
- $data['collection_id'] = $collection_id;
- $user = Dever::db('community/chat_user')->getAll($data);
- # 获取最新的一条聊天记录
- if ($user) {
- $time = time();
- foreach ($user as $k => $v) {
- $where['uid'] = $v['uid'];
- $where['collection_id'] = $collection_id;
- $chat = Dever::db('community/chat_info')->getNew($where);
- $user[$k]['user'] = Dever::load('user/lib/info')->get($v['uid'], $collection_id, true);
- if ($chat) {
- if ($chat['pic'] && !$chat['text']) {
- $chat['text'] = '[图片]';
- } else {
- $chat['text'] = strip_tags($chat['text']);
- }
- $date = Dever::load('collection/lib/times')->getDate($chat['day'], $chat['cdate'], $times, true);
- $chat['day'] = $date[0];
- $chat['date'] = $date[1];
- }
- $user[$k]['chat'] = $chat;
- }
- }
- return $user;
- }
- # 获取聊天记录
- public function getInfo($uid, $to_uid, $collection_id, $times = false)
- {
- $where['key'] = $this->getKey($uid, $to_uid);
- $where['collection_id'] = $collection_id;
- $data['chat'] = Dever::db('community/chat_info')->getAll($where);
- if ($data['chat']) {
- $data['user'][$uid] = Dever::load('user/lib/info')->get($uid, $collection_id, true);
- $data['user'][$to_uid] = Dever::load('user/lib/info')->get($to_uid, $collection_id, true);
- $data['title'] = $data['user'][$to_uid]['username'];
- foreach ($data['chat'] as $k => $v) {
- $data['chat'][$k]['date'] = Dever::load('collection/lib/times')->getDate($v['day'], $v['cdate'], $times);
- $data['chat'][$k]['self'] = false;
- if ($uid == $v['uid']) {
- $data['chat'][$k]['self'] = true;
- }
- }
- $data['chat'] = array_reverse($data['chat']);
- }
- return $data;
- }
- }
|