@@ -24,6 +24,7 @@ class LoginController extends Controller
2424{
2525 //
2626 use Tool;
27+ 2728 public function __construct ()
2829 {
2930 $ this ->middleware ('auth:api ' , ['except ' => ['login ' , 'refresh ' , 'loginByPhone ' , 'captcha ' , 'test ' ]]);
@@ -60,16 +61,16 @@ public function captcha()
6061
6162 protected function checkCode ()
6263 {
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+ }
7374 }
7475
7576 /**
@@ -88,61 +89,75 @@ public function login()
8889 $ verify_code = env ('VERIFY_CODE ' , false );
8990 $ verify_result = $ this ->checkCode ();
9091 if ($ verify_code && is_string ($ verify_result )) { // 开启验证码, 但是验证码不正确,则返回错误信息
91- return $ this ->errorWithInfo ($ verify_result , 400 );
92+ return $ this ->errorWithInfo ($ verify_result , 400 );
9293 }
9394
9495 if (($ verify_code && $ verify_result ) || !$ verify_code ) { // 开启验证码,并且验证码正确,或者没有开启验证码都可以进行登陆
9596 // 兼容登录名和手机号登陆
9697 $ item = DB ::table ('admins ' )->where ('email ' , $ username )->orWhere ('phone ' , $ username )->first ();
9798 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+ // 密码相等
101102// 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+ }
109110 }
110111 }
111112 }
112113
113- public function bind (){
114- $ client_id = request ('uuid ' );
114+ public function bind ()
115+ {
116+ $ client_id = request ('uuid ' );
115117 $ uid = Auth::id ();
116- $ address = env ('REGISTER_ADDRESS ' ,'127.0.0.1:1680 ' );
118+ $ address = env ('REGISTER_ADDRESS ' ,'127.0.0.1:1680 ' );
117119 Gateway::$ registerAddress = $ address ;
118120 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- 124121 // 获得所有的client_id,删除除了该次登录的内容以外,剔除其他的客户端,前端自动的退出
125122 $ arr = Gateway::getClientIdByUid ($ uid );
126123 // 获得之前登录的所有client_id
127124 unset($ arr [array_search ($ client_id , $ arr )]); // 剔除当前登录的client_id后剩余的client_id内容,保证永远一对一,前端用于剔除之前登录的用户
128125 $ 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+ }
135135 return $ this ->success ();
136136 }
137137
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+ 138153 /**
139154 * 获取管理员信息
140155 * @authenticated
141156 * @return \Illuminate\Http\JsonResponse
142157 */
143158 public function me ()
144159 {
145- $ admin = auth ('api ' )->user ();
160+ $ admin = auth ('api ' )->user ();
146161 $ data = Admin::find ($ admin ->id );
147162 return new \App \Http \Resources \Admin ($ data );
148163 }
@@ -155,18 +170,18 @@ public function me()
155170 */
156171 public function logout ()
157172 {
158- if (Auth::check ()){
173+ if (Auth::check ()){
159174 $ id = Auth::id ();
160175 $ uuid = request ('uuid ' , null );
161176 // 取消client_id与uid的绑定
162177 if ($ uuid ) {
163- Gateway::unbindUid ($ uuid , $ id );
164- Gateway::closeClient ($ uuid );
178+ Gateway::unbindUid ($ uuid , $ id );
179+ Gateway::closeClient ($ uuid );
165180 }
166- Auth::user ()->token ()->delete ();
181+ Auth::user ()->token ()->delete ();
167182// $admin = Auth::user();
168183// DB::table('oauth_access_tokens')->where('user_id', $admin->id)->update(['revoked' => 1]);
169- return $ this ->successWithInfo ('退出成功 ' );
184+ return $ this ->successWithInfo ('退出成功 ' );
170185 }
171186 }
172187
@@ -175,7 +190,7 @@ public function logout()
175190 * @return \Illuminate\Http\JsonResponse
176191 */
177192
178- public function refresh (Request $ request )
193+ public function refresh (Request $ request )
179194 {
180195 $ refreshToken = $ request ->input ('refresh_token ' , '' );
181196 if (empty ($ refreshToken )) {
@@ -185,7 +200,7 @@ public function refresh(Request $request)
185200 $ data = [
186201 'grant_type ' => 'refresh_token ' ,
187202 'refresh_token ' => $ refreshToken ,
188- 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
203+ 'client_id ' => env ('PASSPORT_CLIENT_ID ' ),
189204 'client_secret ' => env ('PASSPORT_CLIENT_SECRET ' ),
190205 'scope ' => '' ,
191206 ];
@@ -195,70 +210,72 @@ public function refresh(Request $request)
195210 }
196211
197212
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+ }
229246
230247 public function loginByPhone ()
231248 {
232249 $ verify_code = env ('VERIFY_CODE ' , false );
233250 $ verify_result = $ this ->checkCode ();
234251 if ($ verify_code && is_string ($ verify_result )) { // 开启验证码, 但是验证码不正确,则返回错误信息
235- return $ this ->errorWithInfo ($ verify_result , 400 );
252+ return $ this ->errorWithInfo ($ verify_result , 400 );
236253 }
237254
238255 $ result = $ this ->verify_code ();
239- if (is_string ($ result )){
256+ if (is_string ($ result )){
240257 return $ this ->errorWithInfo ($ result , 400 );
241258 }
242259 if ((is_bool ($ result ) && $ result && $ verify_code && $ verify_result ) || (is_bool ($ result ) && $ result && !$ verify_code )) {
243260 // 开启校验码功能后,手机验证码和图像验证码都正确了,就使用手机号码登陆 或者没有开启校验码功能,则只需要手机验证码正确了就可以登陆了
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+ }
262279 } else {
263280 return $ this ->errorWithInfo ('验证码出错,无法登陆 ' , 400 );
264281 }
@@ -269,13 +286,13 @@ protected function verify_code()
269286 {
270287 $ code = request ('phone_code ' );
271288 $ phone = request ('phone ' );
272- $ value = Cache::has ($ phone )? Cache::get ($ phone ): false ;
289+ $ value = Cache::has ($ phone )? Cache::get ($ phone ) : false ;
273290 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+ }
279296 } else {
280297 return '该手机验证码已经过期,请重新发送 ' ;
281298 }
0 commit comments