123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <?php
- namespace App\Http\Controllers\Api\Base;
- use App\Http\Controllers\Controller;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Enums\ResponseCodeEnum;
- use App\Repositories\Models\Base\User;
- use App\Services\Base\UserService;
- use App\Services\Base\AuthService;
- use App\Support\Traits\LoginLimit;
- use Carbon\Carbon;
- use EasyWeChat\Factory;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Overtrue\EasySms\PhoneNumber;
- /**
- * 用户登录
- */
- class AuthController extends Controller
- {
- use LoginLimit;
- /**
- * @var AuthService
- */
- private $authService;
- /**
- * UserService
- * @var
- */
- private $userService;
- /**
- * AuthController constructor.
- *
- * @param AuthService $authService
- */
- public function __construct(AuthService $authService, UserService $userService)
- {
- parent::__construct();
- $this->authService = $authService;
- $this->userService = $userService;
- }
- /**
- * 微信小程序绑定登录
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \Illuminate\Validation\ValidationException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function wxLogin(Request $request)
- {
- $this->validateData($request, [
- 'code' => 'required|string',
- ], [
- 'code' => 'Code',
- ]);
- $code = $request->get('code');
- try {
- $app = Factory::officialAccount(config('wechat.official_account.default'));
- $user = $app->oauth->userFromCode($code);
- } catch (\Exception $exception) {
- $this->exception($exception);
- }
- $openId = $user->getId();
- $auth = $this->authService->handleCodeToAuth(config('wechat.official_account.default.app_id'), $openId);
- $session_key = $user->getAccessToken();
- if ($session_key) {
- Cache::put("cache:service:auth:session_key:api:" . $auth['id'], $session_key, Carbon::now()->addDay());
- Cache::put("cache:service:auth:userinfo:api:" . $auth['id'], [
- 'name' => $user->getName(),
- 'nickname' => $user->getNickname(),
- 'headimg' => $user->getAvatar(),
- 'data' => $user->getRaw()
- ], Carbon::now()->addDay());
- }
- if (!$auth['user_id']) {
- return $this->response->success([
- 'is_binding' => 0,
- 'auth_id' => $auth['id'],
- 'token' => null
- ]);
- }
- list($token, $user) = $this->userService->handleAuthLogin($auth);
- if (!$user) {
- return $this->response->success([
- 'is_binding' => 0,
- 'auth_id' => $auth['id'],
- 'token' => null
- ]);
- }
- if ($user->status == ModelStatusEnum::PAUSE) {
- return $this->response->success([
- 'is_binding' => 0,
- 'auth_id' => $auth['id'],
- 'token' => null
- ]);
- }
- $this->singleLoginSetToken(login_user_id(), $token, 'api');
- $token = 'Bearer ' . $token;
- $expires_in = auth()->factory()->getTTL() * 60;
- $is_binding = 1;
- return $this->response->success(compact('is_binding', 'token', 'expires_in'));
- }
- /**
- * 退出登录
- * @must
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function logout()
- {
- //解绑微信号
- $this->userService->handleUnbindWechat();
- auth('api')->logout();
- return $this->response->ok('操作成功');
- }
- /**
- * 登录用户信息
- * @must
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function me()
- {
- $user = $this->userService->handleMe();
- return $this->response->success($user);
- }
- /**
- * 刷新token
- * @must
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function refreshToken()
- {
- $token = $this->authService->handleRefreshToken();
- $this->singleLoginSetToken(login_user_id(), $token, 'api');
- $token = 'Bearer ' . $token;
- $expires_in = auth()->factory()->getTTL() * 60;
- return $this->response->success(compact('token', 'expires_in'));
- }
- /**
- * 发送验证码
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \Illuminate\Validation\ValidationException
- */
- public function sendLoginValidateCode(Request $request)
- {
- $this->validateData($request, [
- 'mobile' => 'required',
- 'area_code' => 'required|string',
- ], ['mobile' => '手机号', 'area_code' => '国际码']);
- $mobile = $request->get('mobile');
- $area_code = $request->get('area_code', '+86');
- switch ($area_code) {
- case "+886":
- //中国台湾
- $this->validateData($request, [
- 'mobile' => 'required|mobile_TW',
- ], ['mobile' => '手机号']);
- break;
- case "+853":
- //中国澳门
- $this->validateData($request, [
- 'mobile' => 'required|mobile_OM',
- ], ['mobile' => '手机号']);
- break;
- case "+852":
- //中国香港
- $this->validateData($request, [
- 'mobile' => 'required|mobile_XG',
- ], ['mobile' => '手机号']);
- break;
- case '+86':
- default:
- //大陆
- $this->validateData($request, [
- 'mobile' => 'required|mobile',
- ], ['mobile' => '手机号']);
- $area_code = "+86";
- break;
- }
- $if = $this->userService->handleCheckMobileIsExists($mobile, $area_code);
- if (!$if) {
- abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '找不到该用户');
- }
- $code = rand(1000, 9999);
- $time = 10;
- $number = new PhoneNumber($mobile, $area_code);
- try {
- app('easy_sms')->send($number, [
- 'template' => ($area_code == '+86') ? config('sms.template.verification_code') : config('sms.template.verification_code_gj'),
- 'data' => [
- 'number' => (string)$code,
- 'time' => $time,
- ]
- ]);
- } catch (\Exception $exception) {
- exception($exception->getException(config('sms.default.gateways')[0]));
- }
- $key = "controller:sendLoginValidateCode:mobile:{$area_code}:{$mobile}";
- Cache::put($key, $code, Carbon::now()->addMinutes($time));
- return $this->response->success(['status' => 1], '发送成功');
- }
- /**
- * 手机号登录
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \Illuminate\Validation\ValidationException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function mobileLogin(Request $request)
- {
- $this->validateData($request, [
- 'auth_id' => 'required|integer',
- 'mobile' => 'required',
- 'area_code' => 'required|string',
- 'code' => 'required|size:4',
- ], [
- 'mobile' => '手机号', 'code' => '验证码', 'area_code' => '国际码', 'auth_id' => '微信授权 id'
- ]);
- $auth_id = $request->get('auth_id', 0);
- $code = $request->get('code');
- $mobile = $request->get('mobile');
- //防止暴力破解
- $msg = $this->isCanLogin($request, $mobile, 'api');
- if ($msg) {
- return $this->response->fail($msg);
- }
- $this->storeLoginLog($request, $mobile, 'api');
- $area_code = $request->get('area_code', '+86');
- switch ($area_code) {
- case "+886":
- //中国台湾
- $this->validateData($request, [
- 'mobile' => 'required|mobile_TW',
- ], ['mobile' => '手机号']);
- break;
- case "+853":
- //中国澳门
- $this->validateData($request, [
- 'mobile' => 'required|mobile_OM',
- ], ['mobile' => '手机号']);
- break;
- case "+852":
- //中国香港
- $this->validateData($request, [
- 'mobile' => 'required|mobile_XG',
- ], ['mobile' => '手机号']);
- break;
- case '+86':
- default:
- //大陆
- $this->validateData($request, [
- 'mobile' => 'required|mobile',
- ], ['mobile' => '手机号']);
- $area_code = "+86";
- break;
- }
- $key = "controller:sendLoginValidateCode:mobile:{$area_code}:{$mobile}";
- $yun_code = Cache::get($key, false);
- if ((string)$yun_code !== (string)$code) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '验证码不对');
- list($token, $user) = $this->userService->handleMobileLogin($mobile, $area_code);
- $this->userService->handleBindWechat($auth_id, $user);
- Cache::forget($key);
- $this->singleLoginSetToken(login_user_id(), $token, 'api');
- $token = 'Bearer ' . $token;
- $expires_in = auth()->factory()->getTTL() * 60;
- return $this->response->success(compact('token', 'expires_in'));
- }
- /**
- * 测试登录
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \Illuminate\Validation\ValidationException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function testMobileLogin(Request $request)
- {
- $this->validateData($request, [
- 'mobile' => 'required|mobile',
- ], ['mobile' => '手机号']);
- $mobile = $request->get('mobile');
- if (config("app.env", 'production') === 'production') abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '非法授权');
- list($token, $user) = $this->userService->handleMobileLogin($mobile);
- $this->singleLoginSetToken(login_user_id(), $token, 'api');
- $token = 'Bearer ' . $token;
- $expires_in = auth()->factory()->getTTL() * 60;
- return $this->response->success(compact('token', 'expires_in'));
- }
- }
|