From 5eff598ff79c4e8c3c842080c88f6de175840995 Mon Sep 17 00:00:00 2001 From: weiz Date: Tue, 21 Nov 2023 16:32:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=89=8D=E5=8F=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B3=A8=E5=86=8C=E8=8E=B7=E5=8F=96=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/IndexController.php | 36 +++++++++++++++++++++- app/api/logic/LoginLogic.php | 37 ++++++++++++++++------- app/api/validate/LoginAccountValidate.php | 3 ++ app/api/validate/RegisterValidate.php | 14 ++++----- 4 files changed, 70 insertions(+), 20 deletions(-) diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index c9954803..dcaf3eea 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -16,6 +16,8 @@ namespace app\api\controller; use app\api\logic\IndexLogic; +use app\common\enum\notice\NoticeEnum; +use think\facade\Log; use think\response\Json; @@ -28,7 +30,7 @@ class IndexController extends BaseApiController { - public array $notNeedLogin = ['index', 'config', 'policy', 'decorate']; + public array $notNeedLogin = ['index', 'config', 'policy', 'decorate','code']; /** @@ -89,6 +91,38 @@ class IndexController extends BaseApiController $result = IndexLogic::getDecorate($id); return $this->data($result); } + + // 获取短信验证码 + public function code(): Json + { + //验证请求方式 + if(!$this->request->isPost()){ + return $this->fail('请求方式错误'); + } + //获取参数 + $params = $this->request->post(['phone','scene']); + if(empty($params['phone']) || empty($params['scene'])){ + return $this->fail('缺少必要参数'); + } + if(!in_array($params['scene'],[NoticeEnum::LOGIN_CAPTCHA,NoticeEnum::BIND_MOBILE_CAPTCHA,NoticeEnum::CHANGE_MOBILE_CAPTCHA,NoticeEnum::FIND_LOGIN_PASSWORD_CAPTCHA])){ + return $this->fail('短信场景错误'); + } + //发送短信 + try { + $result = event('Notice', [ + 'scene_id' => $params['scene'], + 'params' => [ + 'mobile' => $params['phone'], + 'code' => mt_rand(100000, 999999), + ] + ]); + return $this->success($result[0]); + }catch(\Exception $e){ + //记录日志 + Log::error($e->getMessage()); + return $this->fail($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/api/logic/LoginLogic.php b/app/api/logic/LoginLogic.php index 1a9263bf..9cc01c8a 100644 --- a/app/api/logic/LoginLogic.php +++ b/app/api/logic/LoginLogic.php @@ -16,6 +16,8 @@ namespace app\api\logic; use app\common\cache\WebScanLoginCache; use app\common\logic\BaseLogic; +use app\common\model\auth\Admin; +use app\common\model\auth\AdminRole; use app\api\service\{UserTokenService, WechatUserService}; use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum}; use app\common\service\{ @@ -44,23 +46,36 @@ class LoginLogic extends BaseLogic * @author 段誉 * @date 2022/9/7 15:37 */ - public static function register(array $params) + public static function register(array $params): bool { try { $userSn = User::createUserSn(); $passwordSalt = Config::get('project.unique_identification'); $password = create_password($params['password'], $passwordSalt); $avatar = ConfigService::get('default_image', 'user_avatar'); - - User::create([ - 'sn' => $userSn, - 'avatar' => $avatar, - 'nickname' => '用户' . $userSn, - 'account' => $params['account'], - 'password' => $password, - 'channel' => $params['channel'], - ]); - + + Db::transaction(function () use($userSn,$password,$avatar,$params) { + $user = User::create([ + 'sn' => $userSn, + 'avatar' => env('project.project_url').'/'.$avatar, + 'nickname' => '用户' . $userSn, + 'account' => $params['mobile'], + 'mobile' => $params['mobile'], + 'password' => $password, + 'channel' => 6, + ]); + $admin = Admin::create([ + 'user_id' => $user->id, + 'name' => '用户' . $userSn, + 'avatar' => env('project.project_url').'/'.$avatar, + 'account' => $params['mobile'], + 'password' => $password + ]); + AdminRole::create([ + 'admin_id' => $admin->id, + 'role_id' => 1 + ]); + }); return true; } catch (\Exception $e) { self::setError($e->getMessage()); diff --git a/app/api/validate/LoginAccountValidate.php b/app/api/validate/LoginAccountValidate.php index 7cbde983..c614522b 100644 --- a/app/api/validate/LoginAccountValidate.php +++ b/app/api/validate/LoginAccountValidate.php @@ -70,6 +70,9 @@ class LoginAccountValidate extends BaseValidate // 账号密码登录 if (LoginEnum::ACCOUNT_PASSWORD == $scene) { + if (!isset($data['account'])) { + return '请输入账号'; + } if (!isset($data['password'])) { return '请输入密码'; } diff --git a/app/api/validate/RegisterValidate.php b/app/api/validate/RegisterValidate.php index 39b4d5c0..803c2d1c 100644 --- a/app/api/validate/RegisterValidate.php +++ b/app/api/validate/RegisterValidate.php @@ -14,7 +14,9 @@ namespace app\api\validate; +use app\common\enum\notice\NoticeEnum; use app\common\model\user\User; +use app\common\service\sms\SmsDriver; use app\common\validate\BaseValidate; /** @@ -31,23 +33,19 @@ class RegisterValidate extends BaseValidate ]; protected $rule = [ - 'channel' => 'require', - 'account' => 'require|length:3,12|unique:' . User::class . '|regex:register', + 'mobile' => 'require|mobile|unique:' . User::class, 'password' => 'require|length:6,20|regex:password', 'password_confirm' => 'require|confirm' ]; protected $message = [ - 'channel.require' => '注册来源参数缺失', - 'account.require' => '请输入账号', - 'account.regex' => '账号须为字母数字组合', - 'account.length' => '账号须为3-12位之间', - 'account.unique' => '账号已存在', + 'mobile.require' => '请输入手机号', + 'mobile.mobile' => '手机号格式错误', + 'mobile.unique' => '手机号已存在', 'password.require' => '请输入密码', 'password.length' => '密码须在6-25位之间', 'password.regex' => '密码须为数字,字母或符号组合', 'password_confirm.require' => '请确认密码', 'password_confirm.confirm' => '两次输入的密码不一致' ]; - } \ No newline at end of file