Commit 21e3429f authored by rlgy's avatar rlgy

版本V2

parent b417470e
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace api\controllers; namespace api\controllers;
use common\base\Exception; use common\base\Exception;
use common\business\ExchangeBusiness;
use common\models\pwallet\Coin; use common\models\pwallet\Coin;
use Yii; use Yii;
use api\base\BaseController; use api\base\BaseController;
...@@ -81,6 +82,16 @@ class CoinController extends BaseController ...@@ -81,6 +82,16 @@ class CoinController extends BaseController
} }
/** /**
* app首页接口V2
*/
public function actionCoinIndexV2()
{
$names = Yii::$app->request->post('names');
$condition = [['in', 'name', $names]];
return ExchangeBusiness::getApiListForIndex($condition);
}
/**
* 按照名称搜索币种 * 按照名称搜索币种
* @return array * @return array
*/ */
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-27
* Time: 下午12:04
*/
namespace common\business;
use common\models\pwallet\Coin;
use common\service\exchange\ExchangeFactory;
/**
* Class ExchangeBusiness
* 各大交易所交易所api逻辑层
* @package common\business
*/
class ExchangeBusiness
{
private static $exchanges = [
0 => 'HuoBi',
1 => 'Hadax',
];
/**
* 保存各个交易所支持的交易对到redis,定时用crontab更新
* @return void
*/
public static function setSupportedSymbol()
{
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
*/
$exchange = ExchangeFactory::createExchange($exchange);
$exchange->setSupportedSymbol();
}
}
/**
* 更新交易所的行情数据保存到redis,定时更新
* @return void
*/
public static function setQuotation()
{
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
*/
$exchange = ExchangeFactory::createExchange($exchange);
$exchange->setQuotation();
}
}
/**
* 根据name返回币种信息
* @param array $condition 需要的币种sid列表
* @return array
*/
public static function getApiListForIndex($condition = [])
{
$rows = Coin::getSelectList(1, 999, ['id', 'sid', 'icon', 'name', 'nickname', 'chain'], $condition);
if ($rows['count'] > 0) {
$rows = $rows['data'];
foreach ($rows as $key => $row) {
$rows[$key]['sid'] = ucfirst($rows[$key]['sid']);
$f = false;//是否获取到行情
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'])) {
$rows[$key] = array_merge($rows[$key], $exchange->getTicker($row['name']));
$f = true;
break;
}
}
if (!$f) {
//所有交易所都不能直接获取交易对的行情就通过BTC去转换
//获取BTC_USD
$btc_usd = ExchangeFactory::createExchange(self::$exchanges[0])->getTicker('btc');
foreach (self::$exchanges as $exchange) {
/**
* @var $exchange \common\service\exchange\ExchangeInterface
*/
$exchange = ExchangeFactory::createExchange($exchange);
if ($exchange->symbolExists($row['name'], 'btc')) {
$price_btc = $exchange->getTicker($row['name'], 'btc');
//获取btcusdt
$result = array_map(function ($a, $b) {
return $a * $b;
}, $price_btc, $btc_usd);
$rows[$key] = array_merge($rows[$key],
['low' => $result[0], 'high' => $result[1], 'last' => $result[2]]);
break;
}
}
}
}
return $rows;
}
return [];
}
}
\ No newline at end of file
...@@ -34,9 +34,6 @@ return [ ...@@ -34,9 +34,6 @@ return [
], ],
'linkAssets' => false,//true 清除缓存 'linkAssets' => false,//true 清除缓存
], ],
/**
* db config
*/
'db' => [ 'db' => [
'class' => 'yii\db\Connection', 'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=manage', 'dsn' => 'mysql:host=127.0.0.1;dbname=manage',
...@@ -74,8 +71,15 @@ return [ ...@@ -74,8 +71,15 @@ return [
'redis' => [ 'redis' => [
'hostname' => 'localhost', 'hostname' => 'localhost',
'port' => 6379, 'port' => 6379,
'database' => 0,
], ],
], ],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => 'localhost',
'port' => 6379,
'database' => 1,
],
'i18n' => [ 'i18n' => [
'translations' => [ 'translations' => [
'*' => [ '*' => [
...@@ -94,8 +98,6 @@ return [ ...@@ -94,8 +98,6 @@ return [
'absolute' => dirname(dirname(__DIR__)) . '/backend/web/upload', 'absolute' => dirname(dirname(__DIR__)) . '/backend/web/upload',
], ],
], ],
/** /**
* 通过配置文件附加行为,全局 * 通过配置文件附加行为,全局
*/ */
...@@ -110,7 +112,6 @@ return [ ...@@ -110,7 +112,6 @@ return [
'gii/*', // 不需要权限检测 'gii/*', // 不需要权限检测
] ]
], ],
/** /**
* 只允许一处登陆 * 只允许一处登陆
*/ */
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-26
* Time: 下午7:22
*/
namespace common\service\exchange;
/**
* Class Exchange
* 交易所抽象类
* @package common\service\exchange
*/
abstract class Exchange
{
protected $supported_symbol = '';
protected $quotation_prefix = '';
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-26
* Time: 下午7:19
*/
namespace common\service\exchange;
use Prophecy\Exception\Doubler\ClassNotFoundException;
/**
* Class ExchangeFactory
* 交易所工厂
* @package common\service\exchange
*/
class ExchangeFactory
{
/**
* 创建交易所,默认为火币
* @param string $name
* @return object
* @throws ClassNotFoundException
*/
public static function createExchange($name = 'HuoBi')
{
$className = __NAMESPACE__ . '\\' . $name;
if (class_exists($className)) {
return new $className();
}
throw new ClassNotFoundException('class ' . $className . ' not found!', $className);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-26
* Time: 下午7:08
*/
namespace common\service\exchange;
interface ExchangeInterface
{
/**
* 获取交易对聚合行情信息
*
* @param string $tag
* @param string $aim
* @return mixed
*/
public function getTicker($tag = 'BTC', $aim = "USD");
/**
* 获取支持查询行情的交易对
*
* @param string $tag
* @param string $aim
* @return bool
*/
public function symbolExists($tag = 'BTC', $aim = "USD");
/**
* 转化交易对为请求变量
* @param string $tag
* @param string $aim
* @return mixed
*/
public static function formatSymbol($tag = 'BTC', $aim = 'USD');
/**
* 保存支持的交易对到redis数据库,使用crontab定时更新
* @return mixed|void
*/
public function setSupportedSymbol();
/**
* 更新交易对行情保存到redis,使用crontab定时更新
* @return mixed|void
*/
public function setQuotation();
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-27
* Time: 下午4:22
*/
namespace common\service\exchange;
use common\helpers\Curl;
use Yii;
class Hadax extends HuoBi
{
protected $supported_symbol = 'supported_symbol_hadax';
protected $quotation_prefix = 'quotation_hadax_';
protected $base_url = 'https://api.hadax.com';
public function setSupportedSymbol()
{
$api = $this->base_url . '/v1/hadax/common/symbols';
$key = $this->supported_symbol;
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
if ($res && $res['status'] == 'ok') {
$data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($data as $item) {
$redis->sadd($key, self::formatSymbol($item['base-currency'], $item['quote-currency']));
}
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-26
* Time: 下午7:21
*/
namespace common\service\exchange;
use Yii;
use common\helpers\Curl;
class HuoBi extends Exchange implements ExchangeInterface
{
protected $supported_symbol = 'supported_symbol_huobi';
protected $quotation_prefix = 'quotation_huobi_';
protected $base_url = 'https://api.huobi.pro';
public function getTicker($tag = 'BTC', $aim = "USDT")
{
$symbol = self::formatSymbol($tag, $aim);
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$keys = $redis->hkeys($this->quotation_prefix . $symbol);
$values = $redis->hvals($this->quotation_prefix . $symbol);
return array_combine($keys, $values);
}
public function symbolExists($tag = 'BTC', $aim = "USDT")
{
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
$supported = $redis->smembers($this->supported_symbol);
if (is_array($supported) && in_array(self::formatSymbol($tag, $aim), $supported)) {
return true;
}
return false;
}
public static function formatSymbol($tag = 'BTC', $aim = 'USDT')
{
return strtolower(trim($tag) . trim($aim));
}
public function setSupportedSymbol()
{
$api = $this->base_url . '/v1/common/symbols';
$key = $this->supported_symbol;
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);//json
if ($res && $res['status'] == 'ok') {
$data = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($data as $item) {
$redis->sadd($key, self::formatSymbol($item['base-currency'], $item['quote-currency']));
}
}
}
public function setQuotation()
{
$api = $this->base_url . '/market/tickers';
$ch = new Curl();
//http代理
if (USER_PROXY) {
$ch->setOptions([
CURLOPT_PROXY => '127.0.0.1',
CURLOPT_PROXYPORT => 1080,
]);
}
$res = $ch->get($api, false);
if ($res && $res['status'] == 'ok') {
$datas = $res['data'];
/**
* @var $redis \yii\redis\Connection
*/
$redis = Yii::$app->redis;
foreach ($datas as $item) {
$key = $this->quotation_prefix . $item['symbol'];
$redis->hmset($key, 'low', $item['low'], 'high', $item['high'], 'last', $item['close']);
$redis->sadd($this->supported_symbol, $item['symbol']);
}
}
}
}
\ No newline at end of file
- 交易所服务层
从redis数据库读取行情数据
使用crontab定时更新数据库行情
1.支持交易对的保存方法:使用redis的set保存交易对
2.交易对行情的保存方法:使用redis的hashmap保存行情
- 键名:$prefix_$tag_$aim(eg: $prefix_byc_usd)
- 行情信息:
[
low=>'',//最低价
high=>'',//最高价
'last'=>'',//当前价格
]
\ No newline at end of file
...@@ -7,72 +7,15 @@ $params = array_merge( ...@@ -7,72 +7,15 @@ $params = array_merge(
); );
return [ return [
'id' => 'app-console', 'id' => 'app-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'bootstrap' => ['log'], 'bootstrap' => ['log'],
'controllerNamespace' => 'console\controllers', 'controllerNamespace' => 'console\controllers',
'aliases' => [ 'aliases' => [
'@bower' => '@vendor/bower-asset', '@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset', '@npm' => '@vendor/npm-asset',
], ],
'controllerMap' => [ 'components' => [
'fixture' => [
'class' => 'yii\console\controllers\FixtureController',
'namespace' => 'common\fixtures',
],
], ],
'components' => [ 'params' => $params,
/*'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],*/
/**
* db config
*/
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=rm-bp144nc2129h43c4i.mysql.rds.aliyuncs.com;dbname=manage',
'username' => 'fzmrisk',
'password' => '37uIrcBFk4nMnUjQ',
'charset' => 'utf8',
'tablePrefix' => 'gli_',
],
'db_parse' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=rr-6wea9n3nrh732s6fmdo.mysql.japan.rds.aliyuncs.com;dbname=parse',
'username' => 'risk',
'password' => 'risk@1001',
'charset' => 'utf8',
'tablePrefix' => 'fxee_',
],
'db_fxee' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=rr-6wea9n3nrh732s6fmdo.mysql.japan.rds.aliyuncs.com;dbname=zhaobi',
'username' => 'risk',
'password' => 'risk@1001',
'charset' => 'utf8',
'tablePrefix' => 'fxee_',
],
'weChat' => [
'class' => 'weChat\WeChat',
'transport' => [
'class' => 'weChat\WeChat_Transport',
'protocol' => 'https',
'host' => 'qyapi.weixin.qq.com',
'corpid' => 'wwa54b55ddca830a01',
'agentid' => '1000011',
'corpsecret' => 'MZxZ5yTAOwG_St71rZNS5o4I_uWuN8a0N31vAv2Izx0',
'department_id' => 10,
],
'messageConfig' => [
'class' => 'weChat\Message',
'setter' => 'weChat\MimeMessage',
]
],
],
'params' => $params,
]; ];
<?php <?php
return [ return [
'adminEmail' => 'admin@example.com',
]; ];
<?php
namespace console\controllers;
use common\models\fxee\CoinRecord;
use common\models\fxee\CashRecord;
use common\models\gli\Cache;
use yii\console\Controller;
use Yii;
class BellController extends Controller
{
public $groupChat = 'fzmcwxfd';
public $domain = 'http://114.55.0.98:6411/admin';
//http://miner.cn/admin
//http://114.55.0.98:6411/admin
/**
* 提币提示
* @author: libingke
*/
public function actionCoin()
{
if (Cache::getData('weChat_tb_open') != 1) {
return;
}
$history = Cache::getData('weChat_tb_history');
$history = is_numeric($history) ? $history : 0;
$lastMinId = Cache::getData('weChat_tb_min');
$lastMinId = is_numeric($lastMinId) ? $lastMinId : 0;
$last = Cache::getData('weChat_tb_last');
$last = is_numeric($last) ? $last : 0;
$query = CoinRecord::find()
->select('count(id) count, min(id) min, max(id) max,')
->where(['statu' => 3, 'type' => 1])
->andWhere(['not', ['currency' => 'USDT']])
->andWhere(['>', 'id', $last])
->asArray()
->one();
$count = isset($query['count']) ? $query['count'] : 0;
$maxId = isset($query['max']) ? $query['max'] : 0;
$minId = isset($query['min']) ? $query['min'] : 0;
if ($count > 0) {
$min = $lastMinId == 0 ? $minId : max($lastMinId, $minId);
Cache::setData('weChat_tb_min', $min);
Cache::setData('weChat_tb_last', $maxId);
Cache::setData('weChat_tb_history', $count + $history);
$msg = "新增申请数: $count\n"
. "历史未确认: $history\n"
. "流水编号范围:\n"
. " $min ~ $maxId\n";
$chat = Yii::$app->getWeChat()->compose();
$chat->setSendType(4);
$chat->setMessageType(5);
$chat->setParam('count', $count + $history);
$chat->setParam('maxId', $maxId);
$chat->setParam('minId', $min);
$chat->setUrl($this->domain . '/chat/finance-confirm?access=lhBub4yhASfXNnOhhkxBVRBuSiVG2a&handle=auto&type=tb');
$chat->setTo($this->groupChat);
$chat->setTitle('新提币申请');
$chat->setSendBody($msg);
$chat->send();
} else {
echo "no data";
}
}
/**
* 提款提示
* @author: libingke
*/
public function actionCash()
{
if (Cache::getData('weChat_tk_open') != 1) {
return;
}
$history = Cache::getData('weChat_tk_history');
$history = is_numeric($history) ? $history : 0;
$lastMinId = Cache::getData('weChat_tk_min');
$lastMinId = is_numeric($lastMinId) ? $lastMinId : 0;
$last = Cache::getData('weChat_tk_last');
$last = is_numeric($last) ? $last : 0;
$maxImport = CashRecord::find()
->select('max(id)')
->where(['statu' => 7, 'type' => 1])
->andWhere(['>', 'id', $last])
->asArray()
->scalar();
$newLast = max($maxImport, (string)$lastMinId);
$query = CashRecord::find()
->select('count(id) count, min(id) min, max(id) max,')
->where(['statu' => 6, 'type' => 1])
->andWhere(['>', 'id', $newLast])
->asArray()
->one();
$count = isset($query['count']) ? $query['count'] : 0;
$maxId = isset($query['max']) ? $query['max'] : 0;
$minId = isset($query['min']) ? $query['min'] : 0;
if ($count > 0) {
$min = $lastMinId == 0 ? $minId : max($lastMinId, $minId);
Cache::setData('weChat_tk_min', $min);
Cache::setData('weChat_tk_last', $maxId);
Cache::setData('weChat_tk_history', $count + $history);
$msg = "新增申请数: $count\n"
. "历史未确认: $history\n"
. "流水编号范围:\n"
. " $min ~ $maxId\n";
$chat = Yii::$app->getWeChat()->compose();
$chat->setSendType(4);
$chat->setMessageType(5);
$chat->setParam('count', $count);
$chat->setParam('maxId', $maxId);
$chat->setParam('minId', $minId);
$chat->setUrl($this->domain . '/chat/finance-confirm?access=lhBub4yhASfXNnOhhkxBVRBuSiVG2a&handle=auto&type=tk');
$chat->setTo($this->groupChat);
$chat->setTitle('¥ 新提款申请');
$chat->setSendBody($msg);
$chat->send();
} else {
echo "no data";
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-27
* Time: 上午10:15
*/
namespace console\controllers;
use yii\console\Controller;
use common\business\ExchangeBusiness;
/**
* Class ExchangeController
* 调用交易所api更新数据保存到redis
* @package console\controllers
*/
class ExchangeController extends Controller
{
/**
* 更新各个交易所支持的交易对
*/
public function actionSupportedSymbol()
{
ExchangeBusiness::setSupportedSymbol();
}
/**
* 更新交易对行情
*/
public function actionQuotation()
{
ExchangeBusiness::setQuotation();
}
}
php yii migrate/backup all
php yii migrate/up
\ No newline at end of file
<?php
use yii\db\Migration;
class m130524_201442_init extends Migration
{
public function up()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%user}}', [
'id' => $this->primaryKey(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
}
public function down()
{
$this->dropTable('{{%user}}');
}
}
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