...
|
...
|
@@ -391,6 +391,75 @@ class WechatService extends BaseService |
|
|
}
|
|
|
}
|
|
|
|
|
|
public function authLoginWithOfficialAccount1($request)
|
|
|
{
|
|
|
$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 = Factory::officialAccount($config);
|
|
|
$oauth = $app->oauth;
|
|
|
|
|
|
$targetUrl = urldecode($request->input('target_url'));
|
|
|
|
|
|
$response = [
|
|
|
'errcode' => 0,
|
|
|
'errmsg' => '操作成功',
|
|
|
'result' => [],
|
|
|
];
|
|
|
|
|
|
if (empty($targetUrl)) {
|
|
|
$this->message = '参数错误:target_url';
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 判断用户是否已经授权
|
|
|
if (empty(session('wechat.oauth_user'))) {
|
|
|
// 用户未授权,则开始授权
|
|
|
session(['wechat.oauth_target_url' => $targetUrl]);
|
|
|
return $oauth->redirect();
|
|
|
} else {
|
|
|
// 用户已经授权
|
|
|
$user = session('wechat.oauth_user');
|
|
|
$response['result'] = $user;
|
|
|
return $response;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function oauthCallback()
|
|
|
{
|
|
|
$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 = Factory::officialAccount($config);
|
|
|
$oauth = $app->oauth;
|
|
|
$user = $oauth->user()->getOriginal();
|
|
|
$targetUrl = session('wechat.oauth_target_url');
|
|
|
|
|
|
// 将用户信息保存到session中
|
|
|
session(['wechat.oauth_user' => $user->toArray()]);
|
|
|
|
|
|
return redirect($targetUrl);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 登录:公众号收取登录
|
|
|
* "openid" => "oemt65wMNi7XXo76PUIrsJuoJUeA"
|
...
|
...
|
@@ -411,9 +480,18 @@ 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不能为空。';
|
|
|
record_log('wechat-auth', '微信公众号授权登录' , 'end');
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 正在申请
|
|
|
$user_info = session('wechat.oauth_user.default')->original; // 拿到授权用户资料
|
|
|
|
|
|
record_log('wechat-auth', '授权用户信息:' .json_encode($user_info));
|
|
|
|
|
|
// $factory = app(WechatFactory::class);
|
|
|
// // 公众号好手气
|
...
|
...
|
@@ -440,18 +518,35 @@ class WechatService extends BaseService |
|
|
}
|
|
|
|
|
|
if ($user_info) {
|
|
|
$pid = $request->inviter_id ? : 0; // 邀请人id
|
|
|
$gid = 0;
|
|
|
if ($pid) {
|
|
|
$inviter = User::find($pid);
|
|
|
$gid = optional($inviter)->pid;
|
|
|
}
|
|
|
$user->avatar = Arr::get($user_info, 'avatarUrl');
|
|
|
$user->nickname = Arr::get($user_info, 'nickName');
|
|
|
$user->gender = Arr::get($user_info, 'gender');
|
|
|
$user->country = Arr::get($user_info, 'country');
|
|
|
$user->province = Arr::get($user_info, 'province');
|
|
|
$user->city = Arr::get($user_info, 'city');
|
|
|
$user->times = 10; // 新用户注册赠送 10 条
|
|
|
$user->invite_qr_code = 'mini_program_code/'.$user->id .'.png';
|
|
|
$user->pid = $pid;
|
|
|
$user->gid = $gid;
|
|
|
$user->openid = $openid;
|
|
|
$user->password = bcrypt(config('constants.PASSWORD'));
|
|
|
$user->role = Role::GENERAL;
|
|
|
$user->password = bcrypt(config('constants.PASSWORD'));
|
|
|
$user->registered_at = date('Y-m-d H:i:s');
|
|
|
$user->registered_ip = $request->ip();
|
|
|
}
|
|
|
$user->token = JWTAuth::fromuser($user);
|
|
|
$user->save();
|
|
|
|
|
|
// 记录登陆日志
|
|
|
$this->recordLoginRecord($request, $user);
|
|
|
record_log('wechat-auth', '微信公众号授权登录' , 'end');
|
|
|
return $user;
|
|
|
}
|
|
|
|
...
|
...
|
|