ThirdloginController.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace App\Http\Controllers\Mobile\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Http\Controllers\Mobile\MobileBaseController;
  5. use App\Services\Auth\AuthService;
  6. use App\Services\Auth\RegisterService;
  7. use App\Services\Common\EmailService;
  8. use App\Services\Common\GeetestService;
  9. use App\Services\Common\MembersSetmealService;
  10. use App\Services\Common\QqService;
  11. use App\Services\Common\SmsService;
  12. use App\Services\Common\TaskService;
  13. use App\Services\Common\WechatService;
  14. use App\Validators\AuthValidatorRequest;
  15. use App\Validators\RegisterValidatorRequest;
  16. use Illuminate\Support\Facades\Cache;
  17. class ThirdloginController extends MobileBaseController
  18. {
  19. /**
  20. * @var WechatService
  21. */
  22. private $wechatService;
  23. /**
  24. * @var AuthService
  25. */
  26. private $authService;
  27. /**
  28. * @var GeetestService
  29. */
  30. private $geetestService;
  31. /**
  32. * @var RegisterService
  33. */
  34. private $registerService;
  35. /**
  36. * @var QqService
  37. */
  38. private $qqService;
  39. /**
  40. * ThirdloginController constructor.
  41. * @param QqService $qqService
  42. * @param WechatService $wechatService
  43. * @param AuthService $authService
  44. * @param GeetestService $geetestService
  45. * @param RegisterService $registerService
  46. */
  47. public function __construct(QqService $qqService, WechatService $wechatService, AuthService $authService, GeetestService $geetestService, RegisterService $registerService)
  48. {
  49. $this->wechatService = $wechatService;
  50. $this->authService = $authService;
  51. $this->geetestService = $geetestService;
  52. $this->registerService = $registerService;
  53. $this->qqService = $qqService;
  54. }
  55. public function officialLogin()
  56. {
  57. $this->authService->logout();
  58. if (request()->has('redirect_url')) {
  59. session(['redirect_url'=>urldecode(request()->get('redirect_url'))]);
  60. }
  61. session(['wechat_state'=>time(), 'subsite_id'=>get_subsite_id(), 'type'=>'mobile', "is_bind"=>0]);
  62. $wechat_url="https://open.weixin.qq.com/connect/oauth2/authorize?"
  63. ."appid=".subsite_config('aix.system.oauth.wechat_official.app_id')
  64. ."&redirect_uri=".urlencode(config('app.url').route('auth.thirdlogin', ['login_type'=>'wechat'], false))
  65. ."&response_type=code&scope=snsapi_userinfo&state=".session('wechat_state')."#wechat_redirect";
  66. return redirect($wechat_url);
  67. }
  68. public function officialGetOpenid()
  69. {
  70. if (!session()->has('wechat_pay_state') || !request()->has('state')) {
  71. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  72. }
  73. if (session('wechat_pay_state') != request()->get('state')) {
  74. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  75. }
  76. $officialAccount=$this->wechatService->getPayOfficialAccount();
  77. $wechatUser=$officialAccount->oauth->user()->getOriginal();
  78. session(['wechat_pay_openid'=>$wechatUser['openid']]);
  79. return redirect(session()->pull('wechat_pay_redirect_url', '/mobile'));
  80. }
  81. public function officialLoginToWeb($ticket)
  82. {
  83. if (!Cache::has($ticket)) {
  84. return $this->showMessage("Ticket错误, 请重试", config('app.url'), true, "首页");
  85. }
  86. Cache::put($ticket.'_status', 1, 60);
  87. return view('mobile.app.official_login_success');
  88. }
  89. public function bindAccount($login_type)
  90. {
  91. $data=[
  92. 'type_name'=>$this->getTypeName($login_type),
  93. 'type'=>$login_type,
  94. 'wap_title'=>"绑定个人账号"
  95. ];
  96. return view('mobile.app.auth.thirdlogin_binding', $data);
  97. }
  98. public function bindCompanyAccount($login_type)
  99. {
  100. $data=[
  101. 'type_name'=>$this->getTypeName($login_type),
  102. 'type'=>$login_type,
  103. 'wap_title'=>"绑定企业账号"
  104. ];
  105. return view('mobile.app.auth.thirdlogin_binding_company', $data);
  106. }
  107. public function bindAccountPost($login_type, $utype, AuthValidatorRequest $request)
  108. {
  109. if (!$user=$this->authService->checkUser($request->username, $request->password, $utype)) {
  110. return $this->sendErrorResponse("用户名或密码错误");
  111. }
  112. if (($utype == 1 && $user->user_status == 0) || ($utype == 2 && $user->status == 0)) {
  113. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  114. }
  115. if ($login_type == 'wechat') {
  116. if ($this->authService->wechatCheck(session('wechatUser'), false)) {
  117. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  118. }
  119. $this->authService->wechatRegister(session('wechatUser'), $user, false, 3);
  120. $this->authService->login($user);
  121. return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/mobile'))]);
  122. }
  123. if ($login_type == 'qq') {
  124. if ($this->authService->qqCheck(session('qqUser'), false)) {
  125. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  126. }
  127. $this->authService->qqRegister(session('qqUser'), $user);
  128. $this->authService->login($user);
  129. return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/mobile'))]);
  130. }
  131. return $this->sendErrorResponse("数据错误, 请刷新页面重试");
  132. }
  133. public function bindNewAccount($login_type)
  134. {
  135. $data=[
  136. 'type_name'=>$this->getTypeName($login_type),
  137. 'type'=>$login_type,
  138. 'wap_title'=>"绑定个人账号"
  139. ];
  140. return view('mobile.app.auth.thirdlogin_binding_new', $data);
  141. }
  142. public function bindNewCompanyAccount($login_type)
  143. {
  144. $data=[
  145. 'type_name'=>$this->getTypeName($login_type),
  146. 'type'=>$login_type,
  147. 'wap_title'=>"绑定企业账号"
  148. ];
  149. return view('mobile.app.auth.thirdlogin_binding_new_company', $data);
  150. }
  151. public function bindNewAccountPost($login_type, $utype, RegisterValidatorRequest $registerValidatorRequest, SmsService $smsService, MembersSetmealService $membersSetmealService, TaskService $taskService, EmailService $emailService)
  152. {
  153. if (!$this->geetestService->checkGeetest()) {
  154. return $this->sendErrorResponse("极验不通过,请重新验证");
  155. }
  156. if ($utype == 2) {
  157. if (!$smsService->checkAuthSms($registerValidatorRequest->mobile, Smser::TEMPLATE_AUTH_REGISTER, $registerValidatorRequest->mobile_vcode)) {
  158. return $this->sendErrorResponse("短信验证码不通过,请重新输入");
  159. }
  160. }
  161. if ($login_type == 'wechat') {
  162. if ($this->authService->wechatCheck(session('wechatUser'), false)) {
  163. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  164. }
  165. }
  166. if ($login_type == 'qq') {
  167. if ($this->authService->qqCheck(session('qqUser'), false)) {
  168. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  169. }
  170. }
  171. if ($utype == 2) {
  172. $user=$this->registerService->registerPerson($registerValidatorRequest->all());
  173. $this->authService->login($user);
  174. } else {
  175. $user=$this->registerService->registerCompany($registerValidatorRequest->all());
  176. $this->authService->login($user);
  177. $membersSetmealService->addMemberSetmeal($user, 1);
  178. $taskService->doTask(17);
  179. $emailService->setAuthTag('company')
  180. ->setCallback('App\Services\Company\CompanyService', 'sendAuthEmailHook', [$registerValidatorRequest->email, $user])
  181. ->sendAuthMail($registerValidatorRequest->email, EmailService::TEMPLATE_VALIDATION);
  182. }
  183. if ($login_type == 'wechat') {
  184. $this->authService->wechatRegister(session('wechatUser'), $user, true, 3);
  185. }
  186. if ($login_type == 'qq') {
  187. $this->authService->qqRegister(session('qqUser'), $user, true);
  188. }
  189. if ($utype == 2) {
  190. return $this->sendSuccessResponse(['url'=>route('mobile.person.index')]);
  191. } else {
  192. return $this->sendSuccessResponse(['url'=>route('mobile.register.send_email', ['type'=>1])]);
  193. }
  194. }
  195. protected function getTypeName($type)
  196. {
  197. switch ($type) {
  198. case "wechat":
  199. return "微信";
  200. case "qq":
  201. return "QQ";
  202. }
  203. }
  204. }