123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/20
- * @time: 11:17
- */
- namespace app\controller\api;
- use app\common\ApiBaseController;
- use app\Request;
- use app\services\user\LoginServices;
- use app\validate\api\user\RegisterValidates;
- use Exception;
- use Psr\SimpleCache\InvalidArgumentException;
- use qiniu\services\CacheService;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\ValidateException;
- use think\facade\Config;
- class Login extends ApiBaseController
- {
- /**
- * LoginController constructor.
- * @param Request $request
- * @param LoginServices $services
- */
- public function __construct(Request $request, LoginServices $services)
- {
- parent::__construct($request);
- $this->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('请输入账号和密码');
- }
- if (!in_array($login_type, ['phone', 'account'])) {
- return $this->error('请选择登录方式');
- }
- 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('登录失败');
- }
- }
- }
|