request->param('page', 1); $limit = $this->request->param('limit', 10); $accountId = $this->getUserInfo('s2_accountId'); if (empty($accountId)){ return ResponseHelper::error('请先登录'); } $chatroomList = Db::table('s2_wechat_chatroom')->alias('wc') ->join(['s2_wechat_message' => 'm'], 'wc.id = m.wechatChatroomId', 'LEFT') ->where(['wc.accountId' => $accountId,'m.type' => 2,' wc.isDeleted' => 0]) ->order('m.id desc') ->group('m.wechatChatroomId') ->page($page, $limit) ->select(); $friendIds = Db::table('s2_wechat_friend') ->where(['accountId' => $accountId,' isDeleted' => 0]) ->group('id') ->column('id'); $friendList = Db::table('s2_wechat_message') ->where(['type' => 1]) ->whereIn('wechatFriendId',$friendIds) ->order('id desc') ->group('wechatFriendId') ->page($page, $limit) ->select(); $list = array_merge($chatroomList,$friendList); // 按createTime字段从大到小排序 usort($list, function($a, $b) { return $b['createTime'] <=> $a['createTime']; }); foreach ($list as $k=>&$v){ $v['createTime'] = !empty($v['createTime']) ? date('Y-m-d H:i:s',$v['createTime']) : ''; $v['wechatTime'] = !empty($v['wechatTime']) ? date('Y-m-d H:i:s',$v['wechatTime']) : ''; if (!empty($v['wechatFriendId'])){ $friend = Db::table('s2_wechat_friend') ->where(['id'=>$v['wechatFriendId']]) ->field('id,nickname,avatar') ->find(); $v['msgInfo'] = $friend; $v['unreadCount'] = Db::table('s2_wechat_message') ->where(['wechatFriendId' => $v['wechatFriendId'],'isRead' => 0]) ->count(); } if (!empty($v['wechatChatroomId'])){ $chatroom = Db::table('s2_wechat_chatroom') ->where(['id'=>$v['wechatChatroomId']]) ->field('id,nickname,chatroomAvatar as avatar') ->find(); $v['msgInfo'] = $chatroom; $v['unreadCount'] = Db::table('s2_wechat_message') ->where(['wechatChatroomId' => $v['wechatChatroomId'],'isRead' => 0]) ->count(); } } unset($v); return ResponseHelper::success($list); } public function readMessage(){ $wechatFriendId = $this->request->param('wechatFriendId', ''); $wechatChatroomId = $this->request->param('wechatChatroomId', ''); $accountId = $this->getUserInfo('s2_accountId'); if (empty($accountId)){ return ResponseHelper::error('请先登录'); } if (empty($wechatChatroomId) && empty($wechatFriendId)){ return ResponseHelper::error('参数缺失'); } $where = []; if (!empty($wechatChatroomId)){ $where[] = ['wechatChatroomId','=',$wechatChatroomId]; } if (!empty($wechatFriendId)){ $where[] = ['wechatFriendId','=',$wechatFriendId]; } Db::table('s2_wechat_message')->where($where)->update(['isRead' => 1]); return ResponseHelper::success([]); } }