Commit cd1db07a authored by shajiaiming's avatar shajiaiming

自选行情接口中

parent a634a3ed
......@@ -2,12 +2,39 @@
namespace api\controllers;
use linslin\yii2\curl\Curl;
use Yii;
use api\base\BaseController;
use common\models\psources\CoinOptional;
use common\service\exchange\ExchangeFactory;
use common\service\exchange\ExchangeBuilderFactory;
class TickerController extends BaseController
{
protected $basic_coin = ['ETH', 'BTC', 'USDT', 'BTY'];
protected $basic_price = [];
public function init()
{
$curl = new Curl();
$data = [
"names" => [
"eth,ethereum",
"btc,btc",
"usdt,ethereum",
"bty,bty",
]
];
$params = json_encode($data);
$curl->setHeader('Content-Type', 'application/json');
$curl->setRawPostData($params);
$res = $curl->post('https://b.biqianbao.net/interface/coin/coin-index', true);
$res = json_decode($res, true);
foreach ($res['data'] as $val) {
$this->basic_price[$val['name']] = $val['rmb'];
}
}
public function actionIndex()
{
$exchange = Yii::$app->request->get('exchange', 'zhaobi');
......@@ -28,4 +55,108 @@ class TickerController extends BaseController
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionOptional()
{
$code = -1;
$msg = 'fail';
$data = null;
$request = Yii::$app->request;
if ($request->isPost) {
$symbol = $request->post('symbol', '');
$platform = $request->post('platform', '');
$device_code = $request->post('device_code', '');
if (empty($symbol) || empty($platform) || empty($device_code)) {
$msg = '参数错误';
goto doEnd;
}
$model = CoinOptional::find()->where(['device_code' => $device_code, 'symbol' => $symbol])->one();
if ($model) {
$msg = '数据已存在!';
goto doEnd;
}
$model = new CoinOptional();
$model->setScenario(CoinOptional::SCENARIOS_CREATE);
if ($model->load(Yii::$app->request->post(), '') && $model->save()) {
$msg = 'success';
$code = 0;
} else {
$msg = current($model->firstErrors);
}
goto doEnd;
}
if ($request->isGet) {
$device_code = $request->get('device_code', '');
if (empty($device_code)) {
$msg = '参数错误';
goto doEnd;
}
$data = $temp = [];
$model = CoinOptional::find()->select('symbol, platform')->where(['device_code' => $device_code])->asArray()->all();
foreach ($model as $val) {
$symbol = explode('/', $val['symbol']);
$tag_first = $symbol[0];
$tag_second = $symbol[1];
$exchange = 'Huobi';
$exchange = ExchangeFactory::createExchange($exchange);
$quotation = $exchange->getTicker(strtolower($tag_first), strtolower($tag_second));
$temp['symbol'] = $val['symbol'];
$temp['close'] = (float)sprintf("%0.4f", $quotation['last']);
$temp['close_rmb'] = (float)sprintf("%0.4f", $quotation['last'] * $this->basic_price[$tag_second]);
//$temp['change'] = (float)sprintf("%0.4f", ($val['close'] - $val['open']) / $val['open'] * 100);
$temp['change'] = 3.33;
$temp['high'] = (float)sprintf("%0.4f", $quotation['high']);
$temp['low'] = (float)sprintf("%0.4f", $quotation['low']);
$temp['platform'] = $val['platform'];
array_push($data, $temp);
}
$msg = 'success';
$code = 0;
}
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public function actionRemoveOptional()
{
$code = -1;
$msg = 'fail';
$data = null;
$request = Yii::$app->request;
if (!$request->isPost) {
$msg = '请求错误!';
goto doEnd;
}
$symbol = $request->post('symbol', '');
$device_code = $request->post('device_code', '');
if (empty($symbol) || empty($device_code)) {
$msg = '请求参数错误!';
goto doEnd;
}
$model = CoinOptional::find()->where(['device_code' => $device_code, 'symbol' => $symbol])->one();
if (empty($model)) {
$msg = '数据不存在!';
goto doEnd;
}
if (!$model->delete()) {
$msg = '删除失败!';
goto doEnd;
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinOptional extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_optional}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules() {
return [
[['symbol','platform', 'device_code'], 'required'],
];
}
public function scenarios() {
$scenarios = [
self:: SCENARIOS_CREATE => ['symbol','platform', 'device_code'],
];
return array_merge( parent:: scenarios(), $scenarios);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment