LikeController.class.php 3.66 KB
<?php
/**
 * Created by PhpStorm.
 * User: tangxingguo
 * Date: 2017/4/11
 * Time: 15:29
 */
namespace Api\Controller\News;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\DataCenter;
use Common\Common\IntegralStrategy;
use Common\Common\NewsHelper;
use Common\Service\ArticleService;
use Common\Service\LikeService;
use Common\Service\UserActionService;

class LikeController extends \Api\Controller\AbstractController
{
   /**
    * Like
    * @author tangxingguo
    * @desc 点赞
    * @param Int article_id:true 新闻公告ID
    * @return array 积分列表
                   array(
                       'integrals' => array(
                           array(
                               'number' => 100,
                               'content' => '',
                           ),
                       ),
                   );
    */

    public function Index_post()
    {
        $user = $this->_login->user;

        // 验证规则
        $rules = [
            'article_id' => 'require|integer',
        ];

        // 验证数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $article_id = $validate->postData['article_id'];

        // 新闻检查
        $articleServ = new ArticleService();
        $newsInfo = $articleServ->get($article_id);
        if (empty($newsInfo)) {
            E('_ERR_ARTICLE_NOT_FOUND');
        }

        // 数据库操作
        $likeServ = new LikeService();
        $likeCount = $likeServ->count_by_conds(['article_id' => $article_id, 'uid' => $user['memUid']]);
        if ($likeCount) {
            // 已经点过赞
            E('_ERR_ARTICLE_ALREADY_LIKE');
        }
        $data = [
            'article_id' => $article_id,
            'uid' => $user['memUid'],
            'username' => $user['memUsername'],
        ];
        $likeServ->insert($data);
        $articleServ->update($article_id, ['like_total = like_total + ?' => 1]);

        // 数据中心点赞埋点
        $dataCenter = &DataCenter::instance();
        $dataCenter->addLike($user, $article_id);
        // 数据中心点赞埋点

        // 积分、学分埋点
        $integrals = $this->_triggerIntegral($newsInfo);

        $this->_result = [
            'integrals' => $integrals,
        ];
    }

    /**
     * 触发积分、学分策略
     * @param array $article 新闻详情
     * @return array
     *          array(
     *              'integrals' => 1, // 获得的积分数
     *          )
     */
    private function _triggerIntegral($article)
    {
        $actionKey = Constant::INT_ACT_LIKE;

        // 记录当前用户的点赞行为
        $userActionServ = new UserActionService();
        $saveRes = $userActionServ->save($this->uid, $article['article_id'], $actionKey);

        if ($saveRes) {
            // 点赞次数
            $likeTotal = $userActionServ->count_by_conds([
                'uid' => $this->uid,
                'action_key' => $actionKey,
            ]);
            $triggersType = [
                [
                    'triggerKey' => Constant::INT_TRIGGER_LIKE,
                    'value' => (int)$likeTotal,
                ],
            ];

            // 构造策略触发参数
            $newsServ = &NewsHelper::instance();
            $triggers = $newsServ->buildTrigger($article, $actionKey, $triggersType);

            // 触发策略
            if (!empty($triggers)) {
                $strategyServ = &IntegralStrategy::instance();
                $integrals = $strategyServ->triggerPoint($this->uid, $article['article_id'], $actionKey, $triggers);
            }
        }

        return isset($integrals) && !empty($integrals) ? $integrals : [];
    }
}