作者 Richer

功能优化:1、公众号授权功能修改;2、其他功能优化

@@ -36,7 +36,7 @@ trait CategoryGrid @@ -36,7 +36,7 @@ trait CategoryGrid
36 public function setGridOrder() 36 public function setGridOrder()
37 { 37 {
38 if (!request('_sort')) { 38 if (!request('_sort')) {
39 - $this->grid->model()->orderBy('sort')->latest(); 39 + $this->grid->model()->orderByDesc('sort')->latest();
40 } else { 40 } else {
41 $this->setGridOrderSpecial(); 41 $this->setGridOrderSpecial();
42 } 42 }
@@ -23,6 +23,7 @@ use App\Http\Requests\RegisterRequest; @@ -23,6 +23,7 @@ use App\Http\Requests\RegisterRequest;
23 use App\Http\Requests\ResetPasswordRequest; 23 use App\Http\Requests\ResetPasswordRequest;
24 use Illuminate\Http\Response; 24 use Illuminate\Http\Response;
25 use Illuminate\Support\Facades\Auth; 25 use Illuminate\Support\Facades\Auth;
  26 +use Illuminate\Support\Facades\Log;
26 27
27 /** 28 /**
28 * Class AuthController 29 * Class AuthController
@@ -101,10 +102,15 @@ class AuthController extends BaseController @@ -101,10 +102,15 @@ class AuthController extends BaseController
101 */ 102 */
102 public function wechatAuth(Request $request) 103 public function wechatAuth(Request $request)
103 { 104 {
  105 + record_log('wechat-auth', '微信公众号授权登录' , 'begin');
104 $result = $this->service->wechatAuth($request); 106 $result = $this->service->wechatAuth($request);
105 if ($result) { 107 if ($result) {
  108 +// Log::info($result);
  109 + record_log('wechat-auth', '返回信息:' .json_encode($result));
  110 + record_log('wechat-auth', '微信公众号授权登录' , 'end');
106 return $this->success($result, __('login') . ' ' . __('succeeded'), 0, $result->token); 111 return $this->success($result, __('login') . ' ' . __('succeeded'), 0, $result->token);
107 } else { 112 } else {
  113 + record_log('wechat-auth', '微信公众号授权登录' , 'end');
108 return $this->error($this->service->getMessage(), 2006); 114 return $this->error($this->service->getMessage(), 2006);
109 } 115 }
110 } 116 }
@@ -78,6 +78,9 @@ class VerifyJWTToken extends BaseMiddleware @@ -78,6 +78,9 @@ class VerifyJWTToken extends BaseMiddleware
78 try { 78 try {
79 // add by Richer 于 2021年6月16日11:19:49 获取用户的角色,根据角色来判断不同的用户表 79 // add by Richer 于 2021年6月16日11:19:49 获取用户的角色,根据角色来判断不同的用户表
80 $role = $this->auth->parseToken()->getClaim('role'); 80 $role = $this->auth->parseToken()->getClaim('role');
  81 + if ($request->test == 1) {
  82 + dump($role);
  83 + }
81 // 通过令牌验证用户。 84 // 通过令牌验证用户。
82 if ($role === 'admin') { 85 if ($role === 'admin') {
83 $user = \Auth::guard('api_admin')->authenticate(); 86 $user = \Auth::guard('api_admin')->authenticate();
@@ -85,6 +88,10 @@ class VerifyJWTToken extends BaseMiddleware @@ -85,6 +88,10 @@ class VerifyJWTToken extends BaseMiddleware
85 $user = $this->auth->parseToken()->authenticate(); 88 $user = $this->auth->parseToken()->authenticate();
86 } 89 }
87 90
  91 + if ($request->test == 1) {
  92 + dump($user);
  93 + }
  94 +
88 if (!$user) { 95 if (!$user) {
89 //获取到用户数据,并赋值给$user 96 //获取到用户数据,并赋值给$user
90 return response()->json([ 97 return response()->json([
@@ -82,7 +82,8 @@ class Category extends BaseModel @@ -82,7 +82,8 @@ class Category extends BaseModel
82 */ 82 */
83 public function children(): HasMany 83 public function children(): HasMany
84 { 84 {
85 - return $this->hasMany(get_class($this), 'pid', 'id'); 85 + return $this->hasMany(get_class($this), 'pid', 'id')->where('status', 1)
  86 + ;
86 } 87 }
87 88
88 /** 89 /**
@@ -42,7 +42,7 @@ class AppServiceProvider extends ServiceProvider @@ -42,7 +42,7 @@ class AppServiceProvider extends ServiceProvider
42 }); 42 });
43 43
44 API::error(function (\Exception $exception) { 44 API::error(function (\Exception $exception) {
45 - dd($exception); 45 +// dd($exception);
46 return response()->json(['code'=>2004,'msg'=> $exception->getMessage(),'data'=> null ], 200); 46 return response()->json(['code'=>2004,'msg'=> $exception->getMessage(),'data'=> null ], 200);
47 }); 47 });
48 48
@@ -321,6 +321,8 @@ class AuthService extends BaseService @@ -321,6 +321,8 @@ class AuthService extends BaseService
321 if (!$result) { 321 if (!$result) {
322 $this->message = $service->getMessage(); 322 $this->message = $service->getMessage();
323 } 323 }
  324 +
  325 + return $result;
324 } catch (\Exception $exception) { 326 } catch (\Exception $exception) {
325 $this->message = $exception->getMessage(); 327 $this->message = $exception->getMessage();
326 return false; 328 return false;
@@ -50,7 +50,7 @@ class CategoryService extends BaseService @@ -50,7 +50,7 @@ class CategoryService extends BaseService
50 $query->where("name", 'like', "%$q%"); 50 $query->where("name", 'like', "%$q%");
51 }) 51 })
52 ->where('status', 1) 52 ->where('status', 1)
53 - ->orderBy('sort') 53 + ->orderByDesc('sort')
54 ->groupBy('id') 54 ->groupBy('id')
55 ->latest() 55 ->latest()
56 ->get(); 56 ->get();
@@ -480,7 +480,70 @@ class WechatService extends BaseService @@ -480,7 +480,70 @@ class WechatService extends BaseService
480 */ 480 */
481 public function authLoginWithOfficialAccount($request) 481 public function authLoginWithOfficialAccount($request)
482 { 482 {
483 - record_log('wechat-auth', '微信公众号授权登录' , 'begin'); 483 + record_log('wechat-auth', '请求信息:' .json_encode($request->all()));
  484 +
  485 + $code = $request->input('code');
  486 + if (!$code) {
  487 + $this->message = '授权失败,code不能为空。';
  488 + return false;
  489 + }
  490 +// $config = [
  491 +// 'app_id' => config('wechat.official_account.app_id'),
  492 +// 'secret' => config('wechat.official_account.secret'),
  493 +// // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  494 +// 'oauth' => [
  495 +// 'scopes' => ['snsapi_userinfo'],
  496 +// 'callback' => '/oauth_callback',
  497 +// ],
  498 +// // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  499 +// 'response_type' => 'array',
  500 +// ];
  501 +
  502 + // 实例化公众号对象
  503 + $app = app('wechat.official_account');
  504 + // 获取用户授权信息
  505 + $user = $app->oauth->user();
  506 + // 获取用户信息
  507 + $user_info = $user->getOriginal();
  508 +
  509 + record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
  510 +
  511 + // 获取 openid
  512 + $openid = Arr::get($user_info, 'openid');
  513 +
  514 + // 获取授权用户
  515 + $user = null;
  516 + if ($openid) {
  517 + $user = User::where('openid', $openid)->first();
  518 + }
  519 +
  520 + // 未获取到创建新用户
  521 + if (!$user) {
  522 + $user = $this->createUser($request, $openid);
  523 + }
  524 +
  525 + // 设置微信头像和昵称
  526 + if ($user_info) {
  527 + if (Arr::get($user_info, 'headimgurl')) {
  528 + $user->avatar = Arr::get($user_info, 'headimgurl');
  529 + }
  530 + if (Arr::get($user_info, 'nickname')) {
  531 + $user->nickname = Arr::get($user_info, 'nickname');
  532 + }
  533 + $user->gender = Arr::get($user_info, 'sex');
  534 + $user->country = Arr::get($user_info, 'country');
  535 + $user->province = Arr::get($user_info, 'province');
  536 + $user->city = Arr::get($user_info, 'city');
  537 + }
  538 + $user->token = JWTAuth::fromuser($user);
  539 + $user->save();
  540 +
  541 + // 记录登陆日志
  542 + $this->recordLoginRecord($request, $user);
  543 + return $user;
  544 +
  545 +// dd($wechat_user);
  546 +
484 record_log('wechat-auth', '请求信息:' .json_encode($request->all())); 547 record_log('wechat-auth', '请求信息:' .json_encode($request->all()));
485 $code = $request->input('code'); 548 $code = $request->input('code');
486 if (!$code) { 549 if (!$code) {
@@ -489,6 +552,8 @@ class WechatService extends BaseService @@ -489,6 +552,8 @@ class WechatService extends BaseService
489 return false; 552 return false;
490 } 553 }
491 554
  555 +
  556 +
492 // 正在申请 557 // 正在申请
493 $user_info = session('wechat.oauth_user.default')->original; // 拿到授权用户资料 558 $user_info = session('wechat.oauth_user.default')->original; // 拿到授权用户资料
494 record_log('wechat-auth', '授权用户信息:' .json_encode($user_info)); 559 record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
@@ -545,7 +610,6 @@ class WechatService extends BaseService @@ -545,7 +610,6 @@ class WechatService extends BaseService
545 610
546 // 记录登陆日志 611 // 记录登陆日志
547 $this->recordLoginRecord($request, $user); 612 $this->recordLoginRecord($request, $user);
548 - record_log('wechat-auth', '微信公众号授权登录' , 'end');  
549 return $user; 613 return $user;
550 } 614 }
551 615
@@ -559,7 +623,7 @@ class WechatService extends BaseService @@ -559,7 +623,7 @@ class WechatService extends BaseService
559 */ 623 */
560 public function createUser($request, $openid, $mobile = '') 624 public function createUser($request, $openid, $mobile = '')
561 { 625 {
562 - $user = new User(); 626 + $user = $this->create($request);
563 // 创建用户 627 // 创建用户
564 $pid = $request->inviter_id ? : 0; // 邀请人id 628 $pid = $request->inviter_id ? : 0; // 邀请人id
565 $gid = 0; 629 $gid = 0;
@@ -582,7 +646,6 @@ class WechatService extends BaseService @@ -582,7 +646,6 @@ class WechatService extends BaseService
582 $user->nickname = '昵称' . $user->id; 646 $user->nickname = '昵称' . $user->id;
583 $user->avatar = 'avatar/'. rand(1, 20) .'.png'; 647 $user->avatar = 'avatar/'. rand(1, 20) .'.png';
584 648
585 -  
586 return $user; 649 return $user;
587 } 650 }
588 651
1 <!DOCTYPE html><html lang=zh-CN><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>chatApp</title><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)')) 1 <!DOCTYPE html><html lang=zh-CN><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><title>chatApp</title><script>var coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'))
2 - document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel=stylesheet href=./static/index.b0707a6a.css></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id=app></div><script src=./static/js/chunk-vendors.fc0b6f42.js></script><script src=./static/js/index.d3e1bce3.js></script></body></html> 2 + document.write('<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (coverSupport ? ', viewport-fit=cover' : '') + '" />')</script><link rel=stylesheet href=./static/index.b0707a6a.css></head><body><noscript><strong>Please enable JavaScript to continue.</strong></noscript><div id=app></div><script src=./static/js/chunk-vendors.fc0b6f42.js></script><script src=./static/js/index.6ce75ea3.js></script></body></html>
@@ -117,8 +117,15 @@ $api->version('v1', ['namespace' => 'App\Http\Api\V1\Controllers', 'middleware' @@ -117,8 +117,15 @@ $api->version('v1', ['namespace' => 'App\Http\Api\V1\Controllers', 'middleware'
117 }); 117 });
118 $api->post('sms-code', 'SmsController@store');// 发送验证码,用户注册:type1 118 $api->post('sms-code', 'SmsController@store');// 发送验证码,用户注册:type1
119 119
120 - $api->group(['middleware' => ['web','wechat.oauth']], function ($api) { 120 +// $api->group(['middleware' => ['web','wechat.oauth']], function ($api) {
  121 +// $api->post('auth', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
  122 +// $api->post('official-account', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
  123 +// $api->any('register', 'AuthController@register');// 注册
  124 +// });
  125 +//
  126 + $api->group(['middleware' => ['web']], function ($api) {
121 $api->any('auth', 'AuthController@wechatAuth')->name('user.authorize'); // 授权 127 $api->any('auth', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
  128 + $api->post('official-account', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
122 $api->any('register', 'AuthController@register');// 注册 129 $api->any('register', 'AuthController@register');// 注册
123 }); 130 });
124 131