新增前台登录注册获取验证码接口 #5

Merged
weiz merged 1 commits from zhangwei into dev 2023-11-21 16:33:44 +08:00
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\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'];
/**
@ -90,5 +92,37 @@ class IndexController extends BaseApiController
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\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,7 +46,7 @@ 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();
@ -52,15 +54,28 @@ class LoginLogic extends BaseLogic
$password = create_password($params['password'], $passwordSalt);
$avatar = ConfigService::get('default_image', 'user_avatar');
User::create([
Db::transaction(function () use($userSn,$password,$avatar,$params) {
$user = User::create([
'sn' => $userSn,
'avatar' => $avatar,
'avatar' => env('project.project_url').'/'.$avatar,
'nickname' => '用户' . $userSn,
'account' => $params['account'],
'account' => $params['mobile'],
'mobile' => $params['mobile'],
'password' => $password,
'channel' => $params['channel'],
'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());

View File

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

View File

@ -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' => '两次输入的密码不一致'
];
}