123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- namespace App\Http\Controllers\Api\Base;
- use App\Http\Controllers\Controller;
- use App\Repositories\Enums\ResponseCodeEnum;
- 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\Arr;
- use Illuminate\Support\Facades\Cache;
- /**
- * 用户登录
- */
- class AuthController extends Controller
- {
- use LoginLimit;
- /**
- * @var AuthService
- */
- private $authService;
- /**
- * AdminService
- * @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 \Illuminate\Validation\ValidationException
- */
- public function accountLogin(Request $request)
- {
- $this->validate($request, [
- 'username' => 'required',
- 'password' => 'required|min:6'
- ], [], [
- 'username' => '账号',
- 'password' => '密码',
- ]);
- $credentials = $request->only(['username', 'password']);
- // if (is_mobile()) {
- // $this->validate($request, [
- // 'type' => 'required',
- // ], [], ['type' => '角色类型']);
- // $credentials['type'] = $request->get('type');
- // }
- $username = $request->get('username');
- $msg = $this->isCanLogin($request, $username, 'api');
- if ($msg) {
- return $this->response->fail($msg);
- }
- $token = $this->userService->handleAccountLogin($credentials, $request);
- $this->clearLoginLogs($request, $username, 'api');
- if ($token) {
- $this->clearLoginLogs($request, $username, 'api');
- $expires_in = auth()->factory()->getTTL() * 60;
- $this->singleLoginSetToken(login_user_id(), $token, 'api');
- $token = 'Bearer ' . $token;
- return $this->response->success(compact('token', 'expires_in'));
- }
- $this->storeLoginLog($request, $username, 'api');
- return $this->response->fail('账号或者密码错误!');
- }
- /**
- * 微信小程序绑定登录
- * @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 miniProgramLogin(Request $request)
- {
- $this->validate($request, [
- 'code' => 'required|string',
- 'appid' => 'required|string',
- 'phone_detail' => 'sometimes'
- ], [], [
- 'code' => 'Code',
- 'appid' => 'AppId',
- 'phone_detail' => '手机信息',
- ]);
- $auth = $this->authService->handleMiniProgramLogin($request);
- $token = $this->userService->handleAuthLogin($auth);
- $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 \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \Illuminate\Validation\ValidationException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function bindWechat(Request $request)
- {
- $this->validate($request, [
- 'code' => 'required|string',
- 'appid' => 'required|string',
- 'type' => 'required|in:3,4,5',
- 'phone_detail' => 'sometimes'
- ], [], [
- 'code' => 'Code',
- 'appid' => 'AppId',
- 'phone_detail' => '手机信息',
- ]);
- $auth = $this->authService->handleMiniProgramLogin($request);
- $this->userService->handleBindWechat($auth);
- return $this->response->ok('绑定成功');
- }
- /**
- * 绑定微信号
- * @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 unbindWechat()
- {
- $this->userService->handleUnbindWechat();
- return $this->response->ok('绑定成功');
- }
- /**
- * 退出登录
- * @must
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function logout()
- {
- 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'));
- }
- /**
- * 微信小程序绑定用户手机号
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\Resource|void
- * Author: Mead
- */
- public function miniBindMobile(Request $request)
- {
- $this->validate($request, [
- 'iv' => 'required|string',
- 'encryptedData' => 'required|string',
- ]);
- //微信解析手机号
- $user = login_user();
- $iv = $request->get('iv');
- $encryptedData = $request->get('encryptedData');
- $session = Cache::get("cache:service:auth:session_key:api:" . $user['wechat_auth_id']);
- if (!$session) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请重新登录');
- $miniConfig = wechat_mini_config(0);
- $miniProgram = Factory::miniProgram($miniConfig);
- try {
- $decryptedData = $miniProgram->encryptor->decryptData($session, $iv, $encryptedData);
- } catch (\Exception $exception) {
- exception($exception);
- }
- if (!array_key_exists('purePhoneNumber', $decryptedData)) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '解密数据不对');
- $mobile = $decryptedData['purePhoneNumber'];
- $this->userService->handleBindMobile($user['id'], $mobile);
- return $this->response->ok('绑定成功');
- }
- /**
- * 修改密码
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- * @throws \Illuminate\Validation\ValidationException
- * @throws \Prettus\Validator\Exceptions\ValidatorException
- */
- public function updatePassword(Request $request)
- {
- $data = $this->validateData($request, [
- 'password' => 'required|confirmed|min:6'
- ], [
- 'password' => '密码',
- ]);
- $password = $data['password'];
- if (!check_password($password)) {
- return $this->response->fail('密码太简单', ResponseCodeEnum::SERVICE_OPERATION_ERROR);
- }
- $this->userService->handleMeResetPassword($password);
- return $this->response->ok('修改成功');
- }
- /**
- * 更新个人信息
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function updateInfo(Request $request)
- {
- $fields = $this->validateData($request, [
- 'fields' => 'required|array',
- ], [
- 'fields' => '字段表',
- ])['fields'];
- $da = [
- 'name' => [
- 'rule' => 'required|string',
- 'name' => '姓名',
- 'key' => 'name',
- ],
- 'sex' => [
- 'rule' => 'required|in:1,2',
- 'name' => '性别',
- 'key' => 'sex',
- ],
- 'headimg' => [
- 'rule' => 'required|string',
- 'name' => '头像',
- 'key' => 'headimg',
- ],
- 'email' => [
- 'rule' => 'required|mail',
- 'name' => '邮箱',
- 'key' => 'email',
- ],
- 'extra_fields.wechat_no' => [
- 'rule' => 'required|string',
- 'name' => '微信号',
- 'key' => 'extra_fields.wechat_no',
- ],
- ];
- $rules = Arr::only($da, $fields);
- $data = $this->validateData($request, Arr::pluck($rules, 'rule', 'key'), Arr::pluck($rules, 'name', 'key'));
- $this->userService->handleMeUpdate($data);
- return $this->response->ok('更新成功');
- }
- /**
- * 更新个人信息[微信]
- * @must
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
- */
- public function miniUpdateInfo(Request $request)
- {
- $this->validate($request, [
- 'iv' => 'required',
- 'encryptedData' => 'required',
- ]);
- //微信解析手机号
- $user = login_user();
- $iv = $request->get('iv');
- $encryptedData = $request->get('encryptedData');
- $session = Cache::get("cache:service:auth:session_key:" . $user['wechat_auth_id']);
- if ($session) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '请重新登录');
- $miniConfig = wechat_mini_config(0);
- $miniProgram = Factory::miniProgram($miniConfig);
- try {
- $decryptedData = $miniProgram->encryptor->decryptData($session, $iv, $encryptedData);
- } catch (\Exception $exception) {
- exception($exception);
- }
- return $this->response->ok('更新成功');
- }
- /**
- * 发送验证码
- * @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|mobile'
- ], ['mobile' => '手机号']);
- $mobile = $request->get('mobile');
- $day = date('Y-m-d');
- $nums = Cache::get("controller:Api:AuthController:sendLoginValidateCode:{$mobile}:{$day}", 0);
- if ($nums > 3) {
- return $this->response->fail('你今天请求的次数太多了,请明天再试。');
- }
- Cache::increment("controller:Api:AuthController:sendLoginValidateCode:{$mobile}:{$day}", 1);
- $code = rand(100000, 999999);
- try {
- // app('easy_sms')->send($mobile, [
- // 'template' => config('sms.template.verification_code'),
- // 'data' => [
- // 'number' => (string)$code
- // ]
- // ]);
- } catch (\Exception $exception) {
- exception($exception->getException(config('sms.default.gateways')[0]));
- }
- Cache::put('controller:sendLoginValidateCode:mobile:' . $mobile, $code, Carbon::now()->addMinutes(5));
- return $this->response->ok('发送成功' . $code);
- }
- /**
- * 手机号登录
- * @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, [
- 'mobile' => 'required|mobile',
- 'code' => 'required|size:6',
- ], ['mobile' => '手机号', 'code' => '验证码']);
- $mobile = $request->get('mobile');
- $code = $request->get('code');
- $yun_code = Cache::get('controller:sendLoginValidateCode:mobile:' . $mobile, false);
- // if ($yun_code !== $code) abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '验证码不对');
- $token = $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'));
- }
- }
|