MessagesController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Jobs\System\ChatExpandTypeCheckJob;
  4. use App\Models\WxChat;
  5. use App\Models\WxNotice;
  6. use App\Models\User\WxUser;
  7. use App\Wen\Utils\FieldUtils;
  8. use App\Wen\Utils\GatewayUtils;
  9. use App\Wen\Utils\Settings;
  10. use App\Wen\Utils\UserUtils;
  11. use Illuminate\Http\Request;
  12. class MessagesController extends BaseController
  13. {
  14. /**
  15. * 消息页数据
  16. * @param Request $request
  17. * @return \Illuminate\Http\JsonResponse
  18. */
  19. public function getMessages(Request $request)
  20. {
  21. $uid = $request->uid;
  22. // 获取所有相关通知
  23. $notices = WxNotice::where('user_id', $uid)
  24. ->where('is_read', 0)
  25. ->whereIn('notice_type', [100, 0, 1, 7, 8, 101, 2, 3, 5, 102, 4, 6])
  26. ->orderByRaw('created_at DESC')
  27. ->get(['created_at', 'title', 'notice_type']);
  28. // 初始化计数和内容
  29. $noticeSystemCount = 0;
  30. $noticeSystemText = '还没有新通知';
  31. $noticeSystemDate = '';
  32. $noticeLikeCollectCount = 0;
  33. $noticeLikeCollectText = '还没有新的赞和收藏';
  34. $noticeLikeCollectDate = '';
  35. $noticeCommentCount = 0;
  36. $noticeCommentText = '还没有新的评论和@';
  37. $noticeCommentDate = '';
  38. // 处理通知
  39. foreach ($notices as $notice) {
  40. if (in_array($notice->notice_type, [100, 0, 1, 7, 8])) {
  41. $noticeSystemCount++;
  42. if ($noticeSystemCount == 1) {
  43. $noticeSystemText = $notice->title ?? '';
  44. $noticeSystemDate = format_datetime($notice->created_at);
  45. }
  46. } elseif (in_array($notice->notice_type, [101, 2, 3, 5])) {
  47. $noticeLikeCollectCount++;
  48. if ($noticeLikeCollectCount == 1) {
  49. $noticeLikeCollectText = $notice->title;
  50. $noticeLikeCollectDate = format_datetime($notice->created_at);
  51. }
  52. } elseif (in_array($notice->notice_type, [102, 4, 6])) {
  53. $noticeCommentCount++;
  54. if ($noticeCommentCount == 1) {
  55. $noticeCommentText = $notice->title;
  56. $noticeCommentDate = format_datetime($notice->created_at);
  57. }
  58. }
  59. }
  60. // 获取圈子未读数
  61. $noticeCircleCount = UserUtils::user_circle_unread_count($uid);
  62. $noticeCircleText = '没有新的'.env('circle_call', '圈子').'动态';
  63. if ($noticeCircleCount > 0) {
  64. $noticeCircleText = '有'.$noticeCircleCount.'条笔记新发布';
  65. }
  66. // 准备返回数据
  67. $list = [
  68. ['noticeSystemText' => $noticeSystemText, 'noticeSystemCount' => $noticeSystemCount, 'noticeSystemDate' => $noticeSystemDate],
  69. ['noticeLikeCollectText' => $noticeLikeCollectText, 'noticeLikeCollectCount' => $noticeLikeCollectCount, 'noticeLikeCollectDate' => $noticeLikeCollectDate],
  70. ['noticeCommentText' => $noticeCommentText, 'noticeCommentCount' => $noticeCommentCount, 'noticeCommentDate' => $noticeCommentDate],
  71. ['noticeCircleText' => $noticeCircleText, 'noticeCircleCount' => $noticeCircleCount, 'noticeCircleDate' => '']
  72. ];
  73. $data = [
  74. 'common' => [
  75. 'isFollow' => (WxUser::where('id', $uid)->value('weixin_mp_openid') ? true : false),
  76. 'client_id' => GatewayUtils::uid2client_id($uid)
  77. ],
  78. 'list' => $list
  79. ];
  80. return $this->success($data);
  81. }
  82. /**
  83. * 通知详情页数据
  84. * @param Request $request
  85. * 系统通知:0,1, 7, 8
  86. * 赞和收藏通知:2,3,5
  87. * 评论和充电通知:4,6
  88. * @return \Illuminate\Http\JsonResponse
  89. */
  90. public function getDetailsMessages(Request $request)
  91. {
  92. $uid = $request->uid;
  93. $type = $request->type;
  94. $limit = $request->limit;
  95. if(_empty_($limit)){
  96. $limit = 10;
  97. }else{
  98. $limit = _between_($limit, 1, 100);
  99. }
  100. if ($type == 0) {
  101. $list = WxNotice::where('user_id', $uid)
  102. ->whereIn('notice_type', [100,0, 1, 7, 8])
  103. ->orderByRaw('created_at DESC')
  104. ->paginate($limit);
  105. } else if ($type == 1) {
  106. $list = WxNotice::where('user_id', $uid)
  107. ->whereIn('notice_type', [101,2, 3, 5])
  108. ->orderByRaw('created_at DESC')
  109. ->paginate($limit);
  110. } else if ($type == 2) {
  111. $list = WxNotice::where('user_id', $uid)
  112. ->whereIn('notice_type', [102,4, 6])
  113. ->orderByRaw('created_at DESC')
  114. ->paginate($limit);
  115. }
  116. $list->map(function ($v)use($type) {
  117. $v->icon = Settings::get('app_notice_type_icon_'.$v->icon_type, '');
  118. if(_empty_($v->icon) && $v->icon_type != 1000){
  119. $v->icon = Settings::get('app_notice_type_icon_1000', '');
  120. }
  121. return $v;
  122. });
  123. return $this->success($list);
  124. }
  125. /**
  126. * 已读对应类通知
  127. * @param Request $request
  128. * @return \Illuminate\Http\JsonResponse
  129. */
  130. public function readMessages(Request $request)
  131. {
  132. $uid = $request->uid;
  133. $type = $request->type;
  134. if ($type == 0) {
  135. $notice = WxNotice::where('user_id', $uid)
  136. ->whereIn('notice_type', [100, 0, 1, 7, 8])
  137. ->update(['is_read' => 1]);
  138. return $this->success($notice);
  139. } else if ($type == 1) {
  140. $notice = WxNotice::where('user_id', $uid)
  141. ->whereIn('notice_type', [101, 2, 3, 5])
  142. ->update(['is_read' => 1]);
  143. return $this->success($notice);
  144. } else if ($type == 2) {
  145. $notice = WxNotice::where('user_id', $uid)
  146. ->whereIn('notice_type', [102, 4, 6])
  147. ->update(['is_read' => 1]);
  148. return $this->success($notice);
  149. }
  150. }
  151. /**
  152. * 查询用户聊天记录列表
  153. * @param Request $request
  154. * @return \Illuminate\Http\JsonResponse
  155. */
  156. public function getUserChatList(Request $request)
  157. {
  158. $uid = $request->uid;
  159. if($uid && $uid > 0){
  160. $data = WxChat::where([['user_id','=', $uid],['chat_state','=', 0]])
  161. ->orWhere([['object_id','=', $uid],['chat_state','=', 0]])->orderBy('id', 'desc')->get();
  162. // 二维数组去重
  163. $res = array();
  164. $data->map(function ($chat) use (&$res, $uid){
  165. if($chat['object_id'] == $uid){
  166. $relative_user_id = $chat['user_id'];
  167. }else{
  168. $relative_user_id = $chat['object_id'];
  169. }
  170. if(!isset($res[$relative_user_id])){
  171. if($chat['chat_audio_url'] && $chat['chat_audio_length'] > 0){
  172. $chat_content = '语音消息';
  173. }else if($chat['chat_image']){
  174. $chat_content = '图片消息';
  175. }else if(_array_key($chat, 'refer_id', 0) > 0){
  176. $chat_content = '转发笔记';
  177. if($chat->expand_type != 1 || $chat->expand_id != $chat->refer_id){
  178. ChatExpandTypeCheckJob::dispatch();
  179. }
  180. }else if(_array_key($chat, 'contact_apply_id', 0) > 0){
  181. $chat_content = '联系信息申请';
  182. if($chat->expand_type != 2 || $chat->expand_id != $chat->contact_apply_id){
  183. ChatExpandTypeCheckJob::dispatch();
  184. }
  185. }else if(_array_key($chat, 'adoption_apply_id', 0) > 0){
  186. $chat_content = '领养申请';
  187. if($chat->expand_type != 3 || $chat->expand_id != $chat->adoption_apply_id){
  188. ChatExpandTypeCheckJob::dispatch();
  189. }
  190. }else{
  191. if($chat['expand_type'] > 0){
  192. if($chat['expand_type'] == 1){
  193. $chat_content = '转发笔记';
  194. $chat['refer_id'] = $chat['expand_id'];
  195. }else if($chat['expand_type'] == 2){
  196. $chat_content = '联系信息申请';
  197. $chat['contact_apply_id'] = $chat['expand_id'];
  198. }else if($chat['expand_type'] == 3){
  199. $chat_content = '领养申请';
  200. $chat['adoption_apply_id'] = $chat['expand_id'];
  201. }else if($chat['expand_type'] == 4){
  202. $chat_content = '商品卡片';
  203. $chat['shop_good_id'] = $chat['expand_id'];
  204. }else if($chat['expand_type'] == 5){
  205. $chat_content = '闲置商品';
  206. $chat['used_good_id'] = $chat['expand_id'];
  207. }else if($chat['expand_type'] == 8){
  208. $chat_content = '选票活动';
  209. $chat['voter_id'] = $chat['expand_id'];
  210. }else if($chat['expand_type'] == 101){
  211. $chat_content = '笔记审核推送';
  212. }else if($chat['expand_type'] == 102){
  213. $chat_content = '评论审核推送';
  214. }else if($chat['expand_type'] == 103){
  215. $chat_content = '组局审核推送';
  216. }else if($chat['expand_type'] == 104){
  217. $chat_content = '选票审核推送';
  218. }else if($chat['expand_type'] == 105){
  219. $chat_content = '选手审核推送';
  220. }else if($chat['expand_type'] == 106){
  221. $chat_content = '闲置审核推送';
  222. }else if($chat['expand_type'] == 107){
  223. $chat_content = env('circle_call', '圈子').'审核推送';
  224. }else if($chat['expand_type'] == 108){
  225. $chat_content = '学生认证审核推送';
  226. }else if($chat['expand_type'] == 109){
  227. $chat_content = '身份认证审核推送';
  228. }else if($chat['expand_type'] == 110){
  229. $chat_content = '俱乐部审核推送';
  230. }else{
  231. return $this->fail(200006, [], '数据库expand_type不合法');
  232. }
  233. }else {
  234. $chat_content = $chat['chat_content'];
  235. }
  236. }
  237. $assistant_user = (int)Settings::get('app_notice_assistant_user', 0);
  238. //print_r($relative_user_id);exit;
  239. $is_mobile = WxUser::where('id', $relative_user_id)->first(['is_member','is_online_status']);
  240. //print_r($user_mobile_info);exit;
  241. //print_r($is_mobile);exit;
  242. $is_online = 0;
  243. if (!empty($is_mobile->is_member) && $is_mobile->is_member == 1) {
  244. if ($is_mobile['is_online_status'] == 1) {
  245. $is_online = 1;
  246. } else {
  247. $is_online = 0;
  248. }
  249. } else {
  250. $is_online = 0;
  251. }
  252. //print_r($is_online);exit;
  253. if(_empty_($assistant_user) || $assistant_user < 0){
  254. if ($is_online == 0) {
  255. $chat_content = (UserUtils::is_user_online($relative_user_id) ? '<span style="color: #36d9a0;">[在线]</span> ' : '[离线] ') . $chat_content;
  256. }
  257. }else{
  258. if($relative_user_id == $assistant_user){
  259. }else{
  260. if ($is_online == 0) {
  261. $chat_content = (UserUtils::is_user_online($relative_user_id) ? '<span style="color: #36d9a0;">[在线]</span> ' : '[离线] ') . $chat_content;
  262. }
  263. }
  264. }
  265. $obj = [
  266. 'user' => WxUser::where('id', $relative_user_id)->first(FieldUtils::userInfoColums()),
  267. 'datetime' => format_datetime($chat['created_at']),
  268. 'chat_content' => _mini_emoji($chat_content, true),
  269. 'read_count' => ($chat['is_read'] == 0 && $chat['object_id'] == $uid) ? 1 : 0,
  270. ];
  271. $res[$relative_user_id] = $obj;
  272. }else{
  273. if($chat['is_read'] == 0 && $chat['object_id'] == $uid){
  274. $res[$relative_user_id]['read_count'] += 1;
  275. }
  276. }
  277. });
  278. return $this->success(array_values($res));
  279. }else{
  280. return $this->success([]);
  281. }
  282. }
  283. /**
  284. * 查询用户是否有未读信息
  285. * @param Request $request
  286. * @return \Illuminate\Http\JsonResponse
  287. */
  288. public function getSysMessageCount(Request $request)
  289. {
  290. $count = 0;
  291. $uid = $request->uid;
  292. if ($uid) {
  293. $noticeCount = WxNotice::where([['user_id','=', $uid], ['is_read','=', 0]])->count();
  294. $chatCount = WxChat::where([['object_id','=', $uid],['is_read','=', 0],['chat_state', '=', 0]])->count();
  295. $circleCount = UserUtils::user_circle_unread_count($uid);
  296. $count = $noticeCount + $chatCount + $circleCount;
  297. }
  298. return $this->success($count);
  299. }
  300. }