多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
数据表结构 ![](https://img.kancloud.cn/38/29/3829f086fb3acc6f561e95843be791ef_934x287.png) ~~~ /**聊天好友列表 * @param $user_id * @return array * @throws \Exception */ public static function jobChatList($user_id){ $message_list = self::where('type','text')->where('user_id', $user_id)->orWhere('to_user_id', $user_id)->select(['user_id', 'to_user_id','content','input_time','type','is_read'])->orderBy('input_time','asc')->get(); $to_user_ids = []; $lastMessages = []; foreach ($message_list as $message) { //发送方 if ($message->to_user_id != $user_id) { $to_user_ids[] = $message->to_user_id; $lastMessages[$message->to_user_id] = [ 'content' => $message->content, 'time' => $message->input_time, 'is_read' => self::where([['user_id',$user_id], ['to_user_id', $message->to_user_id]])->value('is_read') ?? 0 ]; } //接收方 if ($message->user_id != $user_id) { $to_user_ids[] = $message->user_id; $lastMessages[$message->user_id] = [ 'content' => $message->content, 'time' => $message->input_time, 'is_read' => self::where([['to_user_id',$user_id], ['user_id', $message->to_user_id]])->value('is_read') ?? 0 ]; } } $friends = apiPaginate(MemberList::whereIn('user_id', $to_user_ids)->paginate(50)); $friends['list'] = $friends['list']->map(function ($item) use ($lastMessages, $user_id){ $return_data = [ 'nickname' => $item->nickname, 'avatar' => MemberList::getUser($item->user_id)['avatar'], 'text' => $lastMessages[$item->user_id]['content'], 'unread_count' => self::where([['user_id',$item->user_id], ['to_user_id', $user_id], ['is_read',0]])->count(), 'to_user_id' => $item->user_id, 'time' => $lastMessages[$item->user_id]['time'], 'time_text' => date_format_convert($lastMessages[$item->user_id]['time']), ]; if ($lastMessages[$item->user_id]['is_read'] == 1){ $return_data['text'] = 'قارشى تەرەپ ئۇچۇرىڭىزنى كۆردى'; } return $return_data; }); /*排序*/ $sort_list = $friends['list']->toArray(); $values = array_column($sort_list,'time'); array_multisort($values,SORT_DESC, $sort_list); /*排序完毕*/ $new_list['list'] = $sort_list; $new_list['page'] = $friends['page']; return $new_list; } ~~~ 结果: ![](https://img.kancloud.cn/8a/e6/8ae61955c915d305d3a929fb2fbd96d3_333x322.png)