AuthController.php 6.4 KB

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