实现效果:统计未读的信息
控制器:
/** * 获取团长发布消息总数,没有阅读的 * @return array|mixed|string */ public function getTeamMessageCount(){ $result = [ 'status' => false, 'data' => '', 'msg' => '' ]; $token = input('param.token'); if (empty($token)){ $result['status'] = true; $result['data'] = 0; $result['msg'] = "获取成功"; return $result; } $userId = getUserIdByToken($token); $userModel = new UserModel(); $userInfo = $userModel->getUserWxById($userId); $userTeamId = 0; if (isset($userInfo['status']) && $userInfo['status']){ $userTeamId = isset($userInfo['data']['user_team_id']) ? $userInfo['data']['user_team_id'] : 0; } $informationModel = new Information(); return $informationModel->getMessageCountNotReader($userId, $userTeamId); }
跳转到用户模型层代码:
/** * 查询用户信息 根据用id * @param $id * @return void */ public function getUserWxById($id){ $result = [ 'status' => false, 'data' => [], 'msg' => '' ]; $userInfo = $this->where(array('id' => $id))->find(); if ($userInfo){ $result['data'] = $userInfo; $result['status'] = true; }else{ return error_code(11004);//没有找到此用户 } return $result; }
跳转到消息模型层代码:
public function getMessageCountNotReader($userId, $teamId){ $return = [ 'status' => false, 'msg' => '', 'data' => '' ]; $count = $this->alias('mm') ->field("mm.*") ->leftJoin("user_read_team_message_log ut", "ut.team_massage_id = mm.id and ut.user_id=".$userId) ->where(['mm.team_id'=> $teamId, 'mm.status' => 2]) ->whereNull('ut.team_massage_id') ->count(); $return['status'] = true; $return['data'] = $count; return $return; }
数据表的创建: