• rlgy's avatar
    update · d0598d10
    rlgy authored
    d0598d10
CoinController.php 7.22 KB
<?php
/**
 * Created by PhpStorm.
 * User: rlgyzhcn
 * Date: 18-5-31
 * Time: 下午1:02
 */

namespace api\controllers;

use api\base\BaseController;
use common\base\Exception;
use common\business\BrowerBusiness;
use common\business\CoinBusiness;
use common\business\ExchangeBusiness;
use common\models\psources\Coin;
use common\models\psources\CoinRecommend;
use Yii;

/**
 * 币种信息管理控制器
 * Class CoinController
 *
 * @package api\controllers
 */
class CoinController extends BaseController
{
    /**
     * 单币种按照id查询
     *
     * @return array|null|\yii\db\ActiveRecord
     */
    public function actionGetCoinById()
    {
        $request = Yii::$app->request;
        $id      = $request->post('id', 0);
        if ($id) {
            $ret = CoinBusiness::getCoinAllById($id);
            if ($ret) {
                return $ret[0];
            }
        }
        return [];
    }

    /**
     * 获取推介币种列表
     */
    public function actionGetRecList()
    {
        $request     = Yii::$app->request;
        $page        = $request->post('page', 1);
        $limit       = $request->post('limit', 999);
        $platform_id = $request->post('platform_id', 2);//默认币钱包
        if ($platform_id == 1) {
            $platform_id = 3;
        } elseif ($platform_id == 2) {
            $platform_id = 1;
        } elseif ($platform_id == 3) {
            $platform_id = 2;
        }
        $recommend = $request->post('recommend', '');

        $condition = ['platform_id' => $platform_id];
        if ($recommend) {
            $condition['recommend'] = $recommend;
        }
        $select = ['id', 'sid', 'icon', 'name', 'nickname', 'platform', 'chain'];
        $datas  = CoinRecommend::getList($page, $limit, $condition, [], $select);

        //获取详细信息
        $coin_recommends = &$datas['data'];
        if (!empty($coin_recommends)) {
            $coin_ids = array_column($coin_recommends, 'cid');
            //获取币种信息
            $coin_infos = Coin::getCoinInfoByIds($coin_ids, $select, 'id');
            //获取行情信息
            $coin_names      = array_column($coin_infos, 'name');
            $coin_names      = array_merge($coin_names, array_column($coin_infos, 'chain'));
            $coin_quotations = ExchangeBusiness::getQuatationByNames($coin_names);

            if ($coin_infos) {
                array_shift($select);
                foreach ($coin_recommends as $key => &$value) {
                    $temp_key = $coin_infos[$value['cid']]['name'];
                    foreach ($select as $item) {
                        $value[$item] = $coin_infos[$value['cid']][$item];
                        if ($value['platform_id'] != 2) {
                            //国盾币不需要行情
                            $value['low']  = $coin_quotations[$temp_key]['low'];
                            $value['high'] = $coin_quotations[$temp_key]['high'];
                            $value['last'] = $coin_quotations[$temp_key]['last'];
                            $value['rmb']  = $coin_quotations[$temp_key]['rmb'];
                        } else {
                            $value['low']  = 0;
                            $value['high'] = 0;
                            $value['last'] = 0;
                            $value['rmb']  = 0;
                        }
                    }
                    $value['id']              = $value['cid'];
                    $value['sid']             = ucfirst($value['sid']);
                    $value['chain_quotation'] = $coin_quotations[$coin_infos[$value['cid']]['chain']];
                    unset($value['create_time'], $value['update_time'], $value['cid']);
                }
                unset($key, $value);
            }
        }
        return $datas;
    }

    /**
     * 矿工费获取
     *
     * 根据name获取
     *
     * @throws Exception
     */
    public function actionGetMinerFeeByName()
    {
        $names = Yii::$app->request->post('name');
        $coin  = Coin::findOne(['name' => $names]);
        if ($coin) {
            $miner_fee = $coin->minerFee;
            if (empty($miner_fee)) {
                return [];
            }
        } else {
            //如果coin为null,$coin->minerFee会抛出Trying to get property 'minerFee' of non-object",code=>8
            throw new Exception('8', '币种不存在');
        }
        $result        = (array)$miner_fee->getAttributes();
        $result['min'] = (float)$result['min'];
        $result['max'] = (float)$result['max'];
        return $result;
    }

    /**
     * app首页接口V2
     */
    public function actionCoinIndex()
    {
        $names     = Yii::$app->request->post('names');
        $condition = [['in', 'name', $names]];
        $result    = ExchangeBusiness::getApiListForIndex(1, 999, $condition);
        if ($result) {
            $chains          = array_unique(array_column($result['data'], 'chain'));
            $chain_quotation = [];
            foreach ($chains as $key => $value) {
                $chain_quotation[$value] = ExchangeBusiness::getquatation($value);
            }
            foreach ($result['data'] as $key => &$value) {
                $value['chain_quotation'] = $chain_quotation[$value['chain']];
            }
            return $result;
        }
    }

    /**
     * 按照名称搜索币种
     *
     * @return array
     */
    public function actionSearchCoinByName()
    {
        $request      = Yii::$app->request;
        $name         = $request->post('name', '');
        $page         = $request->post('page', 1);
        $limit        = $request->post('limit', 10);
        $platform_ids = $request->post('platform_id', null);

        $condition = [['!=', 'chain', 'other']];
        if (!empty($name)) {
            $condition[] = ['or', ['like', 'name', $name], ['like', 'nickname', $name]];
        }
        if ($platform_ids) {
            $platform_id_arr = explode(',', $platform_ids);
            $condition_arr   = ['OR'];
            foreach ($platform_id_arr as $key => $value) {
                array_push($condition_arr, ['=', 'platform_id', $value]);
            }
            $condition[] = $condition_arr;
        }
        $result = ExchangeBusiness::SearchByName($page, $limit, $condition);
        if (empty($result)) {
            return [];
        }
        $total  = $result['total'];
        $result = $result['data'];
        if ($result) {
            $chains          = array_unique(array_column($result, 'chain'));
            $chain_quotation = [];
            foreach ($chains as $key => $value) {
                $chain_quotation[$value] = ExchangeBusiness::getquatation($value);
            }
            foreach ($result as $key => $value) {
                $result[$key]['chain_quotation'] = $chain_quotation[$value['chain']];
            }
            return ['code' => 0, 'count' => $total, 'data' => $result];
        }
    }

    /**
     * 返回交易状态
     *
     * @return mixed
     */
    public function actionTransaction()
    {
        $request = Yii::$app->request;
        $name    = $request->post('name', '');
        $txhash  = $request->post('txhash', '');
        if ($name && $txhash) {
            return BrowerBusiness::getTransStatus($name, $txhash);
        }
        return false;
    }
}