审查视图

application/api/controller/Message.php 4.5 KB
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/30
 * Time: 17:17
 */

namespace app\api\controller;


use app\common\controller\Api;
郭盛 authored
13
use think\Db;
14 15 16 17 18 19 20 21 22 23 24

/**
 * 平台消息模块
 */
class Message extends Api
{
    /**
     * @ApiTitle    (平台消息列表)
     * @ApiSummary  (平台消息列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/message/index)
郭盛 authored
25 26 27
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
28 29 30 31 32 33
     * @ApiReturn({
        "code": 1,
        "msg": "SUCCESS",
        "time": "1553839125",
        "data": {
            'title'://标题,
郭盛 authored
34 35
            'user_id'://用户ID
            'is_read': //是否已读(0未读1已读),
36
            'content'://内容,
郭盛 authored
37 38 39
            'type'://类型(1普通2活动3奖品)
            'num'://未读消息数量
            'total'://通知数量
40
            'createtime'://创建时间
郭盛 authored
41
            'already'://已读
42 43 44 45 46
        }
        })
     */
    public function index()
    {
郭盛 authored
47 48 49 50 51 52
        $user_id = $this->getUserId();
        $data['list'] = Db::name('message')
            ->where('user_id',$user_id)
            ->whereOr('user_id','null')
            ->order('is_read desc,createtime desc')
            ->select();
郭盛 authored
53
        foreach ($data['list'] as &$v){
郭盛 authored
54
            $v['createtime'] = date('m月d日',$v['createtime']);
郭盛 authored
55
        }
郭盛 authored
56 57 58 59 60 61 62 63 64 65 66 67

        //查询出所有的平台信息
        $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')
郭盛 authored
68
            ->where('is_read',0)
郭盛 authored
69 70 71 72 73
            ->where('user_id',$user_id)
//            ->where(function ($query)use($user_id){
//                $query->where('user_id',$user_id)
//                    ->whereOr('user_id','null');
//            })
郭盛 authored
74
            ->count();
郭盛 authored
75 76
        $data['num'] = $num + $nums;
        //通知数量
郭盛 authored
77 78 79 80
        $data['total'] = Db::name('message')
            ->where('user_id',$user_id)
            ->whereOr('user_id','null')
            ->count();
郭盛 authored
81
        $data['already'] = $data['total'] - $data['num'];
郭盛 authored
82 83
        $this->success('success',$data);
    }
84
郭盛 authored
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    /**
     * @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();
郭盛 authored
123 124 125 126 127 128
            //判断该记录是否已经添加过了
            $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);
            }
郭盛 authored
129 130
            $data = Db::name('message')
                ->where('id',$id)
郭盛 authored
131
                ->field('id,title,content,createtime,type')
郭盛 authored
132 133 134 135 136 137
                ->find();
            $data['createtime'] = date('Y-m-d H:i:s',$data['createtime']);
        }else{
            //不是
            $data = Db::name('message')
                ->where('id',$id)
郭盛 authored
138
                ->field('id,title,content,createtime,type')
郭盛 authored
139 140 141 142 143
                ->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);
144 145
    }
}