Normal.php 4.4 KB
<?php

namespace app\api\controller\v7\notices;

use app\admin\model\Admin;
use app\admin\model\notices\Article;
use app\admin\model\notices\Noticesnormal;
use app\admin\model\User;
use app\common\controller\Api;

/**
 * 通知相关接口
 * Class Normal
 * @package app\api\controller\v7\notices
 */
class Normal extends Api
{
//    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    //获取通知消息
    public function getNotices(){
        $params = $this->request->get();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $where = [
            'rec_type' => 1,
            'rec_id' => $this->auth->id
        ];
        if (isset($params['notice_type'])){
            $where['notice_type'] = $params['notice_type'];
        }
        $noticesnormalModel = new Noticesnormal();
        $total = $noticesnormalModel->where($where)->count();
        $notices = $noticesnormalModel
            ->where($where)
            ->order(['createtime' => 'desc'])
            ->page($page, $limit)
            ->select();
        foreach ($notices as $notice){
            //查询接收者信息
            $userInfo = $this->auth->getUser();
            $notice['rec_info'] = [
                'id' => $userInfo['id'],
                'username' => $userInfo['username'],
                'nickname' => $userInfo['nickname'],
                'email' => $userInfo['email'],
                'avatar' => $userInfo['avatar']
            ];
            //查询发送者信息
            if ($notice['send_type'] == 1){
                $sendInfo = User::get($notice['send_id']);
            }elseif($notice['send_type'] == 2){
                $sendInfo = Admin::get($notice['send_id']);
            }
            $notice['send_info'] = [
                'id' => $sendInfo['id'],
                'username' => $sendInfo['username'],
                'nickname' => $sendInfo['nickname'],
                'email' => $sendInfo['email'],
                'avatar' => $sendInfo['avatar']
            ];
        }
        $result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'rows' => $notices,
        ];
        $this->success('成功', $result);
    }

    //获取公告详情
    public function noticeDetail(){
        $params = $this->request->get();
        if (empty($params['id'])){
            $this->error('公告id必须');
        }
        $articleModel = new Article();
        $article = $articleModel
            ->with('depart')
            ->where(['article.id' => $params['id']])
            ->find();
        if (!$article){
            $this->error('公告不存在');
        }
        $this->success('成功', $article);
    }

    //获取公告列表
    public function getArticle(){
        $params = $this->request->get();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;
        $articleModel = new Article();
        $total = $articleModel->count();
        $list = $articleModel
            ->with('depart')
            ->order(['article.id' => 'desc'])
            ->page($page, $limit)
            ->select();
        $result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'rows' => $list,
        ];
        $this->success('成功', $result);
    }

    //未读通知公告统计
    public function unreadNum(){
        $params = $this->request->get();
        $where = [
            'read' => 0,
            'rec_type' => 1,
            'rec_id' => $this->auth->id
        ];
        if (isset($params['notice_type'])){
            $where['notice_type'] = $params['notice_type'];
        }
        $noticesnormalModel = new Noticesnormal();
        $total = $noticesnormalModel->where($where)->count();
        $this->success('成功', ['unread_num' => $total]);
    }

    //设置消息为已读
    public function read(){
        $params = $this->request->post();
        if (empty($params['id'])){
            $this->error('通知id必须');
        }
        $notice = Noticesnormal::get($params['id']);
        if (!$notice){
            $this->error('通知不存在');
        }

        if ($notice['rec_type'] != 1 || $notice['rec_id'] != $this->auth->id){
            $this->error('无操作权限');
        }

        $notice['read'] = 1;
        $notice->save();
        $this->success('操作成功');
    }
}