Messages.php 3.9 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
 * 系统消息接口
 */
class Messages extends Api
{
    protected  $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $user_id = '';//token存贮user_id
    protected $read = 1;//是否已读 1:已读
    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (系统消息列表)
     * @ApiSummary  (系统消息列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/messages/getMessageList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553777266",
            "data":{
                "id": 1,
                "title": "系统消息",//系统消息标题
                "is_read": 0 //系统消息是否已读  0:未读,1:已读
                },
                {
                "id": 5,
                "title": "重大好消息1",
                "is_read": 0
                }
            })
     */
    public function getMessageList(){
        if($this->request->isGet()){
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.gift_pages');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_message')
                ->alias('l')
                ->join('gc_message m','l.message_id = m.id','LEFT')
                ->where(['l.user_id'=>$this->user_id])
                ->page($page,$limit)
                ->field('m.id,m.title,l.is_read')
                ->select();
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (系统消息详情)
     * @ApiSummary  (系统消息详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/messages/messageDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553777266",
            "data":{
                "id": 5,
                "title": "重大好消息1",//系统消息标题
                "description": "系统消息系统消息详情“,
                "createtime": "2019-03-20 16:24:48"
                }
            })
     */
    public function messageDetail(){
        if($this->request->isGet()){
            $message_id = $this->request->get('id');//商品id
            $rule = config('site.goods');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$message_id])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_message_log')
                ->alias('l')
                ->join('gc_message m','l.message_id = m.id','LEFT')
                ->where(['l.user_id'=>$this->user_id,'m.id'=>$message_id])
                ->field('m.id,m.title,m.description,l.createtime')
                ->select();
            foreach($data as &$value){
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            }
            Db::table('gc_message_log')
                ->where(['message_id'=>$message_id,'user_id'=>$this->user_id])
                ->update(['is_read'=>$this->read,'readtime'=>time()]);
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }
}