-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteController.php
executable file
·231 lines (215 loc) · 6.91 KB
/
SiteController.php
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
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Test;
use yii\db\QueryInterface;
use yii\db\QueryTrait;
use yii\mongodb\Query;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;
/**
* @SWG\Swagger(
* basePath="/",
* produces={"application/json"},
* consumes={"application/x-www-form-urlencoded"},
* @SWG\Info(version="1.0", title="Simple API"),
* )
*/
class SiteController extends Controller
{
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
//$customers = Customer::find()->with('orders')->asArray()->all();
$session = Yii::$app->session;
$session->set('test11',2222);
$cc = $session->get('test11');
var_dump($cc);
die;
//$customers = Customer::find()->with('orders')->asArray()->all();
$db = Yii::$app->db;
$transaction = $db->beginTransaction(); //开启事务
try {
for($i=1;$i<=3;$i++) {
$model = new Test();
$model->name = 111;
$model->name1 = 222;
$model->name2 = 333;
$model->name3 = 4444;
$Res = $model->insert();
if (!$Res) {
throw new \Exception("save fail 1");//抛出异
}
}
$model = new Test();
$model->name1111 = 222;
$Res = $model->insert();
var_dump($Res);
if (!$Res) {
throw new \Exception("save fail 2");//抛出异
}
$transaction->commit();
} catch (\Exception $e) {
// 记录回滚(事务回滚)
$transaction->rollBack();
echo $e->getMessage();
}
var_dump(1111);die;
$data = [
"id"=>"zhansg",
"name"=>111
];
//添加
$model = new Test();
$model->name= $data['name'];
$model->name1= $data['name'];
$model->name2= $data['name'];
$model->name3= json_encode($data);
$Res = $model->insert();
$dada = Test::findAll(['name'=>111]);
echo '<pre>';
//print_r($dada);die;
//删除
$q = Test::find()->where(['name'=>111])->one();
// $cc = $q->delete();
print_r($q);die;
//更新,查询
$q = Test::find()->where(['name'=>222])->one();
$q->name=221222;
$cc = $q->update();
print_r($cc);die;
return $this->render('index');
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin()
{
$request = Yii::$app->getRequest();
$signer = new Sha256();//使用Sha256加密,常用加密方式有Sha256,Sha384,Sha512
$time = time();
$tokenBuilder = (new Builder())
->issuedBy($request->getHostInfo()) // 设置发行人
->permittedFor(isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '') // 设置接收
->identifiedBy(Yii::$app->security->generateRandomString(10), true) // 设置id
->issuedAt($time) // 设置生成token的时间
->canOnlyBeUsedAfter($time) // 设置token使用时间(实时使用)
->expiresAt($time + 3600); //设置token过期时间
//定义自己所需字段
$user = $request->post();
$tokenBuilder->withClaim('user', $user);
$tokenBuilder->withClaim('ceshi', '测试字段');
//使用Sha256加密生成token对象,该对象的字符串形式为一个JWT字符串
$token = $tokenBuilder->getToken($signer, new Key('jwt_secret'));
return (string)$token;
}
/**
* 校验token
*
* @return Response|string
*/
public function actionGetTokenInfo()
{
$request = Yii::$app->getRequest()->post();
$token = (new Parser())->parse($request['token']);
//数据校验
$data = new ValidationData(); // 使用当前时间来校验数据
if (!$token->validate($data)) {
//数据校验失败
return '数据校验失败';
}
//token校验
$signer = new Sha256();//生成JWT时使用的加密方式
if (!$token->verify($signer, new Key('jwt_secret'))) {
//token校验失败
return 'token校验失败';
}
echo '校验成功';
}
/**
* 检测到期时间
*
* @return Response|string
*/
public function actionCheckToken()
{
$request = Yii::$app->getRequest()->post();
$token = (new Parser())->parse($request['token']);
$token->getHeaders(); // 获取JWT的Header(头部)信息
$token->getClaims(); // 获取JWT的PayLoad(负载)信息
//获取指定参数的PayLoad(负载)信息
$token->getClaim('jti');
$token->getClaim('user');
$token->isExpired();
var_dump($token);
//得到过期时间
// var_dump($token->getClaim('exp'));
//正常情况下,先判断用户的token过期没有,
//没有的话 比较字段里面的user(自定义的字段)
//["exp"]=>
//object(Lcobucci\JWT\Claim\GreaterOrEqualsTo)#88 (2) {
//["name":"Lcobucci\JWT\Claim\Basic":private]=>
//string(3) "exp"
//["value":"Lcobucci\JWT\Claim\Basic":private]=>
//int(1590395730)
//}
//["user"]=>
//object(Lcobucci\JWT\Claim\Basic)#89 (2) {
//["name":"Lcobucci\JWT\Claim\Basic":private]=>
//string(4) "user"
//["value":"Lcobucci\JWT\Claim\Basic":private]=>
//object(stdClass)#83 (2) {
//["user_name"]=>
//string(6) "测试"
//["user_no"]=>
//string(3) "001"
//}
//}
//["ceshi"]=>
//object(Lcobucci\JWT\Claim\Basic)#90 (2) {
//["name":"Lcobucci\JWT\Claim\Basic":private]=>
//string(5) "ceshi"
//["value":"Lcobucci\JWT\Claim\Basic":private]=>
//string(12) "测试字段"
//}
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'docs' => [
'class' => 'yii2mod\swagger\SwaggerUIRenderer',
'restUrl' => Url::to(['site/json-schema']),
],
'json-schema' => [
'class' => 'yii2mod\swagger\OpenAPIRenderer',
// Тhe list of directories that contains the swagger annotations.
'scanDir' => [
Yii::getAlias('@app/controllers'),
Yii::getAlias('@app/models'),
],
],
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
}