123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/12
- * @time: 16:31
- */
- namespace app\controller\api\chat;
- use app\common\ApiBaseController;
- use app\Request;
- use app\services\chat\ChatGroupMemberServices;
- use app\services\chat\ChatGroupServices;
- use app\services\chat\ChatLogServices;
- use app\services\decoration\DecorationDesignerServices;
- use app\services\decoration\DecorationSalespersonServices;
- use app\services\decoration\DecorationWorkerServices;
- use app\services\system\store\SystemStoreServices;
- use app\services\user\UserServices;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class Chat extends ApiBaseController
- {
- private $groupMemberService;
- private $groupService;
- public function __construct(Request $request, ChatLogServices $service, ChatGroupMemberServices $groupMemberService, ChatGroupServices $groupService)
- {
- parent::__construct($request);
- $this->service = $service;
- $this->groupMemberService = $groupMemberService;
- $this->groupService = $groupService;
- }
- /**
- * 聊天列表
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function index(Request $request)
- {
- $user_chat = $this->service->search()
- ->where('uid|to_uid', $request->uid())
- // ->where('add_time', '>=', time() - 7 * 86400)
- ->where('group_id', 0)
- ->field('if(uid=' . $request->uid() . ',to_uid,uid) as user_id,sum(if(status=0 && to_uid=' . $request->uid() . ',1,0)) as unread_count,max(add_time) as last_time,to_uid_type,uid_type')
- ->group('user_id,to_uid_type,uid_type')
- ->select();
- foreach ($user_chat as &$v) {
- $v['is_group'] = 0;
- $v['f_info'] = $this->service->getInfo($v['to_uid_type'], $v['user_id']);
- $user_uid = $request->uid();
- $to_uid = $v['user_id'];
- $v['last_msn'] = $this->service->search()->where(function ($query) use ($user_uid, $to_uid) {
- $query->where(function ($q) use ($user_uid, $to_uid) {
- $q->where('uid', $user_uid)->where('to_uid', $to_uid);
- })->whereOr(function ($q) use ($user_uid, $to_uid) {
- $q->where('uid', $to_uid)->where('to_uid', $user_uid);
- });
- })->order('add_time desc,id desc')->find();
- }
- $groups = $this->groupMemberService->search()->where('uid', $request->uid())->column('chat_group_id');
- $groups = $this->groupService->search()->where('id', 'in', $groups)->column('id');
- $group_chat = $this->service->search()->where('group_id', 'in', $groups)
- // ->where('add_time', '>=', time() - 7 * 86400)
- ->where('group_id', '>', 0)
- ->field('group_id,sum(if(find_in_set(' . $request->uid() . ',group_see),0,1)) as unread_count,max(add_time) as last_time')
- ->group('group_id')->select();
- foreach ($group_chat as &$v) {
- $v['is_group'] = 1;
- $v['last_msn'] = $this->service->search()->where('group_id', $v['group_id'])
- ->order('add_time desc,id desc')->find();
- $v['at'] = $this->service->search()->where('group_id', $v['group_id'])
- ->where('at', $request->uid())
- ->where('find_in_set(' . $request->uid() . ',group_see)', '<=', 0)
- ->where('find_in_set(' . $request->uid() . ',at)')
- ->count();
- $v['group_info'] = $this->groupService->get($v['group_id']);
- $v['identity'] = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $v['group_id'])->value('identity');
- }
- $log_list = array_merge($user_chat->toArray(), $group_chat->toArray());
- usort($log_list, function ($a, $b) {
- return -($a['last_time'] <=> $b['last_time']);
- });
- return $this->success('ok', $log_list);
- }
- /**
- * 聊天记录
- * @param $uid
- * @param $type
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function chatLog(Request $request)
- {
- $page = $request->get('page', 1);
- $limit = $request->get('limit', 30);
- $user_uid = $request->uid();
- list($uid, $user_type, $type) = $this->request->getMore([
- ['uid', 0],
- ['user_type', 0],
- ['type', 0]
- ], true);
- $to_uid = $uid;
- $log_list = $this->service->search()->where(function ($query) use ($user_uid, $to_uid, $type, $user_type) {
- $query->where(function ($q) use ($user_uid, $to_uid, $type, $user_type) {
- $q->where('uid', $user_uid)->where('to_uid', $to_uid)->where('to_uid_type', $type)->where('uid_type', $user_type);
- })->whereOr(function ($q) use ($user_uid, $to_uid, $type, $user_type) {
- $q->where('uid', $to_uid)->where('to_uid', $user_uid)->where('uid_type', $type)->where('to_uid_type', $user_type);
- });
- })->page($page, $limit)->order('add_time desc,id desc')->select();
- $f_info = $this->service->getInfo($type, $to_uid);
- $u_info = $this->service->getInfo($user_type, $user_uid);
- return $this->success('ok', compact('log_list', 'f_info', 'u_info'));
- }
- public function setRead(Request $request)
- {
- list($uid, $user_type, $type) = $this->request->postMore([
- ['uid', 0],
- ['user_type', 0],
- ['type', 0]
- ], true);
- $this->service->search()->where('uid', $uid)->where('to_uid', $request->uid())->where('uid_type', $user_type)->where('to_uid_type', $type)->update(['status' => 1]);
- return $this->success('ok');
- }
- public function groupChatLog($group_id, Request $request)
- {
- if (!$user = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $group_id)->find()) {
- return $this->error('你没有权限');
- }
- $group = $this->groupService->search()->where('id', $group_id)->find();
- if (!$group) return $this->error('群不存在');
- $page = $request->get('page', 1);
- $limit = $request->get('limit', 30);
- $log_list = $this->service->search()->where('group_id', $group_id)
- ->page($page, $limit)
- ->where('add_time', '>=', $user['add_time'])
- ->order('add_time desc,id desc')
- ->select();
- foreach ($log_list as &$v) {
- $member = $this->groupMemberService->search()->where('chat_group_id', $group_id)->where('uid', $v['uid'])->find();
- $v['f_info'] = $this->service->getInfo($member['identity'], $member['uid']);
- $v['member_info'] = $member;
- }
- return $this->success('ok', compact('log_list'));
- }
- public function setGroupRead($group_id, Request $request)
- {
- if (!$user = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $group_id)->find()) {
- return $this->error('你没有权限');
- }
- $group = $this->groupService->search()->where('id', $group_id)->find();
- if (!$group) return $this->error('群不存在');
- $this->service->search()->where('group_id', $group_id)
- ->where('!find_in_set(' . $request->uid() . ',group_see)')
- ->update(['group_see' => ['exp', 'CONCAT(group_see,",' . $request->uid() . '")']]);
- return $this->success();
- }
- }
|