Merge pull request '新增前台登录注册获取验证码接口' (#5) from zhangwei into dev

Reviewed-on: #5
This commit is contained in:
weiz 2023-11-21 16:33:43 +08:00
commit f5e4a5eafa
4 changed files with 70 additions and 20 deletions

View File

@ -16,6 +16,8 @@ namespace app\api\controller;
use app\api\logic\IndexLogic; use app\api\logic\IndexLogic;
use app\common\enum\notice\NoticeEnum;
use think\facade\Log;
use think\response\Json; 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); $result = IndexLogic::getDecorate($id);
return $this->data($result); 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());
}
}
} }

View File

@ -16,6 +16,8 @@ namespace app\api\logic;
use app\common\cache\WebScanLoginCache; use app\common\cache\WebScanLoginCache;
use app\common\logic\BaseLogic; 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\api\service\{UserTokenService, WechatUserService};
use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum}; use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum};
use app\common\service\{ use app\common\service\{
@ -44,23 +46,36 @@ class LoginLogic extends BaseLogic
* @author 段誉 * @author 段誉
* @date 2022/9/7 15:37 * @date 2022/9/7 15:37
*/ */
public static function register(array $params) public static function register(array $params): bool
{ {
try { try {
$userSn = User::createUserSn(); $userSn = User::createUserSn();
$passwordSalt = Config::get('project.unique_identification'); $passwordSalt = Config::get('project.unique_identification');
$password = create_password($params['password'], $passwordSalt); $password = create_password($params['password'], $passwordSalt);
$avatar = ConfigService::get('default_image', 'user_avatar'); $avatar = ConfigService::get('default_image', 'user_avatar');
User::create([ Db::transaction(function () use($userSn,$password,$avatar,$params) {
'sn' => $userSn, $user = User::create([
'avatar' => $avatar, 'sn' => $userSn,
'nickname' => '用户' . $userSn, 'avatar' => env('project.project_url').'/'.$avatar,
'account' => $params['account'], 'nickname' => '用户' . $userSn,
'password' => $password, 'account' => $params['mobile'],
'channel' => $params['channel'], '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; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
self::setError($e->getMessage()); self::setError($e->getMessage());

View File

@ -70,6 +70,9 @@ class LoginAccountValidate extends BaseValidate
// 账号密码登录 // 账号密码登录
if (LoginEnum::ACCOUNT_PASSWORD == $scene) { if (LoginEnum::ACCOUNT_PASSWORD == $scene) {
if (!isset($data['account'])) {
return '请输入账号';
}
if (!isset($data['password'])) { if (!isset($data['password'])) {
return '请输入密码'; return '请输入密码';
} }

View File

@ -14,7 +14,9 @@
namespace app\api\validate; namespace app\api\validate;
use app\common\enum\notice\NoticeEnum;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\service\sms\SmsDriver;
use app\common\validate\BaseValidate; use app\common\validate\BaseValidate;
/** /**
@ -31,23 +33,19 @@ class RegisterValidate extends BaseValidate
]; ];
protected $rule = [ protected $rule = [
'channel' => 'require', 'mobile' => 'require|mobile|unique:' . User::class,
'account' => 'require|length:3,12|unique:' . User::class . '|regex:register',
'password' => 'require|length:6,20|regex:password', 'password' => 'require|length:6,20|regex:password',
'password_confirm' => 'require|confirm' 'password_confirm' => 'require|confirm'
]; ];
protected $message = [ protected $message = [
'channel.require' => '注册来源参数缺失', 'mobile.require' => '请输入手机号',
'account.require' => '请输入账号', 'mobile.mobile' => '手机号格式错误',
'account.regex' => '账号须为字母数字组合', 'mobile.unique' => '手机号已存在',
'account.length' => '账号须为3-12位之间',
'account.unique' => '账号已存在',
'password.require' => '请输入密码', 'password.require' => '请输入密码',
'password.length' => '密码须在6-25位之间', 'password.length' => '密码须在6-25位之间',
'password.regex' => '密码须为数字,字母或符号组合', 'password.regex' => '密码须为数字,字母或符号组合',
'password_confirm.require' => '请确认密码', 'password_confirm.require' => '请确认密码',
'password_confirm.confirm' => '两次输入的密码不一致' 'password_confirm.confirm' => '两次输入的密码不一致'
]; ];
} }