tp实现未读信息

2022/4/13 6:14:02

本文主要是介绍tp实现未读信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

实现效果:统计未读的信息

控制器:

   /**
     *  获取团长发布消息总数,没有阅读的
     * @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;
    }

数据表的创建:

 

 

 



这篇关于tp实现未读信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程