AuthController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Http\Controllers\Api\Auth;
  3. use App\Models\Company;
  4. use App\Models\Member;
  5. use App\Models\MemberInfo;
  6. use App\Models\ThirdToken;
  7. use App\Models\WechatAuth;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Api\ApiBaseController;
  10. use Illuminate\Support\Facades\Validator;
  11. use App\Services\Auth\AuthService;
  12. use Illuminate\Support\Facades\Auth;
  13. use App\Services\Common\WechatService;
  14. class AuthController extends ApiBaseController
  15. {
  16. /**
  17. * @var AuthService
  18. */
  19. protected $authService;
  20. private $wechatService;
  21. /**
  22. * LoginController constructor.
  23. * @param AuthService $authService
  24. * @param WechatService $wechatService
  25. * @param SmsService $smsService
  26. * @param GeetestService $geetestService
  27. */
  28. public function __construct(
  29. AuthService $authService,
  30. WechatService $wechatService
  31. )
  32. {
  33. $this->authService = $authService;
  34. $this->wechatService = $wechatService;
  35. }
  36. public function loginByAccount(Request $request)
  37. {
  38. //基础信息的检查
  39. $rules = [
  40. 'account' => 'required',
  41. 'password' => 'required',
  42. ];
  43. $messages = [
  44. 'account.required' => '请输入用户名',
  45. 'password.required' => '请输入密码',
  46. ];
  47. $create_data = $request->all();
  48. $validator = Validator::make($create_data, $rules, $messages);
  49. if ($validator->fails()) {
  50. $msg = $validator->errors()->all();
  51. return response()->json(['status' => 0, 'msg' => $msg[0]]);
  52. } else {
  53. if (!$member = $this->authService->loginByAccount($request->account, $request->password, $request->autoLogin)) {
  54. return $this->sendErrorResponse("账号或密码错误", []);
  55. }
  56. if ($member->status == 0) {
  57. Auth::guard('api-member')->logout();
  58. return $this->sendErrorResponse("你的账号处于封禁状态, 请联系管理员");
  59. }
  60. return response()->json([
  61. 'message' => 'Successfully created user!',
  62. ], 201);
  63. }
  64. }
  65. /**
  66. * 微信登录中转
  67. * $state 自定义参数
  68. * $url 回调地地
  69. */
  70. public function wechatAuth(Request $request)
  71. {
  72. //获取参数
  73. $app_id = subsite_config('aix.system.oauth.wechat_official.app_id');
  74. $redirect_uri = urlencode(route('api.auth.wechat_auth_back'));
  75. $url = $request->input('url', '');
  76. if (empty($url)) {
  77. return response()->json(['status' => 0, 'msg' => '请调写回调地址']);
  78. }
  79. $state = $request->input('state', '');
  80. //存参数
  81. $auth = WechatAuth::create(['url' => $url, 'state' => $state]);
  82. //微信授权
  83. $wechat_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$auth['id']}#wechat_redirect";
  84. return redirect($wechat_url);
  85. }
  86. /**
  87. * 微信回调
  88. */
  89. public function wechatAuthBack(Request $request)
  90. {
  91. //微信登录
  92. $officialAccount = $this->wechatService->getOfficialAccount();
  93. $wechatUser = $officialAccount->oauth->user()->getOriginal();
  94. //回调
  95. $id = $request->input('state');
  96. $auth = WechatAuth::where('id', $id)->first();
  97. $wechatUser['state'] = $auth['state'];
  98. unset($wechatUser['privilege']);
  99. //循环拼接表单项
  100. $formItemString = '';
  101. foreach ($wechatUser as $key => $value) {
  102. $formItemString .= "<input name='{$key}' type='text' value='{$value}'/>";
  103. }
  104. //构造表单并跳转
  105. $content = <<<EOF
  106. <form style= 'display:none' name= 'submit_form' id= 'submit_form' action= '{$auth["url"]}' method= 'post' >
  107. { $formItemString }
  108. </form>
  109. <script type= "text/javascript" >
  110. document.submit_form.submit();
  111. </script>
  112. EOF;
  113. exit ($content);
  114. }
  115. public function test(Request $request)
  116. {
  117. dd($request->post());
  118. }
  119. /**
  120. * 根据token获取信息
  121. */
  122. public function getInfoByToken(Request $request)
  123. {
  124. $token = $request->header('token');
  125. if (empty($token)) {
  126. return response()->json([
  127. 'code' => 2,
  128. 'message' => '请输入token',
  129. ]);
  130. }
  131. $token_info = ThirdToken::where('token', $token)->first();
  132. if (empty($token_info)) {
  133. return response()->json([
  134. 'code' => 2,
  135. 'message' => 'token错误',
  136. ]);
  137. }
  138. $expire = strtotime($token_info['expire_at']);
  139. if ($expire < time()) {
  140. return response()->json([
  141. 'code' => 2,
  142. 'message' => 'token已过期',
  143. ]);
  144. }
  145. if ($token_info['id'] != 130) {
  146. $token_info->expire_at = date('Y-m-d H:i:s', time() + 7200);
  147. $token_info->save();
  148. }
  149. if ($token_info['type'] == 1) {
  150. $member = Member::where('id', $token_info['type_id'])->first();
  151. $member_info = MemberInfo::where('uid', $token_info['type_id'])->first();
  152. $info = [
  153. 'id' => $token_info['type_id'],
  154. 'type' => 1,
  155. 'realname' => $member_info['realname'],
  156. 'avatar' => $member['avatars'] ? upload_asset($member['avatars']) : '',
  157. 'sex' => $member_info['sex'],
  158. 'mobile' => $member['mobile'],
  159. 'email' => $member['email'],
  160. ];
  161. } elseif ($token_info['type'] == 2) {
  162. $company = Company::where('id', $token_info['type_id'])->first();
  163. $info = [
  164. 'id' => $token_info['type_id'],
  165. 'type' => 2,
  166. 'companyname' => $company['companyname'],
  167. 'logo' => $company['logo'] ? upload_asset($company['logo']) : '',
  168. 'mobile' => $company['mobile'],
  169. 'email' => $company['email'],
  170. 'address' => $company['address'],
  171. 'contact' => $company['contact'],
  172. ];
  173. }
  174. return response()->json([
  175. 'code' => 1,
  176. 'data' => $info,
  177. 'message' => '成功',
  178. ]);
  179. }
  180. }