Smessages.php 4.5 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Validate;
/**
 * 系统消息接口**
 */
class Smessages 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/smessages/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": "您有一个系统消息提示,请及时查看。",//提现系统消息标题
                    "type": 1,//类型 0:充值,1:提现
                    "is_read": 0 //系统消息是否已读  0:未读,1:已读
                },
                {
                    "id": 2,
                    "title": "您有一个系统消息提示,请及时查看。",//提现系统消息标题
                    "type": 1,//类型 0:充值,1:提现
                    "is_read": 0 //系统消息是否已读  0:未读,1:已读
                },
            })
     */
    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());
            }
            $withdrawal = config('site.withdrawal');
            $data = Db::table('gc_message')
                ->alias('m')
                ->join('gc_account a','m.account_id = a.id','LEFT')
                ->where(['a.uid'=>$this->user_id,'a.type'=>$withdrawal])
                ->page($page,$limit)
                ->field('m.id,m.title,m.type,m.is_read')
                ->select();
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (系统消息详情)
     * @ApiSummary  (系统消息详情)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/smessages/messageDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="系统消息id")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1554262435",
            "data": [
                {
                    "id": 1,
                    "title": "您有一个系统消息提示,请及时查看。",
                    "type": 1,//类型 0:充值,1:提现
                    "money": 0.01,//充值或提现金额
                    "createtime": "2019-04-03 11:00:29"//时间
                }
            ]
            })
     */
    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());
            }
            $withdrawal = config('site.withdrawal');
            $data = Db::table('gc_message')
                ->alias('m')
                ->join('gc_account a','m.account_id = a.id','LEFT')
                ->where(['m.id'=>$message_id,'a.uid'=>$this->user_id,'a.type'=>$withdrawal])
                ->field('m.id,m.title,m.type,a.money,m.createtime')
                ->select();
            foreach($data as &$value){
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            }
            Db::table('gc_message')
                ->where(['id'=>$message_id])
                ->update(['is_read'=>$this->read,'readtime'=>time()]);
            $this->success('成功',$data);
        }else{
            $this->error('请求方式错误');
        }
    }
}