MessagesController.php 15 KB

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