InfoData.php 8.22 KB
<?php
namespace LibModels\Web\Guang;

use Api\Yohobuy;
use Api\Sign;
class InfoData
{
    /**
     * 获取接口地址
     * @return string
     */
    public static function getUrl($type)
    {
        //作者信息
        if ($type == 'author') {
            return Yohobuy::SERVICE_URL.'guang/service/v1/author/getAuthor';
        }
        //文章评论列表
        elseif($type == 'commentList') {
            return Yohobuy::SERVICE_URL.'guang/api/v1/comments/getList';
        }
        //热门标签
        elseif ($type == 'getTagTop') {
            return Yohobuy::SERVICE_URL.'guang/api/v2/article/getTagTop';
        }
        //精彩推荐
        elseif ($type == 'getArticleByViewsNum') {
            return Yohobuy::SERVICE_URL.'guang/api/v2/article/getArticleByViewsNum';
        }
        elseif ($type == 'getBrand'){
            return Yohobuy::SERVICE_URL.'guang/service/v2/article/getBrand';
        }
        //相关文章
        elseif ($type == 'getOtherArticle') {
            return Yohobuy::SERVICE_URL.'guang/service/v2/article/getOtherArticle';
        }
        //文章标题等相关信息
        elseif ($type == 'getArticle') {
            return Yohobuy::SERVICE_URL.'guang/service/v2/article/getArticle';
        }
        //文章详情
        elseif ($type == 'getArticleContent') {
            return Yohobuy::SERVICE_URL.'guang/service/v2/article/getArticleContent';
        }
        //基本信息
        elseif ($type == 'getArticleBaseInfo') {
            return Yohobuy::SERVICE_URL.'guang/api/v1/article/getArticleBaseInfo';
        }
        //添加评论
        elseif ($type == 'addComment') {
            return Yohobuy::SERVICE_URL.'guang/api/v1/comments/add';
        }
        //点赞
        elseif ($type == 'setPraise') {
            return Yohobuy::SERVICE_URL.'guang/api/v2/praise/setPraise';
        }
        //取消赞
        elseif ($type == 'cancelPraise') {
            return Yohobuy::SERVICE_URL.'guang/api/v2/praise/cancel';
        }
        //收藏
        elseif ($type == 'setFavorite') {
            return Yohobuy::SERVICE_URL.'guang/api/v1/favorite/setFavorite';
        }
        //取消收藏
        elseif ($type == 'cancelFavorite') {
            return Yohobuy::SERVICE_URL.'guang/api/v1/favorite/cancelFavorite';
        }
    }

    /**
     * 调用接口
     * @param string $url
     * @param array $param
     * @param bool $onlyUrl true:返回请求地址; false:返回接口数据
     * @return mixed
     */
    public static function webApi($url, $param, $onlyUrl = false)
    {
        $param += Yohobuy::param();
        $param['client_type'] = 'web';
        $param['private_key'] = Yohobuy::$privateKeyList['web'];
        $param['client_secret'] = Sign::getSign($param);
        if ($onlyUrl) {
            return Yohobuy::httpBuildQuery($url, $param);
        }
        return Yohobuy::get($url, $param);
    }

    /**
     * 根据文章id获取文章信息
     * @param $id
     * @param $onlyUrl
     * @return mixed
     */
    public static function getArticleInfo($id, $onlyUrl = false)
    {
        $param['article_id'] = intval($id);
        return self::webApi(self::getUrl('getArticle'), $param, $onlyUrl);
    }

    /**
     * 组建根据文章id查询作者信息的url
     * @param $authorId
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function author($authorId, $onlyUrl = false)
    {
        $param['author_id'] = intval($authorId);
        return self::webApi(self::getUrl('author'), $param, $onlyUrl);
    }

    /**
     * 热门标签
     * @param int $page
     * @param int $limit
     * @param bool $onlyUrl
     * @return string
     */

    public static function tagTop($page = 1,$limit = 10, $onlyUrl = false)
    {
        $param['page'] = intval($page);
        $param['limit'] = intval($limit);
        return self::webApi(self::getUrl('getTagTop'), $param, $onlyUrl);
    }

    /**
     * 组建根据文章id查询评论列表
     * @param $articleId
     * @param $page
     * @param $limit
     * @param $onlyUrl
     * @return string
     */
    public static function comment($articleId, $page = 1, $limit = 10, $onlyUrl = false)
    {
        $param['article_id'] = intval($articleId);
        $param['page'] = intval($page);
        $param['limit'] = intval($limit);
        return self::webApi(self::getUrl('commentList'), $param, $onlyUrl);
    }

    /**
     * 文章详细内容
     * @param $articleId
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function articleContent($articleId, $onlyUrl = false)
    {
        $param['article_id'] = intval($articleId);
        return self::webApi(self::getUrl('getArticleContent'), $param, $onlyUrl);
    }

    /**
     * 判断用户是否收藏文章地址
     * @param $articleId
     * @param $uid
     * @param $udid
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function baseInfo($articleId, $uid, $udid, $onlyUrl = false)
    {
        $param['id'] = intval($articleId);
        $param['uid'] = $uid;
        $param['udid'] = $udid;
        return self::webApi(self::getUrl('getArticleBaseInfo'), $param, $onlyUrl);
    }

    /**
     * 文章相关品牌
     * @param $articleId
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function relateBrand($articleId, $onlyUrl = false)
    {
        $param['article_id'] = intval($articleId);
        return self::webApi(self::getUrl('getBrand'), $param, $onlyUrl);
    }

    /**
     * 相关文章列表
     * @param $articleId
     * @param $tag
     * @param int $limit
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function relateList($articleId, $tag, $limit = 3, $onlyUrl = false)
    {
        $param['article_id'] = intval($articleId);
        $param['tags'] = $tag;
        $param['limit'] = intval($limit);
        return self::webApi(self::getUrl('getOtherArticle'), $param, $onlyUrl);
    }

    /**
     * 精彩推荐(48小时内浏览最多的文章)
     * 
     * @param string $gender
     * @param int $page
     * @param int $limit
     * @param bool $onlyUrl
     * @return mixed
     */
    public static function recommend($gender, $page = 1, $limit = 10, $onlyUrl = false)
    {
        $param['gender'] = $gender;
        $param['page'] = intval($page);
        $param['limit'] = intval($limit);
        return self::webApi(self::getUrl('getArticleByViewsNum'), $param, $onlyUrl);
    }

    /**
     * 添加文章评论
     * @param $id 文章id
     * @param $uid 用户id
     * @param $content 评论内容
     * @return mixed
     */
    public static function addComment($id, $uid, $content)
    {
        $param['article_id'] = intval($id);
        $param['uid'] = $uid;
        $param['content'] = $content;
        return self::webApi(self::getUrl('addComment'), $param);
    }

    /**
     * 点赞
     * @param $id
     * @param $udid
     * @return mixed
     */
    public static function setPraise($id, $udid)
    {
        $param['article_id'] = intval($id);
        $param['udid'] = $udid;
        return self::webApi(self::getUrl('setPraise'), $param);
    }

    /**
     * 取消赞
     * @param $id
     * @param $udid
     * @return mixed
     */
    public static function cancelPraise($id, $udid)
    {
        $param['article_id'] = intval($id);
        $param['udid'] = $udid;
        return self::webApi(self::getUrl('cancelPraise'), $param);
    }

    /**
     * 收藏文章
     * @param $id
     * @param $uid
     * @return mixed
     */
    public static function setFavorite($id, $uid)
    {
        $param['article_id'] = intval($id);
        $param['uid'] = $uid;
        return self::webApi(self::getUrl('setFavorite'), $param);
    }

    /**
     * 取消文章收藏
     * @param $id
     * @param $uid
     * @return mixed
     */
    public static function cancelFavorite($id, $uid)
    {
        $param['article_id'] = intval($id);
        $param['uid'] = $uid;
        return self::webApi(self::getUrl('cancelFavorite'), $param);
    }
}