| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\ChatMessageModel;
- use App\Models\ConfigModel;
- /**
- * 聊天-服务类
- * Class ChatMessageService
- * @package App\Services
- */
- class ChatMessageService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * ChatMessageService constructor.
- */
- public function __construct()
- {
- $this->model = new ChatMessageModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 绑定用户
- * @param $fd
- * @param $data
- * @return bool
- */
- public function bind($fd, $data)
- {
- $userId = isset($data['from_uid']) ? intval($data['from_uid']) : 0;
- if ($userId <= 0) {
- $this->error = '1013';
- return false;
- }
- RedisService::set("chats:bind:{$userId}", ['fd' => $fd, 'user_id' => $userId], 86400);
- return true;
- }
- /**
- * 获取列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1, 'a.status' => 1];
- $type = isset($params['type']) ? $params['type'] : 1;
- $chatKey = isset($params['chat_key']) ? $params['chat_key'] : '';
- $fromUid = isset($params['from_uid']) ? $params['from_uid'] : 0;
- $toUid = isset($params['to_uid']) ? $params['to_uid'] : 0;
- $orderNo = isset($params['order_no']) ? $params['order_no'] : '';
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- if ($chatKey) {
- $where['a.chat_key'] = $chatKey;
- }
- if ($fromUid > 0) {
- $where['a.from_uid'] = $fromUid;
- }
- if ($toUid > 0) {
- $where['a.to_uid'] = $toUid;
- }
- if ($orderNo) {
- $where['a.order_no'] = $orderNo;
- }
- $list = $this->model->from('chat_message as a')
- ->leftJoin('member as m', 'm.id', '=', 'a.from_uid')
- ->leftJoin('member as m1', 'm1.id', '=', 'a.to_uid')
- ->where($where)
- ->where(function ($query) use ($params) {
- $id = isset($params['id']) ? $params['id'] : 0;
- if ($id > 0) {
- $query->where('a.id', '<', $id);
- }
- })
- ->where(function ($query) use ($params) {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $query->where('a.from_uid', '=', $userId)->orWhere('a.to_uid', '=', $userId);
- }
- })
- ->select(['a.*', 'm.username as from_username', 'm.avatar as from_avatar', 'm1.username as to_username', 'm1.avatar as to_avatar'])
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['from_username_text'] = $item['from_username'] ? format_account($item['from_username']) : '客服';
- $item['from_username'] = $item['from_username'] ? $item['from_username'] : '客服';
- $item['to_username_text'] = $item['to_username'] ? format_account($item['to_username']) : '客服';
- $item['to_username'] = $item['to_username'] ? $item['to_username'] : '客服';
- $item['message_url'] = $item['message_type'] == 2 ? get_image_url($item['message']) : '';
- $item['from_avatar'] = $item['from_avatar'] ? get_image_url($item['from_avatar']) : '';
- $item['to_avatar'] = $item['to_avatar'] ? get_image_url($item['to_avatar']) : '';
- $item['time_text'] = $item['create_time'] ? format_time(strtotime($item['create_time'])) : '刚刚';
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId == $item['to_uid']) {
- // 已读
- $this->model->where(['id' => $item['id']])->update(['is_read' => 1, 'update_time' => time()]);
- }
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 消息列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getNewList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1, 'a.status' => 1];
- $type = isset($params['type']) ? $params['type'] : 1;
- $chatKey = isset($params['chat_key']) ? $params['chat_key'] : '';
- $fromUid = isset($params['from_uid']) ? $params['from_uid'] : 0;
- $toUid = isset($params['to_uid']) ? $params['to_uid'] : 0;
- $orderNo = isset($params['order_no']) ? $params['order_no'] : '';
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- if ($chatKey) {
- $where['a.chat_key'] = $chatKey;
- }
- if ($fromUid > 0) {
- $where['a.from_uid'] = $fromUid;
- }
- if ($toUid > 0) {
- $where['a.to_uid'] = $toUid;
- }
- if ($orderNo) {
- $where['a.order_no'] = $orderNo;
- }
- $list = $this->model->from('chat_message as a')
- ->leftJoin('member as m', 'm.id', '=', 'a.from_uid')
- ->leftJoin('member as m1', 'm1.id', '=', 'a.to_uid')
- ->where($where)
- ->where(function ($query) use ($params) {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $query->where('a.from_uid', '=', $userId)->orWhere('a.to_uid', '=', $userId);
- }
- })
- ->select(['a.id', 'a.from_uid', 'a.to_uid', 'a.type', 'a.message_type', 'a.data_type', 'a.message', 'a.chat_key', 'a.order_no', 'a.create_time', 'm.username as from_username', 'm.avatar as from_avatar', 'm1.username as to_username', 'm1.avatar as to_avatar'])
- ->groupBy('a.chat_key')
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['from_username_text'] = $item['from_username'] ? format_account($item['from_username']) : '客服';
- $item['to_username_text'] = $item['to_username'] ? format_account($item['to_username']) : '客服';
- $item['from_avatar'] = $item['from_avatar'] ? get_image_url($item['from_avatar']) : '';
- $item['to_avatar'] = $item['to_avatar'] ? get_image_url($item['to_avatar']) : '';
- $item['time_text'] = $item['create_time'] ? format_time(strtotime($item['create_time'])) : '刚刚';
- $item['info'] = $this->getTempInfo($item['chat_key']);
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 消息列表
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getWaitList($params, $pageSize = 15)
- {
- $where = ['a.mark' => 1, 'a.status' => 1];
- $type = isset($params['type']) ? $params['type'] : 1;
- $chatKey = isset($params['chat_key']) ? $params['chat_key'] : '';
- $fromUid = isset($params['from_uid']) ? $params['from_uid'] : 0;
- $toUid = isset($params['to_uid']) ? $params['to_uid'] : 0;
- $orderNo = isset($params['order_no']) ? $params['order_no'] : '';
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- if ($chatKey) {
- $where['a.chat_key'] = $chatKey;
- }
- if ($fromUid > 0) {
- $where['a.from_uid'] = $fromUid;
- }
- if ($toUid > 0) {
- $where['a.to_uid'] = $toUid;
- }
- if ($orderNo) {
- $where['a.order_no'] = $orderNo;
- }
- $list = $this->model->from('chat_message as a')
- ->leftJoin('member as m', 'm.id', '=', 'a.from_uid')
- ->leftJoin('member as m1', 'm1.id', '=', 'a.to_uid')
- ->where($where)
- ->where(function ($query) use ($params) {
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $query->where('a.to_uid', '=', $userId);
- }
- })
- ->select(['a.id', 'a.from_uid', 'a.to_uid', 'a.type', 'a.message_type', 'a.data_type', 'a.message', 'a.chat_key', 'a.order_no', 'a.create_time', 'm.username as from_username', 'm.avatar as from_avatar', 'm1.username as to_username', 'm1.avatar as to_avatar'])
- ->groupBy('a.chat_key')
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- foreach ($list['data'] as &$item) {
- $item['from_username_text'] = $item['from_username'] ? format_account($item['from_username']) : '客服';
- $item['to_username_text'] = $item['to_username'] ? format_account($item['to_username']) : '客服';
- $item['from_avatar'] = $item['from_avatar'] ? get_image_url($item['from_avatar']) : '';
- $item['to_avatar'] = $item['to_avatar'] ? get_image_url($item['to_avatar']) : '';
- $item['time_text'] = $item['create_time'] ? format_time(strtotime($item['create_time'])) : '刚刚';
- $item['info'] = $this->getTempInfo($item['chat_key']);
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 消息数量
- * @param $userId
- * @return int[]
- */
- public function getNewCount($userId)
- {
- $counts = ['notice' => 0, 'message' => 0];
- $counts['notice'] = (int)$this->model->where(['status' => 1, 'mark' => 1, 'type' => 3])
- ->where(function ($query) use ($userId) {
- $query->where(['to_uid' => $userId]);
- })->count('id');
- $result = $this->model->where(['status' => 1, 'mark' => 1, 'type' => 1])
- ->where(function ($query) use ($userId) {
- $query->where(['to_uid' => $userId]);
- })->select(['chat_key'])->groupBy('chat_key')->get();
- $result = $result ? $result->toArray() : [];
- $counts['message'] = count($result);
- return $counts;
- }
- /**
- * 消息数量
- * @param $userId
- * @return int[]
- */
- public function getUnReadCount($userId)
- {
- $count = $this->model->where(['status' => 1, 'mark' => 1, 'type' => 1,'is_read'=>2])
- ->where(function ($query) use ($userId) {
- $query->where(['to_uid' => $userId]);
- })->count('id');
- return $count;
- }
- /**
- * 获取最新的聊天消息
- * @param $chatKey 聊天窗口标识
- * @return array|mixed
- */
- public function getTempInfo($chatKey)
- {
- $cacheKey = "caches:chats:temp:{$chatKey}";
- $info = RedisService::get($cacheKey);
- if ($info) {
- return $info;
- }
- $info = $this->model->from('chat_message as a')
- ->leftJoin('member as m', 'm.id', '=', 'a.from_uid')
- ->leftJoin('member as m1', 'm1.id', '=', 'a.to_uid')
- ->where(['a.chat_key' => $chatKey, 'a.status' => 1, 'a.mark' => 1])
- ->select(['a.id', 'a.message','a.from_uid','a.to_uid', 'a.is_read', 'a.message_type', 'a.create_time', 'm.username as from_username', 'm.avatar as from_avatar', 'm1.username as to_username', 'm1.avatar as to_avatar'])
- ->orderBy('create_time', 'desc')
- ->orderBy('id', 'desc')
- ->first();
- $info = $info? $info->toArray() : [];
- if ($info) {
- $info['from_username_text'] = $info['from_username'] ? format_account($info['from_username']) : '客服';
- $info['to_username_text'] = $info['to_username'] ? format_account($info['to_username']) : '客服';
- $info['message_url'] = $info['message_type'] == 2 ? get_image_url($info['message']) : '';
- $info['time_text'] = $info['create_time'] ? format_time(strtotime($info['create_time'])) : '刚刚';
- RedisService::set($cacheKey, $info, rand(3, 5));
- }
- return $info;
- }
- /**
- * 添加或编辑
- * @return array
- */
- public function saveData($data)
- {
- $data = [
- 'type' => isset($data['type']) ? $data['type'] : 1,
- 'message_type' => isset($data['message_type']) ? $data['message_type'] : 1,
- 'from_uid' => isset($data['from_uid']) ? $data['from_uid'] : 1,
- 'to_uid' => isset($data['to_uid']) ? $data['to_uid'] : 1,
- 'order_no' => isset($data['order_no']) ? $data['order_no'] : '',
- 'chat_key' => getChatKey($data['from_uid'], $data['to_uid']),
- 'message' => isset($data['message']) ? $data['message'] : '',
- 'create_time' => time(),
- 'update_time' => time(),
- 'is_read' => 2,
- 'status' => 1,
- 'mark' => 1,
- ];
- // 清除旧记录
- $time = ConfigService::make()->getConfigByCode('chat_expired_time');
- $time = $time? $time : 30;
- $this->model->where('create_time','<=', $time * 24 * 3600)->delete();
- return $this->model->insertGetId($data);
- }
- /**
- * 推送保存消息,如订单消息
- * @param $data 消息数据
- * @return int|number
- */
- public function pushMessage($data)
- {
- return $this->model->edit($data);
- }
- /**
- * 获取最新
- * @param $businessId
- * @return array
- */
- public function getOrderNotify($businessId)
- {
- $cacheKey = "caches:orders:notify:{$businessId}";
- $info = RedisService::get($cacheKey);
- if($info){
- return $info;
- }
- $info = $this->model->from('chat_message as a')
- ->leftJoin('trade_order as b','b.order_no','=','a.order_no')
- ->where('a.create_time','>=', time() - 10)
- ->where(['a.to_uid'=> $businessId,'a.is_read'=>2,'a.mark'=>1,'a.status'=>1])
- ->whereIn('a.type',[2])
- ->select(['a.id','a.type','a.from_uid','a.to_uid','a.order_no','a.message','b.type as order_type'])
- ->orderBy('a.create_time','desc')
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $this->model->where(['id'=> $info['id']])->update(['is_read'=>1,'update_time'=>time()]);
- RedisService::set($cacheKey, $info, rand(3,5));
- }
- return $info? $info : [];
- }
- }
|