feat: 新增登录接口
This commit is contained in:
47
app/admin/controller/v1/Login.php
Normal file
47
app/admin/controller/v1/Login.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\UserModel;
|
||||
use app\admin\validate\v1\LoginValidate;
|
||||
use think\facade\Cache;
|
||||
|
||||
class Login
|
||||
{
|
||||
/**
|
||||
* 登录验证接口
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 获取参数
|
||||
$post = request()->post([
|
||||
'username',
|
||||
'password',
|
||||
]);
|
||||
|
||||
// 验证参数
|
||||
$validate = new LoginValidate();
|
||||
if (!$validate->check($post)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
// 验证用户
|
||||
$user = UserModel::usernameOrMobile($post['username'])->find();
|
||||
if (!$user) {
|
||||
return error('用户不存在!');
|
||||
}
|
||||
|
||||
// 验证密码
|
||||
if ($user['password'] != password_with_salt($post['password'], $user['salt'])) {
|
||||
return error('密码错误!');
|
||||
}
|
||||
|
||||
// 验证用户状态
|
||||
if ($user['status'] == -1) {
|
||||
return error('用户已禁用,请联系管理员!');
|
||||
}
|
||||
dump(session("ss"));
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
43
app/admin/model/v1/UserModel.php
Normal file
43
app/admin/model/v1/UserModel.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class UserModel extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'sys_user';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 隐藏字段
|
||||
protected $hidden = ['password', 'salt'];
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'username' => 'string',
|
||||
'password' => 'string',
|
||||
'salt' => 'string',
|
||||
'role_id' => 'int',
|
||||
'nickname' => 'string',
|
||||
'avatar' => 'string',
|
||||
'mobile' => 'string',
|
||||
'email' => 'string',
|
||||
'status' => 'int',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
] ;
|
||||
|
||||
// 用户名查询范围
|
||||
public function scopeUsernameOrMobile($query, $username)
|
||||
{
|
||||
return $query->where('username', '=', $username)->whereOr('mobile', '=', $username);
|
||||
}
|
||||
}
|
||||
27
app/admin/route/v1.php
Normal file
27
app/admin/route/v1.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
use think\facade\Route;
|
||||
|
||||
// v1版本路由定义
|
||||
Route::group('v1', function () {
|
||||
// 用户模块
|
||||
Route::group('user', function () {
|
||||
// 获取验证码
|
||||
Route::get('captcha', 'Captcha/index');
|
||||
|
||||
// 登录接口
|
||||
Route::post('login', 'Login/index');
|
||||
});
|
||||
})->prefix('v1.');
|
||||
|
||||
Route::miss(function() {
|
||||
return '404 Not Found!';
|
||||
});
|
||||
31
app/admin/validate/v1/LoginValidate.php
Normal file
31
app/admin/validate/v1/LoginValidate.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class LoginValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'username' => 'require',
|
||||
'password' => 'require',
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user