作者 Richer

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

... ... @@ -36,7 +36,7 @@ trait CategoryGrid
public function setGridOrder()
{
if (!request('_sort')) {
$this->grid->model()->orderBy('sort')->latest();
$this->grid->model()->orderByDesc('sort')->latest();
} else {
$this->setGridOrderSpecial();
}
... ...
... ... @@ -23,6 +23,7 @@ use App\Http\Requests\RegisterRequest;
use App\Http\Requests\ResetPasswordRequest;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
/**
* Class AuthController
... ... @@ -101,10 +102,15 @@ class AuthController extends BaseController
*/
public function wechatAuth(Request $request)
{
record_log('wechat-auth', '微信公众号授权登录' , 'begin');
$result = $this->service->wechatAuth($request);
if ($result) {
// Log::info($result);
record_log('wechat-auth', '返回信息:' .json_encode($result));
record_log('wechat-auth', '微信公众号授权登录' , 'end');
return $this->success($result, __('login') . ' ' . __('succeeded'), 0, $result->token);
} else {
record_log('wechat-auth', '微信公众号授权登录' , 'end');
return $this->error($this->service->getMessage(), 2006);
}
}
... ...
... ... @@ -78,6 +78,9 @@ class VerifyJWTToken extends BaseMiddleware
try {
// add by Richer 于 2021年6月16日11:19:49 获取用户的角色,根据角色来判断不同的用户表
$role = $this->auth->parseToken()->getClaim('role');
if ($request->test == 1) {
dump($role);
}
// 通过令牌验证用户。
if ($role === 'admin') {
$user = \Auth::guard('api_admin')->authenticate();
... ... @@ -85,6 +88,10 @@ class VerifyJWTToken extends BaseMiddleware
$user = $this->auth->parseToken()->authenticate();
}
if ($request->test == 1) {
dump($user);
}
if (!$user) {
//获取到用户数据,并赋值给$user
return response()->json([
... ...
... ... @@ -82,7 +82,8 @@ class Category extends BaseModel
*/
public function children(): HasMany
{
return $this->hasMany(get_class($this), 'pid', 'id');
return $this->hasMany(get_class($this), 'pid', 'id')->where('status', 1)
;
}
/**
... ...
... ... @@ -42,7 +42,7 @@ class AppServiceProvider extends ServiceProvider
});
API::error(function (\Exception $exception) {
dd($exception);
// dd($exception);
return response()->json(['code'=>2004,'msg'=> $exception->getMessage(),'data'=> null ], 200);
});
... ...
... ... @@ -321,6 +321,8 @@ class AuthService extends BaseService
if (!$result) {
$this->message = $service->getMessage();
}
return $result;
} catch (\Exception $exception) {
$this->message = $exception->getMessage();
return false;
... ...
... ... @@ -50,7 +50,7 @@ class CategoryService extends BaseService
$query->where("name", 'like', "%$q%");
})
->where('status', 1)
->orderBy('sort')
->orderByDesc('sort')
->groupBy('id')
->latest()
->get();
... ...
... ... @@ -480,7 +480,70 @@ class WechatService extends BaseService
*/
public function authLoginWithOfficialAccount($request)
{
record_log('wechat-auth', '微信公众号授权登录' , 'begin');
record_log('wechat-auth', '请求信息:' .json_encode($request->all()));
$code = $request->input('code');
if (!$code) {
$this->message = '授权失败,code不能为空。';
return false;
}
// $config = [
// 'app_id' => config('wechat.official_account.app_id'),
// 'secret' => config('wechat.official_account.secret'),
// // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
// 'oauth' => [
// 'scopes' => ['snsapi_userinfo'],
// 'callback' => '/oauth_callback',
// ],
// // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
// 'response_type' => 'array',
// ];
// 实例化公众号对象
$app = app('wechat.official_account');
// 获取用户授权信息
$user = $app->oauth->user();
// 获取用户信息
$user_info = $user->getOriginal();
record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
// 获取 openid
$openid = Arr::get($user_info, 'openid');
// 获取授权用户
$user = null;
if ($openid) {
$user = User::where('openid', $openid)->first();
}
// 未获取到创建新用户
if (!$user) {
$user = $this->createUser($request, $openid);
}
// 设置微信头像和昵称
if ($user_info) {
if (Arr::get($user_info, 'headimgurl')) {
$user->avatar = Arr::get($user_info, 'headimgurl');
}
if (Arr::get($user_info, 'nickname')) {
$user->nickname = Arr::get($user_info, 'nickname');
}
$user->gender = Arr::get($user_info, 'sex');
$user->country = Arr::get($user_info, 'country');
$user->province = Arr::get($user_info, 'province');
$user->city = Arr::get($user_info, 'city');
}
$user->token = JWTAuth::fromuser($user);
$user->save();
// 记录登陆日志
$this->recordLoginRecord($request, $user);
return $user;
// dd($wechat_user);
record_log('wechat-auth', '请求信息:' .json_encode($request->all()));
$code = $request->input('code');
if (!$code) {
... ... @@ -489,6 +552,8 @@ class WechatService extends BaseService
return false;
}
// 正在申请
$user_info = session('wechat.oauth_user.default')->original; // 拿到授权用户资料
record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
... ... @@ -540,12 +605,11 @@ class WechatService extends BaseService
$user->province = Arr::get($user_info, 'province');
$user->city = Arr::get($user_info, 'city');
}
$user->token = JWTAuth::fromuser($user);
$user->token = JWTAuth::fromuser($user);
$user->save();
// 记录登陆日志
$this->recordLoginRecord($request, $user);
record_log('wechat-auth', '微信公众号授权登录' , 'end');
return $user;
}
... ... @@ -559,7 +623,7 @@ class WechatService extends BaseService
*/
public function createUser($request, $openid, $mobile = '')
{
$user = new User();
$user = $this->create($request);
// 创建用户
$pid = $request->inviter_id ? : 0; // 邀请人id
$gid = 0;
... ... @@ -582,7 +646,6 @@ class WechatService extends BaseService
$user->nickname = '昵称' . $user->id;
$user->avatar = 'avatar/'. rand(1, 20) .'.png';
return $user;
}
... ...
<!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)'))
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>
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'
});
$api->post('sms-code', 'SmsController@store');// 发送验证码,用户注册:type1
$api->group(['middleware' => ['web','wechat.oauth']], function ($api) {
// $api->group(['middleware' => ['web','wechat.oauth']], function ($api) {
// $api->post('auth', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
// $api->post('official-account', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
// $api->any('register', 'AuthController@register');// 注册
// });
//
$api->group(['middleware' => ['web']], function ($api) {
$api->any('auth', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
$api->post('official-account', 'AuthController@wechatAuth')->name('user.authorize'); // 授权
$api->any('register', 'AuthController@register');// 注册
});
... ...