| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace App\Http\Controllers\Mobile\Auth;
- use Aix\Sms\Contracts\Smser;
- use App\Http\Controllers\Mobile\MobileBaseController;
- 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 MobileBaseController
- {
- /**
- * @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 officialLogin()
- {
- $this->authService->logout();
- if (request()->has('redirect_url')) {
- session(['redirect_url'=>urldecode(request()->get('redirect_url'))]);
- }
- session(['wechat_state'=>time(), 'subsite_id'=>get_subsite_id(), 'type'=>'mobile', "is_bind"=>0]);
- $wechat_url="https://open.weixin.qq.com/connect/oauth2/authorize?"
- ."appid=".subsite_config('aix.system.oauth.wechat_official.app_id')
- ."&redirect_uri=".urlencode(config('app.url').route('auth.thirdlogin', ['login_type'=>'wechat'], false))
- ."&response_type=code&scope=snsapi_userinfo&state=".session('wechat_state')."#wechat_redirect";
- return redirect($wechat_url);
- }
- public function officialGetOpenid()
- {
- if (!session()->has('wechat_pay_state') || !request()->has('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- if (session('wechat_pay_state') != request()->get('state')) {
- return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
- }
- $officialAccount=$this->wechatService->getPayOfficialAccount();
- $wechatUser=$officialAccount->oauth->user()->getOriginal();
- session(['wechat_pay_openid'=>$wechatUser['openid']]);
- return redirect(session()->pull('wechat_pay_redirect_url', '/mobile'));
- }
- public function officialLoginToWeb($ticket)
- {
- if (!Cache::has($ticket)) {
- return $this->showMessage("Ticket错误, 请重试", config('app.url'), true, "首页");
- }
- Cache::put($ticket.'_status', 1, 60);
- return view('mobile.app.official_login_success');
- }
- public function bindAccount($login_type)
- {
- $data=[
- 'type_name'=>$this->getTypeName($login_type),
- 'type'=>$login_type,
- 'wap_title'=>"绑定个人账号"
- ];
- return view('mobile.app.auth.thirdlogin_binding', $data);
- }
- public function bindCompanyAccount($login_type)
- {
- $data=[
- 'type_name'=>$this->getTypeName($login_type),
- 'type'=>$login_type,
- 'wap_title'=>"绑定企业账号"
- ];
- return view('mobile.app.auth.thirdlogin_binding_company', $data);
- }
- 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'), false)) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- $this->authService->wechatRegister(session('wechatUser'), $user, false, 3);
- $this->authService->login($user);
- return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/mobile'))]);
- }
- if ($login_type == 'qq') {
- if ($this->authService->qqCheck(session('qqUser'), false)) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- $this->authService->qqRegister(session('qqUser'), $user);
- $this->authService->login($user);
- return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/mobile'))]);
- }
- return $this->sendErrorResponse("数据错误, 请刷新页面重试");
- }
- public function bindNewAccount($login_type)
- {
- $data=[
- 'type_name'=>$this->getTypeName($login_type),
- 'type'=>$login_type,
- 'wap_title'=>"绑定个人账号"
- ];
- return view('mobile.app.auth.thirdlogin_binding_new', $data);
- }
- public function bindNewCompanyAccount($login_type)
- {
- $data=[
- 'type_name'=>$this->getTypeName($login_type),
- 'type'=>$login_type,
- 'wap_title'=>"绑定企业账号"
- ];
- return view('mobile.app.auth.thirdlogin_binding_new_company', $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'), false)) {
- return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
- }
- }
- if ($login_type == 'qq') {
- if ($this->authService->qqCheck(session('qqUser'), false)) {
- 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, 3);
- }
- if ($login_type == 'qq') {
- $this->authService->qqRegister(session('qqUser'), $user, true);
- }
- if ($utype == 2) {
- return $this->sendSuccessResponse(['url'=>route('mobile.person.index')]);
- } else {
- return $this->sendSuccessResponse(['url'=>route('mobile.register.send_email', ['type'=>1])]);
- }
- }
- protected function getTypeName($type)
- {
- switch ($type) {
- case "wechat":
- return "微信";
- case "qq":
- return "QQ";
- }
- }
- }
|