service = $services; } /** * H5账号登陆 * @param Request $request * @return mixed * @throws DataNotFoundException|ModelNotFoundException|DbException */ public function login(Request $request) { [$account, $password, $spread_uid, $login_type] = $request->postMore([ 'account', 'password', 'spread_uid', ['login_type', 'account'] ], true); if (!$account || !$password) { return app('json')->fail('请输入账号和密码'); } validate(\app\validate\api\LoginValidate::class)->check(['account' => $account, 'pwd' => $password]); if ($login_type == 'phone') { if (!check_phone($account)) return app('json')->fail('请输入正确的手机号码'); } return app('json')->success('登录成功', $this->service->login($account, $login_type, $password, $spread_uid)); } /** * 退出登录 * @param Request $request * @return mixed * @throws InvalidArgumentException */ public function logout(Request $request) { $key = trim(ltrim($request->header(Config::get('cookie.token_name')), 'Bearer')); CacheService::redisHandler()->delete(md5($key)); return app('json')->success('成功'); } public function verifyCode() { $unique = password_hash(uniqid(true), PASSWORD_BCRYPT); CacheService::set('sms.key.' . $unique, 0, 300); $time = sys_config('verify_expire_time', 1); return app('json')->success(['key' => $unique, 'expire_time' => $time]); } public function captcha(Request $request) { ob_clean(); $rep = captcha(); $key = app('session')->get('captcha.key'); $uni = $request->get('key'); if ($uni) CacheService::set('sms.key.cap.' . $uni, $key, 300); return $rep; } /** * 验证验证码是否正确 * * @param $uni * @param string $code * @return bool * @throws InvalidArgumentException */ protected function checkCaptcha($uni, string $code): bool { $cacheName = 'sms.key.cap.' . $uni; if (!CacheService::has($cacheName)) { return false; } $key = CacheService::get($cacheName); $code = mb_strtolower($code, 'UTF-8'); $res = password_verify($code, $key); if ($res) { CacheService::delete($cacheName); } return $res; } /** * @return mixed */ public function ajcaptcha(Request $request) { $captchaType = $request->get('captchaType'); return app('json')->success(aj_captcha_create((string)$captchaType)); } /** * 一次验证 * @return mixed */ public function ajcheck(Request $request) { [$token, $pointJson, $captchaType] = $request->postMore([ ['token', ''], ['pointJson', ''], ['captchaType', ''], ], true); try { aj_captcha_check_one($captchaType, $token, $pointJson); return app('json')->success(); } catch (\Throwable $e) { return app('json')->fail(400336); } } /** * 验证码发送 * @param Request $request * @return mixed * @throws DataNotFoundException * @throws DbException * @throws InvalidArgumentException * @throws ModelNotFoundException */ public function verify(Request $request) { [$phone, $type, $key, $captchaType, $captchaVerification] = $request->postMore([ ['phone', 0], ['type', ''], ['key', ''], ['captchaType', ''], ['captchaVerification', ''], ], true); $keyName = 'sms.key.' . $key; $nowKey = 'sms.' . date('YmdHi'); if (!CacheService::has($keyName)) return $this->error('发送验证码失败,请刷新页面重新获取'); $total = 1; if (CacheService::has($nowKey)) { $total = CacheService::get($nowKey); if ($total > Config::get('sms.maxMinuteCount', 20)) return app('json')->success('触发分钟级流控:' . Config::get('sms.maxMinuteCount', 20)); } //二次验证 try { aj_captcha_check_two($captchaType, $captchaVerification); } catch (\Throwable $e) { return app('json')->fail($e->getError()); } try { validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]); } catch (ValidateException $e) { return app('json')->fail($e->getError()); } $time = sys_config('verify_expire_time', 1); $smsCode = $this->service->verify($phone, $type, $time, app()->request->ip()); if ($smsCode) { CacheService::set('code_' . $phone . '_' . $type, $smsCode, $time * 60); CacheService::set($nowKey, $total, 61); return app('json')->success('发送成功'); } else { return app('json')->fail('发送失败'); } } /** * H5注册新用户 * @param Request $request * @return mixed * @throws InvalidArgumentException */ public function register(Request $request) { [$phone, $captcha, $password, $nickname, $spread_uid] = $request->postMore([ ['phone', ''],//手机号 ['captcha', ''],//验证码 ['password', ''],//密码 ['nickname', ''],//密码 ['spread_uid', ''],//推荐人ID ], true); try { validate(RegisterValidates::class)->scene('register')->check([ 'phone' => $phone, 'captcha' => $captcha, 'password' => $password, ]); } catch (ValidateException $e) { return app('json')->fail($e->getError()); } check_sms_captcha($phone, 'register', $captcha); $user_type = $request->getFromType() ? $request->getFromType() : 'h5'; $registerStatus = $this->service->register($phone, $password, $spread_uid, $user_type, $nickname); if ($registerStatus) { return app('json')->success('注册成功'); } return app('json')->fail('注册失败'); } /** * 密码修改 * @param Request $request * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function reset(Request $request) { [$account, $captcha, $password] = $request->postMore([['phone', ''], ['captcha', ''], ['password', '']], true); check_sms_captcha($account, 'reset', $captcha); try { validate(RegisterValidates::class)->scene('register')->check([ 'phone' => $account, 'captcha' => $captcha, 'password' => $password, ]); } catch (ValidateException $e) { return app('json')->fail($e->getError()); } $resetStatus = $this->service->reset($account, $password); if ($resetStatus) { return app('json')->success('修改成功'); } return app('json')->fail('修改失败'); } /** * 交易密码修改 * @param Request $request * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function reset_trade_pwd(Request $request) { [$captcha, $password] = $request->postMore([['captcha', ''], ['password', '']], true); $account = $request->user()['phone'] ?? '0'; check_sms_captcha($account, 'reset', $captcha); if (!check_trade_password($password)) return app('json')->fail('交易密码为6位数字'); $resetStatus = $this->service->trade_reset($account, $password); if ($resetStatus) { return app('json')->success('修改成功'); } return app('json')->fail('修改失败'); } /** * 手机号登录 * @param Request $request * @return mixed * @throws Exception */ public function mobile(Request $request) { [$phone, $captcha, $spread_uid] = $request->postMore([['phone', ''], ['captcha', ''], ['spread_uid', 0]], true); //验证手机号 try { validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]); } catch (ValidateException $e) { return app('json')->fail($e->getError()); } check_sms_captcha($phone, 'mobile', $captcha); $user_type = $request->getFromType() ? $request->getFromType() : 'h5'; $token = $this->service->mobile($phone, $spread_uid, $user_type); if ($token) { return app('json')->success('登录成功', $token); } else { return app('json')->fail('登录失败'); } } }