1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?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;
}
}