CoinDogController.php 3.47 KB
<?php

namespace api\controllers;

use backend\models\coin\ArticleForm;
use common\models\pwallet\Article;
use linslin\yii2\curl\Curl;
use Yii;
use api\base\BaseController;
use yii\data\Pagination;

class CoinDogController extends BaseController
{
    public function actionArticle()
    {
        $coindog = Yii::$app->params['coindog'];

        $accessKey = $coindog['accessKey'];
        $secretKey = $coindog['secretKey'];
        $httpParams = array(
            'access_key' => $accessKey,
            'date' => time()
        );
        $signParams = array_merge($httpParams, array('secret_key' => $secretKey));

        ksort($signParams);
        $signString = http_build_query($signParams);

        $httpParams['sign'] = strtolower(md5($signString));
        $url = $coindog['article'] . '?' . http_build_query($httpParams);
        $curl = new Curl();
        $resp = $curl->get($url, false);
        return ['code' => 200, 'data' => $resp, 'msg' => 'ok'];
    }

    public function actionLive()
    {
        $limit = Yii::$app->request->get('limit', 20);
        $id = Yii::$app->request->get('id', 0);
        $flag = Yii::$app->request->get('flag', 'down');


        $coindog = Yii::$app->params['coindog'];

        $accessKey = $coindog['accessKey'];
        $secretKey = $coindog['secretKey'];
        $httpParams = array(
            'access_key' => $accessKey,
            'date' => time()
        );

        $signParams = array_merge($httpParams, array('secret_key' => $secretKey));

        ksort($signParams);
        $signString = http_build_query($signParams);

        $httpParams = [
            'sign' => strtolower(md5($signString)),
            'limit' => $limit,
            'id' => $id,
            'flag' => $flag
        ];

        $url = $coindog['live'] . '?' . http_build_query($httpParams);
        $curl = new Curl();
        $resp = $curl->get($url, false);
        if (!isset($resp['list'])) {
            $msg = '数据不存在';
            $code = -1;
            $data = null;
            goto doEnd;
        }
        $list = $resp['list'][0]['lives'];
        foreach ($list as &$item) {
            preg_match_all("/\【(.+?)\】/", $item['content'], $match);
            $item['title'] = $match[1][0];
        }
        $code = 0;
        $msg = 'success';
        doEnd :

        return ['code' => $code, 'data' => $list, 'msg' => $msg];
    }

    public function actionArticleBanner()
    {
        $page = Yii::$app->request->get('page', 1);
        $size = Yii::$app->request->get('size', 10);
        $query = Article::find()
            ->select('id, image_url ,title, url')
            ->orderBy('update_at desc');

        $count = $query->count();
        if (0 == $count) {
            $msg = '数据不存在';
            $code = -1;
            $data = null;
            goto doEnd;
        }
        $data = $query->offset(($page - 1) * $size)->limit($size)->asArray()->all();
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $size]);
        $data = [
            'list' => $data,
            'page' => [
                'pageCount' => $pages->pageCount,
                'pageSize' => $size,
                'currentPage' => $page,
            ]
        ];
        $code = 0;
        $msg = 'success';
        doEnd :

        return ['code' => $code, 'data' => $data, 'msg' => $msg];

    }
}