Message.php 5.9 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/30
 * Time: 17:17
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

/**
 * 平台消息模块
 */
class Message extends Api
{
    /**
     * @ApiTitle    (平台消息列表)
     * @ApiSummary  (平台消息列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/index)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            'title'://标题,
            'user_id'://用户ID
            'is_read': //是否已读(0未读1已读),
            'content'://内容,
            'type'://类型(1普通2活动3奖品)
            'num'://未读消息数量
            'total'://通知数量
            'createtime'://创建时间
            'already'://已读
        }
        })
     */
    public function index()
    {
        $user_id = $this->getUserId();
        $data['list'] = Db::name('message')
            ->where('user_id',$user_id)
            ->whereOr('user_id','null')
            ->order('is_read,createtime desc')
            ->select();
        foreach ($data['list'] as &$v){
            $v['createtime'] = date('m月d日',$v['createtime']);
        }

        //查询出所有的平台信息
        $read = Db::name('message')->where('user_id','null')->select();
        //循环判断平台消息是否已读
        $nums = 0;
        foreach ($read as $val){
            $info = Db::name('read')->where('user_id',$user_id)->where('message_id',$val['id'])->find();
            if(empty($info)){
                $nums += 1;
            }
        }
        $num = Db::name('message')
            ->where('is_read',0)
            ->where('user_id',$user_id)
//            ->where(function ($query)use($user_id){
//                $query->where('user_id',$user_id)
//                    ->whereOr('user_id','null');
//            })
            ->count();
        $data['num'] = $num + $nums;
        //通知数量
        $data['total'] = Db::name('message')
            ->where('user_id',$user_id)
            ->whereOr('user_id','null')
            ->count();
        $data['already'] = $data['total'] - $data['num'];
        $this->success('success',$data);
    }

    /**
     * @ApiTitle    (平台消息详情)
     * @ApiSummary  (平台消息详情)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/detail)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=false, description="平台消息ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            'title'://标题,
            'user_id'://用户ID
            'is_read': //是否已读(0未读1已读),
            'content'://内容,
            'type'://类型(1普通2活动3奖品)
            'createtime'://创建时间
        }
    })
     */
    public function detail()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error('缺少必要参数');
        }

        //判断该消息是否为平台通知
        $info = Db::name('message')->where('id',$id)->find();
        if($info['user_id'] == ''){
            //是
            $read['user_id'] = $user_id;
            $read['message_id'] = $id;
            $read['createtime'] = time();
            //判断该记录是否已经添加过了
            $have = Db::name('read')->where('user_id',$read['user_id'])->where('message_id',$read['message_id'])->find();
            if(empty($have)){
                //没有添加过情况
                Db::name('read')->insertGetId($read);
            }
            $data = Db::name('message')
                ->where('id',$id)
                ->field('id,title,content,createtime,type')
                ->find();
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        }else{
            //不是
            if($info['content'] == '' && $info['type'] == 1){
                //判断是否为认证审核消息
                $ren = Db::name('attestation')
                    ->alias('a')
                    ->join('user b','a.user_id = b.id')
                    ->join('farm c','a.farm_id = c.id')
                    ->where('a.user_id',$info['user_id'])
                    ->field('a.*,c.id as farm_id,c.address,c.project_num,c.name as farm_name,c.identity')
                    ->find();
                //查询入驻消息 拿出 申请人名字
                $shenqing = Db::name('personage')
                    ->where('user_id',$info['user_id'])
                    ->find();
                $list['name'] = $shenqing['name'];
                $list['identity'] = $ren['identity'];
                $list['farm_name'] = $ren['farm_name'];
                $list['project_num'] = $ren['project_num'];
                $list['createtime'] = date('Y-m-d H:i:s',$info['createtime']);
                $list['time'] = date('Y-m-d',$ren['createtime']);
                $list['address'] = $ren['address'];
                $list['institution'] = $ren['institution'];
                $list['title'] = $info['title'];
                $list['status'] = $ren['status'];
                $list['type'] = $info['type'];
                Db::name('message')->where('id',$id)->update(['is_read'=>1]);
                $this->success('success',$list);
            }
            $data = Db::name('message')
                ->where('id',$id)
                ->field('id,title,content,createtime,type')
                ->find();
            Db::name('message')->where('id',$id)->update(['is_read'=>1]);
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        }
        $this->success('success',$data);
    }
}