| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace App\Http\Controllers\Web\Auth;
- use Aix\Sms\Contracts\Smser;
- use App\Http\Controllers\Web\WebBaseController;
- use App\Models\Company;
- use App\Models\Member;
- use App\Services\Auth\AuthService;
- use App\Services\Auth\RegisterService;
- use App\Services\Common\EmailService;
- use App\Services\Common\GeetestService;
- use App\Services\Common\MembersSetmealService;
- use App\Services\Common\QqService;
- use App\Services\Common\SmsService;
- use App\Services\Common\TaskService;
- use App\Services\Common\WechatService;
- use App\Validators\AuthValidatorRequest;
- use App\Validators\RegisterValidatorRequest;
- use Illuminate\Support\Facades\Cache;
- class ThirdloginController extends WebBaseController
- {
- /**
- * @var WechatService
- */
- private $wechatService;
- /**
- * @var AuthService
- */
- private $authService;
- /**
- * @var GeetestService
- */
- private $geetestService;
- /**
- * @var RegisterService
- */
- private $registerService;
- /**
- * @var QqService
- */
- private $qqService;
- /**
- * ThirdloginController constructor.
- * @param QqService $qqService
- * @param WechatService $wechatService
- * @param AuthService $authService
- * @param GeetestService $geetestService
- * @param RegisterService $registerService
- */
- public function __construct(QqService $qqService, WechatService $wechatService, AuthService $authService, GeetestService $geetestService, RegisterService $registerService)
- {
- $this->wechatService = $wechatService;
- $this->authService = $authService;
- $this->geetestService = $geetestService;
- $this->registerService = $registerService;
- $this->qqService = $qqService;
- }
- public function backgroundLogin($ticket)
- {
- if (!Cache::has($ticket)) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- $this->authService->logout();
- $user = Cache::pull($ticket);
- $this->authService->login($user, false);
- if (request()->has('redirect_url')) {
- return redirect(urldecode(request()->get('redirect_url')));
- }
- return redirect('/');
- }
- /**
- * pc端使用公众号来绑定账号
- */
- public function officialBind()
- {
- return view('app.auth.official_bind');
- }
- public function officialBindCheck($ticket)
- {
- if (request()->isXmlHttpRequest()) {
- if (!Cache::has($ticket.'_status')) {
- return $this->sendSuccessResponse(['is_login'=>0]);
- }
- return $this->sendSuccessResponse(['is_login'=>1]);
- }
- if (!Cache::has($ticket.'_status')) {
- return $this->showMessage("非法访问", $this->getReturnUrl(), true, session()->pull('redirect_name', "首页"));
- }
- $user=auth('web-member')->user()?auth('web-member')->user():
- (auth('web-company')->user()?auth('web-company')->user():false);
- if (false === $user) {
- return $this->showMessage("非法访问", $this->getReturnUrl(), true, session()->pull('redirect_name', "首页"));
- }
- Cache::pull($ticket.'_status');
- $wechatUser=Cache::pull($ticket);
- $this->authService->wechatRegister($wechatUser, $user, true, 3);
- return $this->showMessage("绑定成功", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
- }
- public function thirdLogin($login_type)
- {
- if ($login_type == 'wechat') {
- if (!session()->has('wechat_state') || !request()->has('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- if (session('wechat_state') != request()->get('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- if (session()->get('type', 'web') == 'mobile') {
- $officialAccount=$this->wechatService->getOfficialAccount();
- $wechatUser=$officialAccount->oauth->user()->getOriginal();
- } else {
- $openAccount=$this->wechatService->getOpenAccount();
- $wechatUser=$openAccount->oauth->user()->getOriginal();
- }
- return $this->bindResult($login_type, $wechatUser);
- }
- if ($login_type == "qq") {
- if (!session()->has('qq_state') || !request()->has('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- if (session('qq_state') != request()->get('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- $user=$this->qqService->getUser(request()->get('code'));
- return $this->bindResult($login_type, $user);
- }
- }
- public function checkLogin($ticket)
- {
- if (!Cache::has($ticket.'_status')) {
- return $this->sendSuccessResponse(['is_login'=>0]);
- }
- Cache::pull($ticket.'_status');
- $user=Cache::pull($ticket);
- $this->authService->login($user);
- return $this->sendSuccessResponse(['is_login'=>1]);
- }
- public function bindAccount($login_type)
- {
- if ($login_type == 'wechat') {
- return view('app.auth.thirdlogin_binding', ['type_name'=>'微信', 'type'=>'wechat']);
- }
- if ($login_type == 'qq') {
- return view('app.auth.thirdlogin_binding', ['type_name'=>'QQ', 'type'=>'qq']);
- }
- }
- public function bindAccountPost($login_type, $utype, AuthValidatorRequest $request)
- {
- if (!$user=$this->authService->checkUser($request->username, $request->password, $utype)) {
- return $this->sendErrorResponse("用户名或密码错误");
- }
- if (($utype == 1 && $user->user_status == 0) || ($utype == 2 && $user->status == 0)) {
- return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
- }
- if ($login_type == 'wechat') {
- if ($this->authService->wechatCheck(session('wechatUser'), $this->getThirdType($login_type))) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- $this->authService->wechatRegister(session('wechatUser'), $user, false, $this->getThirdType($login_type));
- $this->authService->login($user);
- return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/'))]);
- }
- if ($login_type == 'qq') {
- if ($this->authService->qqCheck(session('qqUser'), $this->getThirdType($login_type))) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- $this->authService->qqRegister(session('qqUser'), $user, false, $this->getThirdType($login_type));
- $this->authService->login($user);
- return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/'))]);
- }
- return $this->sendErrorResponse("数据错误, 请刷新页面重试");
- }
- public function bindNewAccount($login_type)
- {
- $data=[
- 'type_name'=>$this->getTypeName($login_type),
- 'type'=>$login_type
- ];
- return view('app.auth.thirdlogin_binding_new', $data);
- }
- public function bindNewAccountPost($login_type, $utype, RegisterValidatorRequest $registerValidatorRequest, SmsService $smsService, MembersSetmealService $membersSetmealService, TaskService $taskService, EmailService $emailService)
- {
- if (!$this->geetestService->checkGeetest()) {
- return $this->sendErrorResponse("极验不通过,请重新验证");
- }
- if ($utype == 2) {
- if (!$smsService->checkAuthSms($registerValidatorRequest->mobile, Smser::TEMPLATE_AUTH_REGISTER, $registerValidatorRequest->mobile_vcode)) {
- return $this->sendErrorResponse("短信验证码不通过,请重新输入");
- }
- }
- if ($login_type == 'wechat') {
- if ($this->authService->wechatCheck(session('wechatUser'), $this->getThirdType($login_type))) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- }
- if ($login_type == 'qq') {
- if ($this->authService->qqCheck(session('qqUser'), $this->getThirdType($login_type))) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- }
- if ($utype == 2) {
- $user=$this->registerService->registerPerson($registerValidatorRequest->all());
- $this->authService->login($user);
- } else {
- $user=$this->registerService->registerCompany($registerValidatorRequest->all());
- $this->authService->login($user);
- $membersSetmealService->addMemberSetmeal($user, 1);
- $taskService->doTask(17);
- $emailService->setAuthTag('company')
- ->setCallback('App\Services\Company\CompanyService', 'sendAuthEmailHook', [$registerValidatorRequest->email, $user])
- ->sendAuthMail($registerValidatorRequest->email, EmailService::TEMPLATE_VALIDATION);
- }
- if ($login_type == 'wechat') {
- $this->authService->wechatRegister(session('wechatUser'), $user, true, $this->getThirdType($login_type));
- }
- if ($login_type == 'qq') {
- $this->authService->qqRegister(session('qqUser'), $user, true, $this->getThirdType($login_type));
- }
- if ($utype == 2) {
- return $this->sendSuccessResponse(['url'=>route('person.index')]);
- } else {
- return $this->sendSuccessResponse(['url'=>route('register.send_email', ['type'=>1])]);
- }
- }
- /**
- * 显示信息页面
- * @param $message
- * @param $jump_url
- * @param bool $is_error
- * @param string $return_page_name
- * @param int $count_down
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- protected function showMobileMessage($message, $jump_url, $is_error = false, $return_page_name = "上一页", $count_down = 10)
- {
- return view('mobile.app.show_message', compact(['message', 'jump_url', 'is_error', 'return_page_name', 'count_down']));
- }
- protected function bindResult($type, $thirdUser)
- {
- if ($user=$this->authService->{$type.'Check'}($thirdUser, $this->getThirdType($type))) {
- if (session()->pull('is_bind', 0)) {
- if (session()->pull('type', 'web') == 'mobile') {
- return $this->showMobileMessage("该{$this->getTypeName($type)}账号已经绑定了其它账号, 请重试", session()->pull('redirect_url', config('app.url')), true, session()->pull('redirect_name', "首页"));
- }
- return $this->showMessage("该{$this->getTypeName($type)}账号已经绑定了其它账号, 请重试", session()->pull('redirect_url', config('app.url')), true, session()->pull('redirect_name', "首页"));
- }
- if (!empty($user->deleted_at)) {
- if (session()->pull('type', 'web') == 'mobile') {
- return $this->showMobileMessage("你的账号不存在,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
- }
- return $this->showMessage("你的账号不存在,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
- }
- if (($user instanceof Company && $user->user_status == 0) || ($user instanceof Member && $user->status == 0)) {
- if (session()->pull('type', 'web') == 'mobile') {
- return $this->showMobileMessage("你的账号处于封禁状态, 请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
- }
- return $this->showMessage("你的账号处于封禁状态,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
- }
- $this->authService->login($user);
- return redirect($this->getReturnUrl());
- } else {
- if (($user=$this->getLoginUser()) && session()->pull('is_bind', 0)) {
- if (session()->pull('type', 'web') == 'mobile') {
- $this->authService->{$type.'Register'}($thirdUser, $user, false, $this->getThirdType($type));
- return $this->showMobileMessage("绑定成功!", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
- }
- $this->authService->{$type.'Register'}($thirdUser, $user, false, $this->getThirdType($type));
- return $this->showMessage("绑定成功", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
- }
- session([$type.'User' => $thirdUser]);
- $route_name=(session()->get('type')=='mobile')?'mobile.auth.thirdlogin.bind':'auth.thirdlogin.bind';
- return redirect(get_subsite_domain(session('subsite_id')).route($route_name, ['login_type'=>$type], false));
- }
- }
- private function getReturnUrl()
- {
- if (session()->has('redirect_url')) {
- return session()->pull('redirect_url');
- }
- if (session()->has('url.intended')) {
- return session()->pull('url.intended');
- }
- $type = session()->pull('type', '');
- $return_url=get_subsite_domain(session('subsite_id')).'/'.$type;
- if ($this->getLoginUser() instanceof Member) {
- $route_name=($type=='mobile')?'mobile.person.index':'person.index';
- $return_url= get_subsite_domain(session('subsite_id')).route($route_name, [], false);
- } elseif ($this->getLoginUser() instanceof Company) {
- $route_name=($type=='mobile')?'mobile.firm.index':'com.index';
- $return_url= get_subsite_domain(session('subsite_id')).route($route_name, [], false);
- }
- return $return_url;
- }
- protected function getTypeName($type)
- {
- switch ($type) {
- case "wechat":
- return "微信";
- case "qq":
- return "QQ";
- }
- }
- protected function getThirdType($type)
- {
- switch ($type) {
- case "wechat":
- if (session()->get('type', 'web') == 'mobile') {
- return 3;
- }
- return 2;
- case "qq":
- return 1;
- }
- }
- }
|