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
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-5-31
* Time: 上午9:56
*/
namespace backend\controllers;
use Yii;
use common\models\pwallet\Coin;
use backend\models\coin\CoinForm;
use yii\web\UploadedFile;
use yii\validators\ImageValidator;
use common\business\CoinBusiness;
/**
* 币种管理控制器
* Class CoinController
* @package backend\controllers
*/
class CoinController extends BaseController
{
public function actionIndex()
{
if (Yii::$app->request->isAjax) {
$request = Yii::$app->request;
$page = $request->get('page', 1);
$limit = $request->get('limit', 10);
$name = $request->get('name', null);
$platform = $request->get('platform', 'all');
$recommend = $request->get('recommend', 'all');
$condition = [];
if ($name) {
$condition[] = ['like', 'name', $name];
}
if ($platform != 'all') {
$condition[] = ['chain' => $platform];
}
if ($recommend != 'all') {
$recommend = $recommend ? 1 : 0;
$condition[] = ['recommend' => $recommend];
}
$data = CoinBusiness::getList($page, $limit, $condition);
$data['code'] = 0;
//ajax return
Yii::$app->response->format = 'json';
return $data;
}
$platforms = Coin::getChainList();
return $this->render('index', ['platforms' => $platforms]);
}
public function actionAdd()
{
$model = new CoinForm();
$model->scenario = 'add';
if (Yii::$app->request->isPost) {
$request = Yii::$app->request;
if ($model->load($request->post()) && $model->validate()) {
/**
* @var $coin \common\models\pwallet\Coin
*/
$coin = Yii::createObject(Coin::className());
$result = $coin->addOne($request->post());
if ($result === true) {
$this->success('添加成功', '/admin/coin/index');
}
}
//表单验证失败
$errors = $model->errors;
if ($errors) {
foreach ($errors as $key => $item) {
$errors = $item[0];
break;
}
} elseif (isset($result) && $result['code'] != 0) {
$errors = $result['message'];
}
$this->error($errors, Yii::$app->request->getReferrer());
}
return $this->render('add', ['model' => $model]);
}
public function actionEdit()
{
if (Yii::$app->request->isPost) {
$model = new CoinForm();
$model->scenario = 'update';
$req = Yii::$app->request;
Yii::$app->response->format = 'json';
if ($model->load($req->post()) && $model->validate()) {
$coin = Yii::createObject(Coin::className());
$result = $coin->updateOne($req->post());
if ($result === true) {
return ['code' => 0, 'msg' => 'succeed'];
}
}
$errors = $model->errors;
if ($errors) {
foreach ($errors as $key => $item) {
$errors = $item[0];
break;
}
} elseif (isset($result) && $result['code'] != 0) {
$errors = $result['message'];
}
return ['code' => 1, 'msg' => $errors];
} elseif (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id', null);
if ($id) {
$coin = Coin::findOne(['id' => $id]);
$this->layout = false;
return $this->render('edit', ['model' => $coin]);
}
}
}
/**
* 上传币种图标
*/
public function actionUpload()
{
Yii::$app->response->format = 'json';
$uploaded_file = UploadedFile::getInstanceByName('file');
try {
/**
* @var $validator ImageValidator
*/
$validator = Yii::createObject(ImageValidator::className(), ['extensions' => 'png,jpg,gif']);
if ($validator->validate($uploaded_file)) {
$src = Yii::$app->uploader->upload($uploaded_file);
if ($src === false) {
return ['code' => 1, 'msg' => '上传失败'];
} else {
return ['code' => 0, 'data' => ['src' => $src]];
}
}
} catch (\Exception $exception) {
return ['code' => $exception->getCode(), 'msg' => $exception->getMessage()];
}
}
public function actionDelete()
{
$id = Yii::$app->request->get('id', 0);
if ($id) {
$model = Coin::findOne(['id' => $id]);
if ($model) {
try {
$model->delete();
$this->success('删除成功', '/admin/coin/index', true);
} catch (\Throwable $t) {
} catch (\Exception $e) {
}
}
}
$this->error('删除失败', Yii::$app->request->getReferrer(), true);
}
public function actionGetExchangeListById()
{
$id = Yii::$app->request->get('id', 0);
$exchanges = [];
if ($id) {
$exchanges = CoinBusiness::getExchangeListById($id);
}
$this->layout = false;
return $this->render('exchange', ['exchanges' => $exchanges]);
}
}