CollectionController.class.php 3.49 KB
<?php
/**
 * CollectionController.class.php
 * 同事圈收藏
 * User: Xtong
 * Date: 2017年06月02日
 */

namespace Api\Controller\Workmate;

use Common\Common\Collection;
use Common\Common\Constant;
use Common\Common\User;
use Common\Model\CircleModel;
use Common\Service\AttachmentService;
use Common\Service\CircleService;
use VcySDK\Logger;

class CollectionController extends \Api\Controller\AbstractController
{

    /**
     * 主方法
     * @return boolean
     */
    public function Index_post()
    {
        $id = I('post.id');

        // 如果话题ID不存在
        if (empty($id)) {

            E('_EMPTY_CIRCLE_ID');
        }

        // 不支持外部人员收藏
        if (empty($this->uid)) {

            E('_NOT_ALLOW_OUTER_COLLECTION');
        }

        // 实例化话题表
        $service = new CircleService();

        // 获取话题详情
        $info = $service->get_by_conds(
            array(
                'id' => $id,
                'pid' => CircleModel::CIRCLE_PID
            )
        );

        // 如果话题详情不存在
        if (empty($info)) {

            E('_EMPTY_CIRCLE_INFO');
        }

        $img = [];

        // 实例化附件表
        $attach_serv = new AttachmentService();

        $res = $attach_serv->list_by_conds([
            'cid' => $id
        ], null);

        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $img[$k]['atId'] = $v['atid'];
                $img[$k]['imgUrl'] = imgUrlReal($v['atid']);
            }
        }

        // 初始化收藏类
        $collection = new Collection();

        // 获取ICON详情
        $icon_info = $collection->get_icon_info(array(
            'app_dir' => APP_DIR,
            'app_identifier' => APP_IDENTIFIER,
            'data_category_id' => '1'
        ));

        // 获取收藏状态 xtong 2017年06月02日
        $url = rpcUrl('/Public/Rpc/Collection/CollectionNew');

        $title = $info['content'];
        if (Constant::CIRCLE_TOPIC_TYPE == $info['type']) {
            $title = $info['title'];
        }

        $msg_type = $service::CIRCLE_INFO;
        // 话题回帖
        if (Constant::CIRCLE_REPLIES_TOPIC_TYPE == $info['type']) {
            $msg_type = $service::REPLIES_INFO;
        }
        // 话题
        if (Constant::CIRCLE_TOPIC_TYPE == $info['type']) {
            $msg_type = $service::TOPIC_INFO;
        }

        $params = [
            'uid' => $this->uid,
            'app' => 'workmate',
            'dataId' => $id,
            'title' => $title,
            'cover_type' => 0,
            'url' => "Workmate/Frontend/Index/Msg?type={$msg_type}&id=" . $id,
            'circle_uid' => $info['uid'],
            'circle_face' => User::instance()->avatar($info['uid']),
            'circle_name' => $info['username'],
            'circle_img' => $img,
            'icon_title' => $icon_info['title'],
            'icon_id' => $icon_info['recommender_id'],
            'data_category_id' => '',
            'data_category_name' => '',
            'cover_id' => $info['cover_id'],
            'cover_url' => !empty($info['cover_id']) ? imgUrlReal($info['cover_id']) : '',
            'type' => $info['type'],
            'is_anonymous'=>intval($info['is_anonymous'])
        ];

        $res = \Com\Rpc::phprpc($url)->invoke('Index', $params);

        Logger::write('收藏添加:' . var_export($res, true));

        if (!$res) {
            E('_ERR_ALREADY_COLLECTION');
        }

        // 返回成功
        $this->_result = [];
    }
}