@@ -24,6 +24,7 @@ class LoginController extends Controller
24
24
{
25
25
//
26
26
use Tool;
27
+
27
28
public function __construct ()
28
29
{
29
30
$ this ->middleware ('auth:api ' , ['except ' => ['login ' , 'refresh ' , 'loginByPhone ' , 'captcha ' , 'test ' ]]);
@@ -60,16 +61,16 @@ public function captcha()
60
61
61
62
protected function checkCode ()
62
63
{
63
- $ code = request ('code ' , '' );
64
- $ key = request ('key ' , '' );
65
- if ($ code === 'A123456789 ' ) { // 万能验证码,调试接口时候使用
66
- return true ;
67
- }
68
- if (!captcha_api_check ($ code , $ key )){
69
- return '图像验证码不匹配, 请重新填写 ' ;
70
- } else {
71
- return true ;
72
- }
64
+ $ code = request ('code ' , '' );
65
+ $ key = request ('key ' , '' );
66
+ if ($ code === 'A123456789 ' ) { // 万能验证码,调试接口时候使用
67
+ return true ;
68
+ }
69
+ if (!captcha_api_check ($ code , $ key )) {
70
+ return '图像验证码不匹配, 请重新填写 ' ;
71
+ } else {
72
+ return true ;
73
+ }
73
74
}
74
75
75
76
/**
@@ -88,61 +89,75 @@ public function login()
88
89
$ verify_code = env ('VERIFY_CODE ' , false );
89
90
$ verify_result = $ this ->checkCode ();
90
91
if ($ verify_code && is_string ($ verify_result )) { // 开启验证码, 但是验证码不正确,则返回错误信息
91
- return $ this ->errorWithInfo ($ verify_result , 400 );
92
+ return $ this ->errorWithInfo ($ verify_result , 400 );
92
93
}
93
94
94
95
if (($ verify_code && $ verify_result ) || !$ verify_code ) { // 开启验证码,并且验证码正确,或者没有开启验证码都可以进行登陆
95
96
// 兼容登录名和手机号登陆
96
97
$ item = DB ::table ('admins ' )->where ('email ' , $ username )->orWhere ('phone ' , $ username )->first ();
97
98
if ($ item && $ item ->status === 1 ) {
98
- $ pwd = $ item ->password ;
99
- if (Hash::check ($ password , $ pwd )) {
100
- // 密码相等
99
+ $ pwd = $ item ->password ;
100
+ if (Hash::check ($ password , $ pwd )) {
101
+ // 密码相等
101
102
// DB::table('oauth_access_tokens')->where('user_id', $item->id)->update(['revoked' => 1]);
102
- $ result = $ this ->proxy ($ username , $ password );
103
- $ admin =Admin::find ($ item ->id );
104
- event (new UserLogin ($ admin ));
105
- return $ result ;
106
- } else {
107
- return $ this ->errorWithInfo ('认证出错,用户名或者密码不对 ' , 401 );
108
- }
103
+ $ result = $ this ->proxy ($ username , $ password );
104
+ $ admin = Admin::find ($ item ->id );
105
+ event (new UserLogin ($ admin ));
106
+ return $ result ;
107
+ } else {
108
+ return $ this ->errorWithInfo ('认证出错,用户名或者密码不对 ' , 401 );
109
+ }
109
110
}
110
111
}
111
112
}
112
113
113
- public function bind (){
114
- $ client_id = request ('uuid ' );
114
+ public function bind ()
115
+ {
116
+ $ client_id = request ('uuid ' );
115
117
$ uid = Auth::id ();
116
- $ address = env ('REGISTER_ADDRESS ' ,'127.0.0.1:1680 ' );
118
+ $ address = env ('REGISTER_ADDRESS ' , '127.0.0.1:1680 ' );
117
119
Gateway::$ registerAddress = $ address ;
118
120
Gateway::bindUid ($ client_id , $ uid );
119
- $ old_user_id = Gateway::getUidByClientId ($ client_id );
120
- var_dump ($ uid );
121
- var_dump ($ old_user_id );
122
- //
123
-
124
121
// 获得所有的client_id,删除除了该次登录的内容以外,剔除其他的客户端,前端自动的退出
125
122
$ arr = Gateway::getClientIdByUid ($ uid );
126
123
// 获得之前登录的所有client_id
127
124
unset($ arr [array_search ($ client_id , $ arr )]); // 剔除当前登录的client_id后剩余的client_id内容,保证永远一对一,前端用于剔除之前登录的用户
128
125
$ arr = array_values ($ arr ); // 此操作非常重要,这样才能保证经过json编码后为数组
129
- $ result = [
130
- 'type ' => 'logout ' ,
131
- 'content ' => null ,
132
- 'select ' => 'all ' ,
133
- ];
134
- Gateway::sendToAll (json_encode ($ result ), $ arr );
126
+ if (count ($ arr ) >= 1 ) {
127
+ var_dump (count ($ arr ));
128
+ $ result = [
129
+ 'type ' => 'logout ' ,
130
+ 'content ' => null ,
131
+ 'select ' => 'all ' ,
132
+ ];
133
+ Gateway::sendToAll (json_encode ($ result ), $ arr );
134
+ }
135
135
return $ this ->success ();
136
136
}
137
137
138
+ public function unBind ()
139
+ {
140
+ $ client_id = $ this ->initGateWay ();
141
+ $ this ->initGateWay ();
142
+
143
+
144
+
145
+ }
146
+
147
+ protected function initGateWay ()
148
+ {
149
+ $ address = env ('REGISTER_ADDRESS ' , '127.0.0.1:1680 ' );
150
+ Gateway::$ registerAddress = $ address ;
151
+ }
152
+
138
153
/**
139
154
* 获取管理员信息
140
155
* @authenticated
141
156
* @return \Illuminate\Http\JsonResponse
142
157
*/
143
158
public function me ()
144
159
{
145
- $ admin = auth ('api ' )->user ();
160
+ $ admin = auth ('api ' )->user ();
146
161
$ data = Admin::find ($ admin ->id );
147
162
return new \App \Http \Resources \Admin ($ data );
148
163
}
@@ -155,18 +170,18 @@ public function me()
155
170
*/
156
171
public function logout ()
157
172
{
158
- if (Auth::check ()){
173
+ if (Auth::check ()) {
159
174
$ id = Auth::id ();
160
175
$ uuid = request ('uuid ' , null );
161
176
// 取消client_id与uid的绑定
162
177
if ($ uuid ) {
163
- Gateway::unbindUid ($ uuid , $ id );
164
- Gateway::closeClient ($ uuid );
178
+ Gateway::unbindUid ($ uuid , $ id );
179
+ Gateway::closeClient ($ uuid );
165
180
}
166
- Auth::user ()->token ()->delete ();
181
+ Auth::user ()->token ()->delete ();
167
182
// $admin = Auth::user();
168
183
// DB::table('oauth_access_tokens')->where('user_id', $admin->id)->update(['revoked' => 1]);
169
- return $ this ->successWithInfo ('退出成功 ' );
184
+ return $ this ->successWithInfo ('退出成功 ' );
170
185
}
171
186
}
172
187
@@ -175,7 +190,7 @@ public function logout()
175
190
* @return \Illuminate\Http\JsonResponse
176
191
*/
177
192
178
- public function refresh (Request $ request )
193
+ public function refresh (Request $ request )
179
194
{
180
195
$ refreshToken = $ request ->input ('refresh_token ' , '' );
181
196
if (empty ($ refreshToken )) {
@@ -185,7 +200,7 @@ public function refresh(Request $request)
185
200
$ data = [
186
201
'grant_type ' => 'refresh_token ' ,
187
202
'refresh_token ' => $ refreshToken ,
188
- 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
203
+ 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
189
204
'client_secret ' => env ('PASSPORT_CLIENT_SECRET ' ),
190
205
'scope ' => '' ,
191
206
];
@@ -195,70 +210,72 @@ public function refresh(Request $request)
195
210
}
196
211
197
212
198
- }
199
-
200
- protected function proxy ($ username , $ password ){
201
- $ data = [
202
- 'grant_type ' => 'password ' ,
203
- 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
204
- 'client_secret ' => env ('PASSPORT_CLIENT_SECRET ' ),
205
- 'username ' => $ username ,
206
- 'password ' => $ password ,
207
- 'scope ' => '' ,
208
- ];
209
- return $ this ->token ($ data );
210
-
211
- }
212
-
213
- protected function token ($ data = []){
214
- $ http = new Client ();
215
- $ url = env ('APP_URL ' );
216
- $ result = $ http ->post ("$ url/oauth/token " , [
217
- 'form_params ' => $ data ,
218
- "verify " => false
219
- ]);
220
- $ result = json_decode ((string ) $ result ->getBody (), true );
221
- return response ()->json ([
222
- 'access_token ' => $ result ['access_token ' ],
223
- 'expires_in ' => $ result ['expires_in ' ],
224
- 'refresh_token ' => $ result ['refresh_token ' ],
225
- 'status ' => 'success ' ,
226
- 'status_code ' => 200
227
- ], 200 );
228
- }
213
+ }
214
+
215
+ protected function proxy ($ username , $ password )
216
+ {
217
+ $ data = [
218
+ 'grant_type ' => 'password ' ,
219
+ 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
220
+ 'client_secret ' => env ('PASSPORT_CLIENT_SECRET ' ),
221
+ 'username ' => $ username ,
222
+ 'password ' => $ password ,
223
+ 'scope ' => '' ,
224
+ ];
225
+ return $ this ->token ($ data );
226
+
227
+ }
228
+
229
+ protected function token ($ data = [])
230
+ {
231
+ $ http = new Client ();
232
+ $ url = env ('APP_URL ' );
233
+ $ result = $ http ->post ("$ url/oauth/token " , [
234
+ 'form_params ' => $ data ,
235
+ "verify " => false
236
+ ]);
237
+ $ result = json_decode ((string )$ result ->getBody (), true );
238
+ return response ()->json ([
239
+ 'access_token ' => $ result ['access_token ' ],
240
+ 'expires_in ' => $ result ['expires_in ' ],
241
+ 'refresh_token ' => $ result ['refresh_token ' ],
242
+ 'status ' => 'success ' ,
243
+ 'status_code ' => 200
244
+ ], 200 );
245
+ }
229
246
230
247
public function loginByPhone ()
231
248
{
232
249
$ verify_code = env ('VERIFY_CODE ' , false );
233
250
$ verify_result = $ this ->checkCode ();
234
251
if ($ verify_code && is_string ($ verify_result )) { // 开启验证码, 但是验证码不正确,则返回错误信息
235
- return $ this ->errorWithInfo ($ verify_result , 400 );
252
+ return $ this ->errorWithInfo ($ verify_result , 400 );
236
253
}
237
254
238
255
$ result = $ this ->verify_code ();
239
- if (is_string ($ result )){
256
+ if (is_string ($ result )) {
240
257
return $ this ->errorWithInfo ($ result , 400 );
241
258
}
242
259
if ((is_bool ($ result ) && $ result && $ verify_code && $ verify_result ) || (is_bool ($ result ) && $ result && !$ verify_code )) {
243
260
// 开启校验码功能后,手机验证码和图像验证码都正确了,就使用手机号码登陆 或者没有开启校验码功能,则只需要手机验证码正确了就可以登陆了
244
- $ phone = request ('phone ' );
245
- $ faker = Factory::create ();
246
- $ pwd = $ faker ->regexify ('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4} ' );
247
- $ item = Admin::where ('phone ' , $ phone )->first ();
248
- if ($ item ) {
249
- // 为了能发放令牌,需要修改一个用户的密码,然后进行验证后再返回密码
250
- $ password = $ item ->password ;
251
- Admin::where ('phone ' , $ phone )->update ([
252
- 'password ' => bcrypt ($ pwd )
253
- ]);
254
- $ result = $ this ->proxy ($ phone , $ pwd );
255
- Admin::where ('phone ' , $ phone )->update ([
256
- 'password ' => $ password
257
- ]);
258
- return $ result ;
259
- } else {
260
- return $ this ->errorWithInfo ('没有指定的手机号码,无法登陆 ' , 400 );
261
- }
261
+ $ phone = request ('phone ' );
262
+ $ faker = Factory::create ();
263
+ $ pwd = $ faker ->regexify ('[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4} ' );
264
+ $ item = Admin::where ('phone ' , $ phone )->first ();
265
+ if ($ item ) {
266
+ // 为了能发放令牌,需要修改一个用户的密码,然后进行验证后再返回密码
267
+ $ password = $ item ->password ;
268
+ Admin::where ('phone ' , $ phone )->update ([
269
+ 'password ' => bcrypt ($ pwd )
270
+ ]);
271
+ $ result = $ this ->proxy ($ phone , $ pwd );
272
+ Admin::where ('phone ' , $ phone )->update ([
273
+ 'password ' => $ password
274
+ ]);
275
+ return $ result ;
276
+ } else {
277
+ return $ this ->errorWithInfo ('没有指定的手机号码,无法登陆 ' , 400 );
278
+ }
262
279
} else {
263
280
return $ this ->errorWithInfo ('验证码出错,无法登陆 ' , 400 );
264
281
}
@@ -269,13 +286,13 @@ protected function verify_code()
269
286
{
270
287
$ code = request ('phone_code ' );
271
288
$ phone = request ('phone ' );
272
- $ value = Cache::has ($ phone )? Cache::get ($ phone ): false ;
289
+ $ value = Cache::has ($ phone ) ? Cache::get ($ phone ) : false ;
273
290
if ($ value ) {
274
- if ((int )$ value === (int )$ code ) {
275
- return true ;
276
- } else {
277
- return false ;
278
- }
291
+ if ((int )$ value === (int )$ code ) {
292
+ return true ;
293
+ } else {
294
+ return false ;
295
+ }
279
296
} else {
280
297
return '该手机验证码已经过期,请重新发送 ' ;
281
298
}
0 commit comments