Message.php 4.8 KB
<?php


namespace app\api\controller;

use think\Db;
use think\Exception;

/**
 * 消息相关
 * @package app\api\controller
 */
class Message extends BaseApi
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';

    /**
     * 获取用户总的未读消息
     * @ApiTitle    (获取用户总的未读消息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/getIsReadNum)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "请求数量成功",
        "time": "1609323269",
        "data": "获取用户总的未读消息"
     })
     */
    public function getIsReadNum(){
        $count = model('message')->where('user_id',$this->auth->id)->where('is_read',0)->count();
        $this->success('请求数量成功',$count);
    }
    /**
     * 分类获取用户的未读消息
     * @ApiTitle    (分类获取用户的未读消息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/getTypeIsReadNum)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
            "code": 1,
            "msg": "请求数量成功",
            "time": "1609323546",
            "data": {
                "one": "收藏消息",
                "two": "动态消息",
                "three": "礼物消息",
                "four": "通知消息"
            }
        })
     */
    public function getTypeIsReadNum(){
        $data = [];
        $data['one'] = model('message')->where('status',1)->where('user_id',$this->auth->id)->where('is_read',0)->count();
        $data['two'] = model('message')->where('status',2)->where('user_id',$this->auth->id)->where('is_read',0)->count();
        $data['three'] = model('message')->where('status',3)->where('user_id',$this->auth->id)->where('is_read',0)->count();
        $data['four'] = model('message')->where('status',4)->where('user_id',$this->auth->id)->where('is_read',0)->count();
        $this->success('请求数量成功',$data);
    }
    /**
     * 获取列表消息
     * @ApiTitle    (获取列表消息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/getMessageList)
     * @ApiParams   (name="status", type="string", required=true, description="1=收藏2=动态3=礼物4=通知")
     * @ApiParams   (name="page", type="integer", required=true, description="分页次数")
     * @ApiParams   (name="num", type="integer", required=true, description="分页数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回")
     * @ApiReturn   ({
        "code": 1,
        "msg": "请求数据成功",
        "time": "1609324842",
        "data": [
            {
                "id": 4,
                "user_id": "收到消息用户ID",
                "in_user_id": "用户ID",
                "avatar": "头像",
                "nickname": "署名",
                "title": "消息标题",
                "brief": "消息简介",
                "content": "消息内容",
                "price": "价格",
                "is_read": "是否已读:0=未读,1=已读",
                "status": "类别:1=收藏,2=动态,3=礼物,4=通知",
                "is_stu": "样式:1=动态,2=作品,3=墨宝档案,4=展览",
                "key_id": "被动主键ID",
                "createtime": "2020-12-30 18:39:32",
                "updatetime": "2020-12-30 18:39:32"
            }
        ]
        })
     */
    public function getMessageList(){
        //1.获取状态
        $data = $this->get_data_array([
            ['status','状态不能为空'],
            ['page','分页次数不能为空'],
            ['num','分页数量不能为空'],
        ]);
        //2.查询数据
        $res = model('message')
            ->where('status',$data['status'])
            ->where('user_id',$this->auth->id)
            ->where('is_read',0)
            ->order('createtime','desc')
            ->page($data['page'],$data['num'])
            ->select();
        //3.将数据变为已读
        model('message')
            ->where('status',$data['status'])
            ->where('user_id',$this->auth->id)
            ->where('is_read',0)
            ->update(['is_read'=>1]);
        $this->success('请求数据成功',$res);
    }

}