ThirdloginController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace App\Http\Controllers\Web\Auth;
  3. use Aix\Sms\Contracts\Smser;
  4. use App\Http\Controllers\Web\WebBaseController;
  5. use App\Models\Company;
  6. use App\Models\Member;
  7. use App\Services\Auth\AuthService;
  8. use App\Services\Auth\RegisterService;
  9. use App\Services\Common\EmailService;
  10. use App\Services\Common\GeetestService;
  11. use App\Services\Common\MembersSetmealService;
  12. use App\Services\Common\QqService;
  13. use App\Services\Common\SmsService;
  14. use App\Services\Common\TaskService;
  15. use App\Services\Common\WechatService;
  16. use App\Validators\AuthValidatorRequest;
  17. use App\Validators\RegisterValidatorRequest;
  18. use Illuminate\Support\Facades\Cache;
  19. class ThirdloginController extends WebBaseController
  20. {
  21. /**
  22. * @var WechatService
  23. */
  24. private $wechatService;
  25. /**
  26. * @var AuthService
  27. */
  28. private $authService;
  29. /**
  30. * @var GeetestService
  31. */
  32. private $geetestService;
  33. /**
  34. * @var RegisterService
  35. */
  36. private $registerService;
  37. /**
  38. * @var QqService
  39. */
  40. private $qqService;
  41. /**
  42. * ThirdloginController constructor.
  43. * @param QqService $qqService
  44. * @param WechatService $wechatService
  45. * @param AuthService $authService
  46. * @param GeetestService $geetestService
  47. * @param RegisterService $registerService
  48. */
  49. public function __construct(QqService $qqService, WechatService $wechatService, AuthService $authService, GeetestService $geetestService, RegisterService $registerService)
  50. {
  51. $this->wechatService = $wechatService;
  52. $this->authService = $authService;
  53. $this->geetestService = $geetestService;
  54. $this->registerService = $registerService;
  55. $this->qqService = $qqService;
  56. }
  57. public function backgroundLogin($ticket)
  58. {
  59. if (!Cache::has($ticket)) {
  60. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  61. }
  62. $this->authService->logout();
  63. $user = Cache::pull($ticket);
  64. $this->authService->login($user, false);
  65. if (request()->has('redirect_url')) {
  66. return redirect(urldecode(request()->get('redirect_url')));
  67. }
  68. return redirect('/');
  69. }
  70. /**
  71. * pc端使用公众号来绑定账号
  72. */
  73. public function officialBind()
  74. {
  75. return view('app.auth.official_bind');
  76. }
  77. public function officialBindCheck($ticket)
  78. {
  79. if (request()->isXmlHttpRequest()) {
  80. if (!Cache::has($ticket.'_status')) {
  81. return $this->sendSuccessResponse(['is_login'=>0]);
  82. }
  83. return $this->sendSuccessResponse(['is_login'=>1]);
  84. }
  85. if (!Cache::has($ticket.'_status')) {
  86. return $this->showMessage("非法访问", $this->getReturnUrl(), true, session()->pull('redirect_name', "首页"));
  87. }
  88. $user=auth('web-member')->user()?auth('web-member')->user():
  89. (auth('web-company')->user()?auth('web-company')->user():false);
  90. if (false === $user) {
  91. return $this->showMessage("非法访问", $this->getReturnUrl(), true, session()->pull('redirect_name', "首页"));
  92. }
  93. Cache::pull($ticket.'_status');
  94. $wechatUser=Cache::pull($ticket);
  95. $this->authService->wechatRegister($wechatUser, $user, true, 3);
  96. return $this->showMessage("绑定成功", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
  97. }
  98. public function thirdLogin($login_type)
  99. {
  100. if ($login_type == 'wechat') {
  101. if (!session()->has('wechat_state') || !request()->has('state')) {
  102. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  103. }
  104. if (session('wechat_state') != request()->get('state')) {
  105. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  106. }
  107. if (session()->get('type', 'web') == 'mobile') {
  108. $officialAccount=$this->wechatService->getOfficialAccount();
  109. $wechatUser=$officialAccount->oauth->user()->getOriginal();
  110. } else {
  111. $openAccount=$this->wechatService->getOpenAccount();
  112. $wechatUser=$openAccount->oauth->user()->getOriginal();
  113. }
  114. return $this->bindResult($login_type, $wechatUser);
  115. }
  116. if ($login_type == "qq") {
  117. if (!session()->has('qq_state') || !request()->has('state')) {
  118. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  119. }
  120. if (session('qq_state') != request()->get('state')) {
  121. return $this->showMessage("非法访问, 请重试", config('app.url'), true, "首页");
  122. }
  123. $user=$this->qqService->getUser(request()->get('code'));
  124. return $this->bindResult($login_type, $user);
  125. }
  126. }
  127. public function checkLogin($ticket)
  128. {
  129. if (!Cache::has($ticket.'_status')) {
  130. return $this->sendSuccessResponse(['is_login'=>0]);
  131. }
  132. Cache::pull($ticket.'_status');
  133. $user=Cache::pull($ticket);
  134. $this->authService->login($user);
  135. return $this->sendSuccessResponse(['is_login'=>1]);
  136. }
  137. public function bindAccount($login_type)
  138. {
  139. if ($login_type == 'wechat') {
  140. return view('app.auth.thirdlogin_binding', ['type_name'=>'微信', 'type'=>'wechat']);
  141. }
  142. if ($login_type == 'qq') {
  143. return view('app.auth.thirdlogin_binding', ['type_name'=>'QQ', 'type'=>'qq']);
  144. }
  145. }
  146. public function bindAccountPost($login_type, $utype, AuthValidatorRequest $request)
  147. {
  148. if (!$user=$this->authService->checkUser($request->username, $request->password, $utype)) {
  149. return $this->sendErrorResponse("用户名或密码错误");
  150. }
  151. if (($utype == 1 && $user->user_status == 0) || ($utype == 2 && $user->status == 0)) {
  152. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  153. }
  154. if ($login_type == 'wechat') {
  155. if ($this->authService->wechatCheck(session('wechatUser'), $this->getThirdType($login_type))) {
  156. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  157. }
  158. $this->authService->wechatRegister(session('wechatUser'), $user, false, $this->getThirdType($login_type));
  159. $this->authService->login($user);
  160. return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/'))]);
  161. }
  162. if ($login_type == 'qq') {
  163. if ($this->authService->qqCheck(session('qqUser'), $this->getThirdType($login_type))) {
  164. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  165. }
  166. $this->authService->qqRegister(session('qqUser'), $user, false, $this->getThirdType($login_type));
  167. $this->authService->login($user);
  168. return $this->sendSuccessResponse(['redirect_url'=>url($request->session()->pull('url.intended', '/'))]);
  169. }
  170. return $this->sendErrorResponse("数据错误, 请刷新页面重试");
  171. }
  172. public function bindNewAccount($login_type)
  173. {
  174. $data=[
  175. 'type_name'=>$this->getTypeName($login_type),
  176. 'type'=>$login_type
  177. ];
  178. return view('app.auth.thirdlogin_binding_new', $data);
  179. }
  180. public function bindNewAccountPost($login_type, $utype, RegisterValidatorRequest $registerValidatorRequest, SmsService $smsService, MembersSetmealService $membersSetmealService, TaskService $taskService, EmailService $emailService)
  181. {
  182. if (!$this->geetestService->checkGeetest()) {
  183. return $this->sendErrorResponse("极验不通过,请重新验证");
  184. }
  185. if ($utype == 2) {
  186. if (!$smsService->checkAuthSms($registerValidatorRequest->mobile, Smser::TEMPLATE_AUTH_REGISTER, $registerValidatorRequest->mobile_vcode)) {
  187. return $this->sendErrorResponse("短信验证码不通过,请重新输入");
  188. }
  189. }
  190. if ($login_type == 'wechat') {
  191. if ($this->authService->wechatCheck(session('wechatUser'), $this->getThirdType($login_type))) {
  192. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  193. }
  194. }
  195. if ($login_type == 'qq') {
  196. if ($this->authService->qqCheck(session('qqUser'), $this->getThirdType($login_type))) {
  197. return $this->sendErrorResponse("你已绑定了其它账号, 请直接登录");
  198. }
  199. }
  200. if ($utype == 2) {
  201. $user=$this->registerService->registerPerson($registerValidatorRequest->all());
  202. $this->authService->login($user);
  203. } else {
  204. $user=$this->registerService->registerCompany($registerValidatorRequest->all());
  205. $this->authService->login($user);
  206. $membersSetmealService->addMemberSetmeal($user, 1);
  207. $taskService->doTask(17);
  208. $emailService->setAuthTag('company')
  209. ->setCallback('App\Services\Company\CompanyService', 'sendAuthEmailHook', [$registerValidatorRequest->email, $user])
  210. ->sendAuthMail($registerValidatorRequest->email, EmailService::TEMPLATE_VALIDATION);
  211. }
  212. if ($login_type == 'wechat') {
  213. $this->authService->wechatRegister(session('wechatUser'), $user, true, $this->getThirdType($login_type));
  214. }
  215. if ($login_type == 'qq') {
  216. $this->authService->qqRegister(session('qqUser'), $user, true, $this->getThirdType($login_type));
  217. }
  218. if ($utype == 2) {
  219. return $this->sendSuccessResponse(['url'=>route('person.index')]);
  220. } else {
  221. return $this->sendSuccessResponse(['url'=>route('register.send_email', ['type'=>1])]);
  222. }
  223. }
  224. /**
  225. * 显示信息页面
  226. * @param $message
  227. * @param $jump_url
  228. * @param bool $is_error
  229. * @param string $return_page_name
  230. * @param int $count_down
  231. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  232. */
  233. protected function showMobileMessage($message, $jump_url, $is_error = false, $return_page_name = "上一页", $count_down = 10)
  234. {
  235. return view('mobile.app.show_message', compact(['message', 'jump_url', 'is_error', 'return_page_name', 'count_down']));
  236. }
  237. protected function bindResult($type, $thirdUser)
  238. {
  239. if ($user=$this->authService->{$type.'Check'}($thirdUser, $this->getThirdType($type))) {
  240. if (session()->pull('is_bind', 0)) {
  241. if (session()->pull('type', 'web') == 'mobile') {
  242. return $this->showMobileMessage("该{$this->getTypeName($type)}账号已经绑定了其它账号, 请重试", session()->pull('redirect_url', config('app.url')), true, session()->pull('redirect_name', "首页"));
  243. }
  244. return $this->showMessage("该{$this->getTypeName($type)}账号已经绑定了其它账号, 请重试", session()->pull('redirect_url', config('app.url')), true, session()->pull('redirect_name', "首页"));
  245. }
  246. if (!empty($user->deleted_at)) {
  247. if (session()->pull('type', 'web') == 'mobile') {
  248. return $this->showMobileMessage("你的账号不存在,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
  249. }
  250. return $this->showMessage("你的账号不存在,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
  251. }
  252. if (($user instanceof Company && $user->user_status == 0) || ($user instanceof Member && $user->status == 0)) {
  253. if (session()->pull('type', 'web') == 'mobile') {
  254. return $this->showMobileMessage("你的账号处于封禁状态, 请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
  255. }
  256. return $this->showMessage("你的账号处于封禁状态,请联系管理员", get_subsite_domain(session('subsite_id')).'/'.session()->pull('type', ''), true, "首页");
  257. }
  258. $this->authService->login($user);
  259. return redirect($this->getReturnUrl());
  260. } else {
  261. if (($user=$this->getLoginUser()) && session()->pull('is_bind', 0)) {
  262. if (session()->pull('type', 'web') == 'mobile') {
  263. $this->authService->{$type.'Register'}($thirdUser, $user, false, $this->getThirdType($type));
  264. return $this->showMobileMessage("绑定成功!", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
  265. }
  266. $this->authService->{$type.'Register'}($thirdUser, $user, false, $this->getThirdType($type));
  267. return $this->showMessage("绑定成功", $this->getReturnUrl(), false, session()->pull('redirect_name', "首页"));
  268. }
  269. session([$type.'User' => $thirdUser]);
  270. $route_name=(session()->get('type')=='mobile')?'mobile.auth.thirdlogin.bind':'auth.thirdlogin.bind';
  271. return redirect(get_subsite_domain(session('subsite_id')).route($route_name, ['login_type'=>$type], false));
  272. }
  273. }
  274. private function getReturnUrl()
  275. {
  276. if (session()->has('redirect_url')) {
  277. return session()->pull('redirect_url');
  278. }
  279. if (session()->has('url.intended')) {
  280. return session()->pull('url.intended');
  281. }
  282. $type = session()->pull('type', '');
  283. $return_url=get_subsite_domain(session('subsite_id')).'/'.$type;
  284. if ($this->getLoginUser() instanceof Member) {
  285. $route_name=($type=='mobile')?'mobile.person.index':'person.index';
  286. $return_url= get_subsite_domain(session('subsite_id')).route($route_name, [], false);
  287. } elseif ($this->getLoginUser() instanceof Company) {
  288. $route_name=($type=='mobile')?'mobile.firm.index':'com.index';
  289. $return_url= get_subsite_domain(session('subsite_id')).route($route_name, [], false);
  290. }
  291. return $return_url;
  292. }
  293. protected function getTypeName($type)
  294. {
  295. switch ($type) {
  296. case "wechat":
  297. return "微信";
  298. case "qq":
  299. return "QQ";
  300. }
  301. }
  302. protected function getThirdType($type)
  303. {
  304. switch ($type) {
  305. case "wechat":
  306. if (session()->get('type', 'web') == 'mobile') {
  307. return 3;
  308. }
  309. return 2;
  310. case "qq":
  311. return 1;
  312. }
  313. }
  314. }