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' => '密码不能为空',
|
||||
];
|
||||
}
|
||||
@@ -1,2 +1,35 @@
|
||||
<?php
|
||||
// 应用公共文件
|
||||
|
||||
// 接口错误返回
|
||||
if (!function_exists('error')) {
|
||||
function error($msg = '', $data = [])
|
||||
{
|
||||
return \apiret\Api::error($msg, $data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 接口成功返回
|
||||
if (!function_exists('success')) {
|
||||
function success($msg = '', $data = [])
|
||||
{
|
||||
return \apiret\Api::success($msg, $data);
|
||||
}
|
||||
}
|
||||
|
||||
// 接口调结果返回
|
||||
if (!function_exists('result')) {
|
||||
function result($errno, $msg = '', $data = [])
|
||||
{
|
||||
return \apiret\Api::result($errno)->message($msg)->response($data);
|
||||
}
|
||||
}
|
||||
|
||||
// 密码加盐
|
||||
if (!function_exists('password_with_salt')) {
|
||||
function password_with_salt($password, $salt)
|
||||
{
|
||||
return md5(hash('sha256', $password . $salt));
|
||||
}
|
||||
}
|
||||
22
config/apiret.php
Normal file
22
config/apiret.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | api 返回状态配置
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
return [
|
||||
'status_var' => 'code',
|
||||
'message_var' => 'msg',
|
||||
'data_var' => 'data',
|
||||
'states' => [
|
||||
'success' => [
|
||||
'code' => 0,
|
||||
'msg' => '操作成功!',
|
||||
'data' => []
|
||||
],
|
||||
'error' => [
|
||||
'code' => 1,
|
||||
'msg' => '操作错误!',
|
||||
'data' => []
|
||||
],
|
||||
]
|
||||
];
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
return [
|
||||
// 默认缓存驱动
|
||||
'default' => 'file',
|
||||
'default' => 'redis',
|
||||
|
||||
// 缓存连接方式配置
|
||||
'stores' => [
|
||||
@@ -25,5 +25,19 @@ return [
|
||||
'serialize' => [],
|
||||
],
|
||||
// 更多的缓存连接
|
||||
'redis' => [
|
||||
// 驱动方式
|
||||
'type' => 'redis',
|
||||
// 服务器地址
|
||||
'host' => '127.0.0.1',
|
||||
// 端口
|
||||
'port' => 6379,
|
||||
// 密码
|
||||
'password' => 'orico@f2b211',
|
||||
// 缓存有效期 0表示永久缓存
|
||||
'expire' => 0,
|
||||
// 缓存前缀
|
||||
'prefix' => 'ow:',
|
||||
]
|
||||
],
|
||||
];
|
||||
|
||||
@@ -9,7 +9,7 @@ return [
|
||||
// 是否开启路由延迟解析
|
||||
'url_lazy_route' => false,
|
||||
// 是否强制使用路由
|
||||
'url_route_must' => false,
|
||||
'url_route_must' => true,
|
||||
// 是否区分大小写
|
||||
'url_case_sensitive' => false,
|
||||
// 合并路由规则
|
||||
|
||||
Reference in New Issue
Block a user