审查视图

api/user/controller/CommentsController.php 5.2 KB
董瑞恩 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php
// +----------------------------------------------------------------------
// | 文件说明:评论
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Date: 2017-7-26
// +----------------------------------------------------------------------
namespace api\user\controller;

use api\user\model\CommentModel as Comment;
use api\user\model\UserModel as User;
use cmf\controller\RestUserBaseController;

class CommentsController extends RestUserBaseController
{

    /**
     * [getUserComments 获取用户评论]
     * @Author:   wuwu<15093565100@163.com>
     * @DateTime: 2017-05-25T20:48:53+0800
     * @since:    1.0
     * @return    [array_json] [获取Comment]
     */
    public function getUserComments()
    {
        $input = $this->request->param();

        $comment                 = new Comment();
        $map['where']['user_id'] = $this->getUserId();
        $map['order']            = '-create_time';
        $map['relation']         = 'user,to_user';
        if (!empty($input['page'])) {
            $map['page'] = $input['page'];
        }
        //处理不同的情况
        $data = $comment->getDatas($map);
        $this->success('请求成功', $data);

    }

    /**
     * [getComments 获取评论]
     * @Author:   wuwu<15093565100@163.com>
     * @DateTime: 2017-05-25T20:48:53+0800
     * @since:    1.0
     * @return    [array_json] [获取Comment]
     */
    public function getComments()
    {
        $input           = $this->request->param();
        $id              = $this->request->has('object_id') ? $input['object_id'] : $this->error('id参数不存在');
        $table           = $this->request->has('table_name') ? $input['table_name'] : $this->error('table参数不存在');
        $comment         = new Comment();
        $map['where']    = [
            'object_id'  => $id,
            'table_name' => $table
        ];
        $map['relation'] = 'user,to_user';

        if (!empty($input['page'])) {
            $map['page'] = $input['page'];
        }

        $data = $comment->getDatas($map);
        //数据是否存在
        if ($data->isEmpty()) {
            $this->error('评论数据为空');
        } else {
            $this->success('评论获取成功!', $data);
        }
    }

    /**
     * [delComments 删除评论]
     * @Author:   wuwu<15093565100@163.com>
     * @DateTime: 2017-08-11T22:08:56+0800
     * @since:    1.0
     * @return
     */
    public function delComments()
    {
        $input  = $this->request->param();
        $id     = $this->request->has('id') ? intval($input['id']) : $this->error('id参数不存在');
        $userId = $this->getUserId();
        Comment::destroy(['id' => $id, 'user_id' => $userId]);

        $this->success('删除成功');
    }

    /**
     * [setComments 添加评论]
     * @Author:   wuwu<15093565100@163.com>
     * @DateTime: 2017-08-16T01:07:44+0800
     * @since:    1.0
     */
    public function setComments()
    {
        $data = $this->_setComments();
        if ($res = Comment::setComment($data)) {
            $this->success('评论成功', $res);
        } else {
            $this->error('评论失败');
        }
    }

    /**
     * [_setComments 评论数据组织]
     * @Author:   wuwu<15093565100@163.com>
     * @DateTime: 2017-08-16T01:00:02+0800
     * @since:    1.0
     */
    protected function _setComments()
    {
        $input              = $this->request->param();
        $data['object_id']  = $this->request->has('object_id') ? $input['object_id'] : $this->error('object_id参数不存在');
        $data['table_name'] = $this->request->has('table_name') ? $input['table_name'] : $this->error('table_name参数不存在');
        $data['url']        = $this->request->has('url') ? $input['url'] : $this->error('url参数不存在');
        $data['content']    = $this->request->has('content') ? $input['content'] : $this->error('内容不为空');
        $data['parent_id']  = $this->request->has('parent_id') ? $input['parent_id'] : 0;
        $result             = $this->validate($data,
            [
                'object_id' => 'require|number',
                'content'   => 'require',
            ]);
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $data['delete_time'] = 0;
        $data['create_time'] = time();
        if ($data['parent_id']) {
            $res = Comment::field('parent_id', 'path', 'user_id')->find($data['parent_id']);
            if ($res) {
                $data['path']       = $res['path'] . $data['parent_id'] . ',';
                $data['to_user_id'] = $res['user_id'];
            } else {
                $this->error('回复的评论不存在');
            }
        } else {
            $data['path'] = '0,';
        }
        $data['user_id'] = $this->getUserId();
        $userData        = User::field(true)->find($data['user_id']);
        if (!$userData) {
            $this->error('评论用户不存在');
        }

        $data['full_name'] = $userData['user_nickname'];
        $data['email']     = $userData['user_email'];
        return $data;
    }
}