diff --git a/application/admin/view/common/script.html b/application/admin/view/common/script.html index 01c615e3..04d1e80f 100644 --- a/application/admin/view/common/script.html +++ b/application/admin/view/common/script.html @@ -1 +1 @@ - \ No newline at end of file + diff --git a/application/api/controller/Ems.php b/application/api/controller/Ems.php index 7d39a12b..1a37c1b5 100644 --- a/application/api/controller/Ems.php +++ b/application/api/controller/Ems.php @@ -30,9 +30,17 @@ class Ems extends Api public function send() { $email = $this->request->post("email"); + $captcha = $this->request->post("captcha"); $event = $this->request->post("event"); $event = $event ? $event : 'register'; + //发送前验证码 + if (config('fastadmin.user_api_captcha')) { + if (!\think\Validate::is($captcha, 'captcha')) { + $this->error("验证码不正确"); + } + } + $last = Emslib::get($email, $event); if ($last && time() - $last['createtime'] < 60) { $this->error(__('发送频繁')); diff --git a/application/api/controller/Sms.php b/application/api/controller/Sms.php index 373b1299..df335fb4 100644 --- a/application/api/controller/Sms.php +++ b/application/api/controller/Sms.php @@ -25,9 +25,16 @@ class Sms extends Api public function send() { $mobile = $this->request->post("mobile"); + $captcha = $this->request->post("captcha"); $event = $this->request->post("event"); $event = $event ? $event : 'register'; + //发送前验证码 + if (config('fastadmin.user_api_captcha')) { + if (!\think\Validate::is($captcha, 'captcha')) { + $this->error("验证码不正确"); + } + } if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('手机号不正确')); } diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 762d2e5d..b13c9002 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -105,7 +105,7 @@ class User extends Api * @param string $password 密码 * @param string $email 邮箱 * @param string $mobile 手机号 - * @param string $code 验证码 + * @param string $captcha 验证码 */ public function register() { @@ -113,7 +113,8 @@ class User extends Api $password = $this->request->post('password'); $email = $this->request->post('email'); $mobile = $this->request->post('mobile'); - $code = $this->request->post('code'); + $captcha = $this->request->post("captcha", $this->request->post('code')); + if (!$username || !$password) { $this->error(__('Invalid parameters')); } @@ -123,7 +124,7 @@ class User extends Api if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) { $this->error(__('Mobile is incorrect')); } - $ret = Sms::check($mobile, $code, 'register'); + $ret = Sms::check($mobile, $captcha, 'register'); if (!$ret) { $this->error(__('Captcha is incorrect')); } diff --git a/application/config.php b/application/config.php index fff70edd..a58823bb 100755 --- a/application/config.php +++ b/application/config.php @@ -232,6 +232,8 @@ return [ 'captcha' => [ // 验证码字符集合 'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY', + // 验证码过期时间(s) + 'expire' => 600, // 验证码字体大小(px) 'fontSize' => 18, // 是否画混淆曲线 @@ -265,7 +267,9 @@ return [ //是否开启前台会员中心 'usercenter' => true, //会员注册验证码类型email/mobile/wechat/text/false - 'user_register_captcha' => 'text', + 'user_register_captcha' => 'mobile', + //是否启用发送前验证码(用于短信和邮件发送) + 'user_api_captcha' => true, //登录验证码 'login_captcha' => true, //登录失败超过10次则1天后重试 diff --git a/application/index/controller/User.php b/application/index/controller/User.php index 25e9c3da..13512027 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -7,6 +7,7 @@ use app\common\controller\Frontend; use app\common\library\Ems; use app\common\library\Sms; use app\common\model\Attachment; +use fast\Random; use think\Config; use think\Cookie; use think\Hook; @@ -19,7 +20,7 @@ use think\Validate; class User extends Frontend { protected $layout = 'default'; - protected $noNeedLogin = ['login', 'register', 'third']; + protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'third']; protected $noNeedRight = ['*']; public function _initialize() @@ -65,7 +66,7 @@ class User extends Frontend */ public function register() { - $url = $this->request->request('url', '', 'trim'); + $url = $this->request->request('url', '', 'trim|xss_clean'); if ($this->auth->id) { $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index')); } @@ -144,7 +145,7 @@ class User extends Frontend */ public function login() { - $url = $this->request->request('url', '', 'trim'); + $url = $this->request->request('url', '', 'trim|xss_clean'); if ($this->auth->id) { $this->success(__('You\'ve logged in, do not login again'), $url ? $url : url('user/index')); } @@ -193,6 +194,53 @@ class User extends Frontend return $this->view->fetch(); } + /** + * 手机号验证码登录 + */ + public function mobilelogin() + { + $url = $this->request->request('url', '', 'trim|xss_clean'); + if ($this->request->isPost()) { + $mobile = $this->request->post('mobile'); + $captcha = $this->request->post('captcha'); + if (!$mobile || !$captcha) { + $this->error(__('Invalid parameters')); + } + if (!Validate::regex($mobile, "^1\d{10}$")) { + $this->error(__('Mobile is incorrect')); + } + if (!Sms::check($mobile, $captcha, 'mobilelogin')) { + $this->error(__('Captcha is incorrect')); + } + $user = \app\common\model\User::getByMobile($mobile); + if ($user) { + if ($user->status != 'normal') { + $this->error(__('Account is locked')); + } + //如果已经有账号则直接登录 + $ret = $this->auth->direct($user->id); + } else { + $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []); + } + if ($ret) { + Sms::flush($mobile, 'mobilelogin'); + $data = ['userinfo' => $this->auth->getUserinfo()]; + $this->success(__('Logged in successful'), $url); + } else { + $this->error($this->auth->getError()); + } + } + //判断来源 + $referer = $this->request->server('HTTP_REFERER'); + if (!$url && (strtolower(parse_url($referer, PHP_URL_HOST)) == strtolower($this->request->host())) + && !preg_match("/(user\/login|user\/register|user\/logout)/i", $referer)) { + $url = $referer; + } + $this->view->assign('url', $url); + $this->view->assign('title', __('Login')); + return $this->view->fetch(); + } + /** * 退出登录 */ diff --git a/application/index/lang/zh-cn/user.php b/application/index/lang/zh-cn/user.php index 26d1efd7..0b4ad5c0 100755 --- a/application/index/lang/zh-cn/user.php +++ b/application/index/lang/zh-cn/user.php @@ -19,8 +19,12 @@ return [ 'Change' => '修改', 'Click to edit' => '点击编辑', 'Email/Mobile/Username' => '邮箱/手机/用户名', + 'Sign in with account' => '使用账号密码登录', + 'Sign in with mobile phone' => '使用手机验证码登录', 'Sign up successful' => '注册成功', 'Email active successful' => '邮箱激活成功', + 'Please enter your mobile phone number' => '请输入你的手机号', + 'Please enter %s numbers' => '请输入%s位数字', 'Username can not be empty' => '用户名不能为空', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符', 'Username must be 6 to 30 characters' => '用户名必须6-30个字符', @@ -61,7 +65,7 @@ return [ 'Logout successful' => '退出成功', 'User center already closed' => '会员中心已经关闭', 'Don\'t have an account? Sign up' => '还没有账号?点击注册', - 'Already have an account? Sign in' => '已经有账号?点击登录', + 'Already have an account? Sign in' => '已经有账号?点击登录', 'Operation failed' => '操作失败', 'Invalid parameters' => '参数不正确', 'Change password failure' => '修改密码失败', diff --git a/application/index/view/common/captcha.html b/application/index/view/common/captcha.html index d424e70f..bf8cfff0 100644 --- a/application/index/view/common/captcha.html +++ b/application/index/view/common/captcha.html @@ -1,19 +1,19 @@ {if "[type]" == 'email'} - + - 发送验证码 + {:__('Send verification code')} {elseif "[type]" == 'mobile'/} - + - 发送验证码 + {:__('Send verification code')} {elseif "[type]" == 'wechat'/} {if get_addon_info('wechat')} - 获取验证码 + {:__('Send verification code')} {else/} 请在后台插件管理中安装《微信管理插件》 @@ -24,4 +24,4 @@ {/if} - + \ No newline at end of file diff --git a/application/index/view/common/script.html b/application/index/view/common/script.html index bae2707b..4c45a84d 100644 --- a/application/index/view/common/script.html +++ b/application/index/view/common/script.html @@ -1 +1,24 @@ - \ No newline at end of file +{if $Think.config.fastadmin.user_api_captcha} + +{/if} + diff --git a/application/index/view/user/login.html b/application/index/view/user/login.html index 792c65ac..a041ba93 100755 --- a/application/index/view/user/login.html +++ b/application/index/view/user/login.html @@ -1,19 +1,40 @@
- +