Opt.php 4.64 KB
<?php

use Action\AbstractAction;
use LibModels\Wap\Guang\OptData;
use LibModels\Wap\Product\BrandData;
use Plugin\Helpers;

/**
 * 逛操作
 */
class OptController extends AbstractAction
{

    /**
     * 资讯文章点赞 (H5里显示点赞)
     *
     * @param int $id 唯一的资讯ID
     * @param string $opt 操作(ok:表示确定,cancel:表示取消)
     * @return json
     */
    public function praiseArticleAction()
    {
        $result = array();

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 判断参数是否有效 */
            $id = $this->post('id');
            if (!is_numeric($id)) {
                break;
            }

            /* 执行点赞或取消操作 */
            $opt = $this->post('opt', 'ok');
            $udid = $this->getUdid();
            $result = OptData::praiseArticle($udid, $id, $opt);
        } while (false);

        $this->echoJson($result);
    }

    /**
     * 资讯文章收藏 (APP里显示收藏)
     *
     * @param int $id 唯一的资讯ID
     * @param string $opt 操作(ok:表示确定,cancel:表示取消)
     * @return json
     */
    public function collectArticleAction()
    {
        $result = array('code' => 200, 'message' => '成功!', 'data' => '');
        $jumpUrl = '';
        $isNoLogin = false;

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                $result = array('code' => 400, 'message' => '不是Ajax请求!', 'data' => '');
                break;
            }

            /* 判断参数是否有效 */
            $id = $this->post('id');
            if (!is_numeric($id)) {
                $result = array('code' => 400, 'message' => '没有ID!', 'data' => '');
                break;
            }

            /* 判断用户是否登录 */
            $uid = $this->get('uid');
            $isNoLogin = empty($uid) || !is_numeric($uid);

            /* 如果有 yh_channel 是 app */
            if ($yhChannel = $this->post('yh_channel')) {

                /* 如果没有登录,拼接 App 登录规则 */
                if ($isNoLogin) {
                    $playUrl = Helpers::url('/author/index', '', 'guang');
                    $playUrlEncode = strtr($playUrl, array('/' => '\\/'));
                    $jumpUrl = $playUrl . '?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"' . $playUrlEncode . '","param":{"from":"app","id":"' . $id . '",""yh_channel":"' . $yhChannel . '",""uid":"' . $uid . '"}},"requesturl":{"url":"","param":{}},"priority":"N"}}&uid=' . $uid;
                    $result = array('code' => 200, 'message' => '未登录', 'data' => array('jumpUrl' => $jumpUrl, 'jump' => true));
                    break;
                } else {

                    /* 执行点赞或取消操作 */
                    $opt = $this->post('opt', 'ok');
                    $data = OptData::collectArticle($uid, $id, $opt);
                    if (!$data) {
                        $result = array('code' => 400, 'message' => '操作失败', 'data' => '');
                        break;
                    }
                }
            } else { /* 非 app 不需要点赞*/
                $result = array('code' => 400, 'message' => '不是App!', 'data' => '');
            }
        } while (false);

        $this->echoJson($result);
    }

    /**
     * 品牌收藏
     *
     * @param int $id 品牌的ID
     * @param string $opt 操作(ok:表示确定,cancel:表示取消)
     * @return json
     */
    public function favoriteBrandAction()
    {
        $refer = Helpers::url('/signin.html', array('refer' => $this->server('HTTP_REFERER', '/')));
        $result = array('code' => 400, 'message' => '未登录', 'data' => $refer);

        do {
            /* 判断是不是AJAX请求 */
            if (!$this->isAjax()) {
                break;
            }

            /* 判断参数是否有效 */
            $id = $this->post('id');
            if (!is_numeric($id)) {
                break;
            }

            /* 检查用户是否登录 */
            $uid = $this->getUid();
            if (!$uid) {
                $uid = $this->getSession('uid');
            }
            if (empty($uid) || !is_numeric($uid)) {
                break;
            }

            /* 取消收藏 */
            $opt = $this->post('opt', 'ok');
            if ($opt !== 'ok') {
                $result = BrandData::favoriteCancel($id, $uid);
                break;
            }

            /* 收藏 */
            $result = BrandData::favorite($id, $uid);
        } while (false);

        $this->echoJson($result);
    }

}