微信登录需要使用EasyWechat
登录后需要绑定手机号
控制器中引入类库
config 配置添加app_id、secret
use EasyWeChat\Factory;
//微信授权登陆
public function auth2wechat(Request $request)
{
$device_type = $this->deviceType;
$code = $request->input('code', '');
if (!$code) {
return $this->error('请输入code授权码');
}
if ($device_type == 'app') {
//开放平台APP应用
$config = config('wechat.open_platform.app');
$app = Factory::openPlatform($config);
} else {
if (isWechat()) {
//微信公众号
$config = config('wechat.official_account.default');
$app = Factory::officialAccount($config);
} else {
// 开放平台web应用
$config = config('wechat.open_platform.default');
$app = Factory::openPlatform($config);
}
}
$data = $app->oauth->userFromCode($code)->toArray();
$openid = $data['id'];
$unionid = $data['original']['unionid'];
if (!$openid && !$unionid) {
return $this->error('获取用户信息失败');
}
$user = User::where('weixin_unionid', $unionid)->orWhere('weixin_openid', $openid)->first();
$userData = [
'weixin_openid' => $openid,
'weixin_unionid' => $unionid,
'avatar' => $data['avatar'],
'weixin_nickname' => $data['name'],
'sex' => $data['original']['sex'],
'socials_type' => 'wechat',
];
if (!$user) {
return $this->error([
'code' => 422,
'msg' => '请绑定手机',
], $userData);
}
if (isWechat()) {
$user->wechat_openid = $openid;
$user->save();
}
$response = MemberService::handleSocials($user);
return $this->success('登录成功', $response);
}
Qq登录
//QQ授权登陆
public function auth2qq(Request $request)
{
$openid = $request->input('openid', '');
$nickname = $request->input('nickname', '');
$figureurl_qq = $request->input('figureurl_qq', '');
$unionid = $request->input('unionid', '');
$gender = $request->input('gender', '');
if (!$openid) {
return $this->error('缺失 openid');
}
if (!$unionid) {
return $this->error('缺失 unionid');
}
$data['socials_type'] = 'qq';
$data['avatar'] = $figureurl_qq;
$data['qq_nickname'] = $nickname;
$data['qq_unionid'] = $unionid;
$data['qq_openid'] = $openid;
$data['sex'] = $gender == '女' ? 2 : 1;
$user = User::where('qq_unionid', $unionid)->orWhere('qq_openid', $openid)->first();
if (!$user) {
return $this->error([
'code' => 422,
'msg' => '请绑定手机',
], $data);
}
$response = MemberService::handleSocials($user);
return $this->success('登录成功', $response);
}
Apple登录
//apple第三方登陆
public function auth2apple(Request $request)
{
// 授权的用户唯一标识
$user = $request->input('user', '');
// 邮箱
$email = $request->input('email', '');
// 用户信息
$fullName = $request->input('fullName', '');
if (!$user) {
return $this->error('缺少用户标识参数');
}
$data['socials_type'] = 'apple';
$data['apple_openid'] = $user;
$data['user_email'] = $email;
$data['user_nickname'] = $fullName;
$user = User::where('apple_openid', $user)->first();
if (!$user) {
return $this->error([
'code' => 422,
'msg' => '请绑定手机',
], $data);
}
$response = MemberService::handleSocials($user);
return $this->success('登录成功', $response);
}
第三方授权绑定手机号
//第三方授权登陆绑定手机号
public function mobileBind(Request $request)
{
$socials_type = $request->input('socials_type', '');
if (!$socials_type) {
return $this->error('缺少第三方授权登陆类型');
}
$mobile = $request->input('mobile', '');
$mobile_code = $request->input('mobile_code', '');
if (!preg_match('/^[1]([3-9])[0-9]{9}$/', $mobile)) {
return $this->error('请检查手机号位数和格式是否正确');
}
if (!VerificationCodeService::checkMobileCode($mobile, $mobile_code)) {
return $this->error('验证码不正确或已过期');
}
$user = User::where('mobile', $mobile)->first();
if ($socials_type == 'wechat') {
if ($user->weixin_unionid || $user->weixin_openid) {
return $this->error('此手机号已绑定其它微信');
}
$weixin_openid = $request->input('weixin_openid', '');
$weixin_unionid = $request->input('weixin_unionid', '');
$weixin_nickname = $request->input('weixin_nickname', '');
$avatar = $request->input('avatar', '');
$sex = $request->input('sex', '');
if (!$weixin_openid || $weixin_unionid) {
return $this->error('缺少微信用户信息');
}
$data = [
'user_login' => $mobile,
'user_nickname' => $weixin_nickname,
'weixin_openid' => $weixin_openid,
'weixin_unionid' => $weixin_unionid,
'weixin_nickname' => $weixin_nickname,
'avatar' => $avatar,
'sex' => $sex,
];
if (isWechat()) {
$data['wechat_openid'] = $weixin_openid;
}
} elseif ($socials_type == 'qq') {
if ($user->qq_unionid || $user->qq_openid) {
return $this->error('此手机号已绑定其它微信');
}
$qq_openid = $request->input('qq_openid', '');
$qq_unionid = $request->input('qq_unionid', '');
$qq_nickname = $request->input('qq_nickname', '');
$avatar = $request->input('avatar', '');
$sex = $request->input('sex', '');
if (!$qq_openid || $qq_unionid) {
return $this->error('缺少QQ用户信息');
}
$data = [
'user_login' => $mobile,
'user_nickname' => $qq_nickname,
'qq_openid' => $qq_openid,
'qq_unionid' => $qq_unionid,
'qq_nickname' => $qq_nickname,
'avatar' => $avatar,
'sex' => $sex,
];
} elseif ($socials_type == 'apple') {
if ($user->apple_openid) {
return $this->error('此手机号已绑定其它苹果账号');
}
$apple_openid = $request->input('apple_openid', '');
$user_email = $request->input('user_email', '');
$user_nickname = $request->input('user_nickname', '');
if (!$apple_openid) {
return $this->error('缺少Apple用户信息');
}
$data = [
'user_login' => $mobile,
'apple_openid' => $apple_openid,
'user_nickname' => $user_nickname,
'user_email' => $user_email,
];
}
$data = array_merge($data, [
'user_type' => 2,
'user_status' => 2,
'user_pass' => '',
'last_login_ip' => get_client_ip(0, true),
'last_login_time' => time(),
'create_time' => time(),
'update_time' => date('Y-m-d H:i:s', time()),
]);
$user = User::create($data);
if (!$user) {
return $this->error('创建用户失败');
}
$response = MemberService::handleSocials($user);
return $this->success('登录成功', $response);
}