Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wallet
token
Commits
9626c654
Commit
9626c654
authored
Jun 29, 2018
by
rlgy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
搜索币api v2
parent
f42616f8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
42 deletions
+108
-42
BaseController.php
api/base/BaseController.php
+14
-0
BaseResponse.php
api/base/BaseResponse.php
+3
-0
CoinController.php
api/controllers/v2/CoinController.php
+2
-1
ExchangeBusiness.php
common/business/ExchangeBusiness.php
+89
-41
No files found.
api/base/BaseController.php
View file @
9626c654
...
@@ -12,5 +12,18 @@ use yii\web\Controller;
...
@@ -12,5 +12,18 @@ use yii\web\Controller;
class
BaseController
extends
Controller
class
BaseController
extends
Controller
{
{
public
$start
;
public
$end
;
public
function
beforeAction
(
$action
)
{
$this
->
start
=
microtime
(
true
);
return
parent
::
beforeAction
(
$action
);
// TODO: Change the autogenerated stub
}
public
function
afterAction
(
$action
,
$result
)
{
$this
->
end
=
microtime
(
true
);
return
parent
::
afterAction
(
$action
,
$result
);
// TODO: Change the autogenerated stub
}
}
}
\ No newline at end of file
api/base/BaseResponse.php
View file @
9626c654
...
@@ -31,6 +31,9 @@ class BaseResponse extends Response
...
@@ -31,6 +31,9 @@ class BaseResponse extends Response
}
else
{
}
else
{
$return
=
$data
;
$return
=
$data
;
}
}
if
(
YII_ENV_DEV
)
{
$return
[
'time'
]
=
\Yii
::
$app
->
controller
->
end
-
\Yii
::
$app
->
controller
->
start
;
}
\Yii
::
$app
->
response
->
data
=
$return
;
\Yii
::
$app
->
response
->
data
=
$return
;
parent
::
send
();
parent
::
send
();
}
}
...
...
api/controllers/v2/CoinController.php
View file @
9626c654
...
@@ -92,7 +92,7 @@ class CoinController extends BaseController
...
@@ -92,7 +92,7 @@ class CoinController extends BaseController
$limit
=
$request
->
post
(
'limit'
,
10
);
$limit
=
$request
->
post
(
'limit'
,
10
);
if
(
$name
)
{
if
(
$name
)
{
$condition
=
[[
'or'
,
[
'like'
,
'name'
,
$name
],
[
'like'
,
'nickname'
,
$name
]]];
$condition
=
[[
'or'
,
[
'like'
,
'name'
,
$name
],
[
'like'
,
'nickname'
,
$name
]]];
return
Coin
Business
::
SearchByName
(
$page
,
$limit
,
$condition
);
return
Exchange
Business
::
SearchByName
(
$page
,
$limit
,
$condition
);
}
}
}
}
}
}
\ No newline at end of file
common/business/ExchangeBusiness.php
View file @
9626c654
...
@@ -19,6 +19,9 @@ use common\service\coin\CoinFactory;
...
@@ -19,6 +19,9 @@ use common\service\coin\CoinFactory;
*/
*/
class
ExchangeBusiness
class
ExchangeBusiness
{
{
/**支持的交易所
* @var array
*/
private
static
$exchanges
=
[
private
static
$exchanges
=
[
0
=>
'HuoBi'
,
0
=>
'HuoBi'
,
1
=>
'Hadax'
,
1
=>
'Hadax'
,
...
@@ -28,6 +31,59 @@ class ExchangeBusiness
...
@@ -28,6 +31,59 @@ class ExchangeBusiness
];
];
/**
/**
* 获取行情
* @param string $tag 目标币种
* @param string $aim 计数币种
* @return array|bool
*/
private
static
function
getquatation
(
$tag
=
'btc'
,
$aim
=
''
)
{
$f
=
false
;
$quotation
=
[];
foreach
(
self
::
$exchanges
as
$exchange
)
{
/**
* @var $exchange \common\service\exchange\Exchange
*/
$exchange
=
ExchangeFactory
::
createExchange
(
$exchange
);
if
(
$exchange
->
symbolExists
(
$tag
))
{
$quotation
=
$exchange
->
getTicker
(
$tag
);
$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\Exchange
*/
$exchange
=
ExchangeFactory
::
createExchange
(
$exchange
);
if
(
$exchange
->
symbolExists
(
$tag
,
'btc'
))
{
$price_btc
=
$exchange
->
getTicker
(
$tag
,
'btc'
);
//获取btcusdt
$result
=
array_map
(
function
(
$a
,
$b
)
{
return
$a
*
$b
;
},
$price_btc
,
$btc_usd
);
$quotation
=
[
'low'
=>
$result
[
0
],
'high'
=>
$result
[
1
],
'last'
=>
$result
[
2
]];
$f
=
true
;
break
;
}
}
}
if
(
!
$f
||
empty
(
$quotation
))
{
return
false
;
}
//格式化行情数据
foreach
(
$quotation
as
$key
=>
$item
)
{
$quotation
[
$key
]
=
(
double
)
$item
;
}
$quotation
[
'rmb'
]
=
6.6
*
$quotation
[
'last'
];
return
$quotation
;
}
/**
* 保存各个交易所支持的交易对到redis,定时用crontab更新
* 保存各个交易所支持的交易对到redis,定时用crontab更新
* @return void
* @return void
*/
*/
...
@@ -69,42 +125,40 @@ class ExchangeBusiness
...
@@ -69,42 +125,40 @@ class ExchangeBusiness
$rows
=
$rows
[
'data'
];
$rows
=
$rows
[
'data'
];
foreach
(
$rows
as
$key
=>
$row
)
{
foreach
(
$rows
as
$key
=>
$row
)
{
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
$f
=
false
;
//是否获取到行情
$quotation
=
self
::
getquatation
(
$row
[
'name'
]);
foreach
(
self
::
$exchanges
as
$exchange
)
{
if
(
!
$quotation
)
{
/**
//使用Coin服务
* @var $exchange \common\service\exchange\Exchange
$coinServer
=
CoinFactory
::
createCoin
(
$row
[
'name'
],
$row
[
'id'
],
$row
[
'sid'
]);
*/
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
$exchange
=
ExchangeFactory
::
createExchange
(
$exchange
);
$rows
[
$key
][
'rmb'
]
=
$coinServer
->
getPrice
();
if
(
$exchange
->
symbolExists
(
$row
[
'name'
]))
{
$rows
[
$key
][
'last'
]
=
$coinServer
->
getDollar
();
$rows
[
$key
]
=
array_merge
(
$rows
[
$key
],
$exchange
->
getTicker
(
$row
[
'name'
]));
$rows
[
$key
][
'low'
]
=
$coinServer
->
getLow
();
$f
=
true
;
$rows
[
$key
][
'high'
]
=
$coinServer
->
getHigh
();
break
;
$coinServer
->
__destruct
();
}
}
}
if
(
!
$f
)
{
$rows
[
$key
]
=
array_merge
(
$rows
[
$key
],
$quotation
);
//所有交易所都不能直接获取交易对的行情就通过BTC去转换
}
//获取BTC_USD
return
$rows
;
$btc_usd
=
ExchangeFactory
::
createExchange
(
self
::
$exchanges
[
0
])
->
getTicker
(
'btc'
);
}
return
[];
}
foreach
(
self
::
$exchanges
as
$exchange
)
{
/**
/**
* 根据名称搜索
* @var $exchange \common\service\exchange\Exchange
* @param int $page
*/
* @param int $limit
$exchange
=
ExchangeFactory
::
createExchange
(
$exchange
);
* @param array $condition
if
(
$exchange
->
symbolExists
(
$row
[
'name'
],
'btc'
))
{
* @return array|\yii\db\ActiveRecord|\yii\db\ActiveRecord[]
$price_btc
=
$exchange
->
getTicker
(
$row
[
'name'
],
'btc'
);
*/
//获取btcusdt
public
static
function
SearchByName
(
$page
=
1
,
$limit
=
10
,
$condition
=
[])
$result
=
array_map
(
function
(
$a
,
$b
)
{
{
return
$a
*
$b
;
$rows
=
Coin
::
getSelectList
(
$page
,
$limit
,
[
'id'
,
'sid'
,
'icon'
,
'name'
,
'nickname'
,
'chain'
],
$condition
);
},
$price_btc
,
$btc_usd
);
if
(
$rows
[
'count'
]
>
0
)
{
$rows
[
$key
]
=
array_merge
(
$rows
[
$key
],
$rows
=
$rows
[
'data'
];
[
'low'
=>
$result
[
0
],
'high'
=>
$result
[
1
],
'last'
=>
$result
[
2
]]);
foreach
(
$rows
as
$key
=>
$row
)
{
$f
=
true
;
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
break
;
$quotation
=
self
::
getquatation
(
$row
[
'name'
]);
}
if
(
!
$quotation
)
{
}
}
if
(
!
$f
)
{
//使用Coin服务
//使用Coin服务
$coinServer
=
CoinFactory
::
createCoin
(
$row
[
'name'
],
$row
[
'id'
],
$row
[
'sid'
]);
$coinServer
=
CoinFactory
::
createCoin
(
$row
[
'name'
],
$row
[
'id'
],
$row
[
'sid'
]);
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
$rows
[
$key
][
'sid'
]
=
ucfirst
(
$rows
[
$key
][
'sid'
]);
...
@@ -114,13 +168,7 @@ class ExchangeBusiness
...
@@ -114,13 +168,7 @@ class ExchangeBusiness
$rows
[
$key
][
'high'
]
=
$coinServer
->
getHigh
();
$rows
[
$key
][
'high'
]
=
$coinServer
->
getHigh
();
$coinServer
->
__destruct
();
$coinServer
->
__destruct
();
}
}
if
(
!
isset
(
$row
[
$key
][
'rmb'
]))
{
$rows
[
$key
]
=
array_merge
(
$rows
[
$key
],
$quotation
);
$rows
[
$key
][
'rmb'
]
=
6.6
*
$rows
[
$key
][
'last'
];
}
//转化价格为数字
$rows
[
$key
][
'low'
]
=
(
double
)
$rows
[
$key
][
'low'
];
$rows
[
$key
][
'high'
]
=
(
double
)
$rows
[
$key
][
'high'
];
$rows
[
$key
][
'last'
]
=
(
double
)
$rows
[
$key
][
'last'
];
}
}
return
$rows
;
return
$rows
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment