Chat.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/11/12
  6. * @time: 16:31
  7. */
  8. namespace app\controller\api\chat;
  9. use app\common\ApiBaseController;
  10. use app\Request;
  11. use app\services\chat\ChatGroupMemberServices;
  12. use app\services\chat\ChatGroupServices;
  13. use app\services\chat\ChatLogServices;
  14. use app\services\decoration\DecorationDesignerServices;
  15. use app\services\decoration\DecorationSalespersonServices;
  16. use app\services\decoration\DecorationWorkerServices;
  17. use app\services\system\store\SystemStoreServices;
  18. use app\services\user\UserServices;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. class Chat extends ApiBaseController
  23. {
  24. private $groupMemberService;
  25. private $groupService;
  26. public function __construct(Request $request, ChatLogServices $service, ChatGroupMemberServices $groupMemberService, ChatGroupServices $groupService)
  27. {
  28. parent::__construct($request);
  29. $this->service = $service;
  30. $this->groupMemberService = $groupMemberService;
  31. $this->groupService = $groupService;
  32. }
  33. /**
  34. * 聊天列表
  35. * @param Request $request
  36. * @return mixed
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException
  40. */
  41. public function index(Request $request)
  42. {
  43. $user_chat = $this->service->search()
  44. ->where('uid|to_uid', $request->uid())
  45. // ->where('add_time', '>=', time() - 7 * 86400)
  46. ->where('group_id', 0)
  47. ->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')
  48. ->group('user_id,to_uid_type,uid_type')
  49. ->select();
  50. foreach ($user_chat as &$v) {
  51. $v['is_group'] = 0;
  52. $v['f_info'] = $this->service->getInfo($v['to_uid_type'], $v['user_id']);
  53. $user_uid = $request->uid();
  54. $to_uid = $v['user_id'];
  55. $v['last_msn'] = $this->service->search()->where(function ($query) use ($user_uid, $to_uid) {
  56. $query->where(function ($q) use ($user_uid, $to_uid) {
  57. $q->where('uid', $user_uid)->where('to_uid', $to_uid);
  58. })->whereOr(function ($q) use ($user_uid, $to_uid) {
  59. $q->where('uid', $to_uid)->where('to_uid', $user_uid);
  60. });
  61. })->order('add_time desc,id desc')->find();
  62. }
  63. $groups = $this->groupMemberService->search()->where('uid', $request->uid())->column('chat_group_id');
  64. $groups = $this->groupService->search()->where('id', 'in', $groups)->column('id');
  65. $group_chat = $this->service->search()->where('group_id', 'in', $groups)
  66. // ->where('add_time', '>=', time() - 7 * 86400)
  67. ->where('group_id', '>', 0)
  68. ->field('group_id,sum(if(find_in_set(' . $request->uid() . ',group_see),0,1)) as unread_count,max(add_time) as last_time')
  69. ->group('group_id')->select();
  70. foreach ($group_chat as &$v) {
  71. $v['is_group'] = 1;
  72. $v['last_msn'] = $this->service->search()->where('group_id', $v['group_id'])
  73. ->order('add_time desc,id desc')->find();
  74. $v['at'] = $this->service->search()->where('group_id', $v['group_id'])
  75. ->where('at', $request->uid())
  76. ->where('find_in_set(' . $request->uid() . ',group_see)', '<=', 0)
  77. ->where('find_in_set(' . $request->uid() . ',at)')
  78. ->count();
  79. $v['group_info'] = $this->groupService->get($v['group_id']);
  80. $v['identity'] = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $v['group_id'])->value('identity');
  81. }
  82. $log_list = array_merge($user_chat->toArray(), $group_chat->toArray());
  83. usort($log_list, function ($a, $b) {
  84. return -($a['last_time'] <=> $b['last_time']);
  85. });
  86. return $this->success('ok', $log_list);
  87. }
  88. /**
  89. * 聊天记录
  90. * @param $uid
  91. * @param $type
  92. * @param Request $request
  93. * @return mixed
  94. * @throws DataNotFoundException
  95. * @throws DbException
  96. * @throws ModelNotFoundException
  97. */
  98. public function chatLog(Request $request)
  99. {
  100. $page = $request->get('page', 1);
  101. $limit = $request->get('limit', 30);
  102. $user_uid = $request->uid();
  103. list($uid, $user_type, $type) = $this->request->getMore([
  104. ['uid', 0],
  105. ['user_type', 0],
  106. ['type', 0]
  107. ], true);
  108. $to_uid = $uid;
  109. $log_list = $this->service->search()->where(function ($query) use ($user_uid, $to_uid, $type, $user_type) {
  110. $query->where(function ($q) use ($user_uid, $to_uid, $type, $user_type) {
  111. $q->where('uid', $user_uid)->where('to_uid', $to_uid)->where('to_uid_type', $type)->where('uid_type', $user_type);
  112. })->whereOr(function ($q) use ($user_uid, $to_uid, $type, $user_type) {
  113. $q->where('uid', $to_uid)->where('to_uid', $user_uid)->where('uid_type', $type)->where('to_uid_type', $user_type);
  114. });
  115. })->page($page, $limit)->order('add_time desc,id desc')->select();
  116. $f_info = $this->service->getInfo($type, $to_uid);
  117. $u_info = $this->service->getInfo($user_type, $user_uid);
  118. return $this->success('ok', compact('log_list', 'f_info', 'u_info'));
  119. }
  120. public function setRead(Request $request)
  121. {
  122. list($uid, $user_type, $type) = $this->request->postMore([
  123. ['uid', 0],
  124. ['user_type', 0],
  125. ['type', 0]
  126. ], true);
  127. $this->service->search()->where('uid', $uid)->where('to_uid', $request->uid())->where('uid_type', $user_type)->where('to_uid_type', $type)->update(['status' => 1]);
  128. return $this->success('ok');
  129. }
  130. public function groupChatLog($group_id, Request $request)
  131. {
  132. if (!$user = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $group_id)->find()) {
  133. return $this->error('你没有权限');
  134. }
  135. $group = $this->groupService->search()->where('id', $group_id)->find();
  136. if (!$group) return $this->error('群不存在');
  137. $page = $request->get('page', 1);
  138. $limit = $request->get('limit', 30);
  139. $log_list = $this->service->search()->where('group_id', $group_id)
  140. ->page($page, $limit)
  141. ->where('add_time', '>=', $user['add_time'])
  142. ->order('add_time desc,id desc')
  143. ->select();
  144. foreach ($log_list as &$v) {
  145. $member = $this->groupMemberService->search()->where('chat_group_id', $group_id)->where('uid', $v['uid'])->find();
  146. $v['f_info'] = $this->service->getInfo($member['identity'], $member['uid']);
  147. $v['member_info'] = $member;
  148. }
  149. return $this->success('ok', compact('log_list'));
  150. }
  151. public function setGroupRead($group_id, Request $request)
  152. {
  153. if (!$user = $this->groupMemberService->search()->where('uid', $request->uid())->where('chat_group_id', $group_id)->find()) {
  154. return $this->error('你没有权限');
  155. }
  156. $group = $this->groupService->search()->where('id', $group_id)->find();
  157. if (!$group) return $this->error('群不存在');
  158. $this->service->search()->where('group_id', $group_id)
  159. ->where('!find_in_set(' . $request->uid() . ',group_see)')
  160. ->update(['group_see' => ['exp', 'CONCAT(group_see,",' . $request->uid() . '")']]);
  161. return $this->success();
  162. }
  163. }