ReadController.class.php 3.2 KB
<?php
/**
 * Created by PhpStorm.
 * User: liyifei2012it
 * Date: 17/8/1
 * Time: 17:47
 */
namespace Api\Controller\News;

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

class ReadController extends \Api\Controller\AbstractController
{
    /**
     * Read
     * @author liyifei
     * @desc 已读新闻接口
     * @param int article_id:true 新闻公告ID
     * @return array 获得积分列表
                array(
                    'integrals' => array(  // 每次获得的积分数
                        array(
                            'content' => '阅读新闻获得积分',
                            'number' => 1,
                        ),
                    ),
                )
     */
    public function Index_post()
    {
        // 验证规则
        $rules = [
            'article_id' => 'require|integer',
        ];

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

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

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

        $this->_result = [
            'integrals' => isset($integrals) ? $integrals : [],
        ];
    }

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

        // 记录阅读新闻
        $actionServ = new UserActionService();
        $saveRes = $actionServ->save($this->uid, $article['article_id'], $actionKey);

        // 首次阅读 + 后台设置开启积分策略时,可进行积分埋点
        if ($saveRes) {
            // 触发积分策略:完成阅读
            $triggersType = [
                [
                    'triggerKey' => Constant::INT_TRIGGER_COMPLETE,
                    'value' => 1,
                ]
            ];

            // 触发积分策略:完成阅读新闻篇数
            $readTotal = $actionServ->count_by_conds([
                'uid' => $this->uid,
                'action_key' => $actionKey,
            ]);
            $triggersType[] = [
                'triggerKey' => Constant::INT_TRIGGER_COMPLETE_NUM,
                'value' => $readTotal,
            ];

            // 构造策略触发参数
            $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 : [];
    }
}