46 lines
1.1 KiB
PHP
Raw Normal View History

2023-12-08 15:42:52 +08:00
<?php
namespace app\controller\api\dataview;
use app\common\repositories\user\UserRepository;
use crmeb\basic\BaseController;
use think\App;
2023-12-08 16:38:18 +08:00
use think\facade\Cache;
2023-12-08 15:42:52 +08:00
use think\facade\Db;
use think\exception\ValidateException;
class Login extends BaseController
{
public function __construct(App $app)
{
parent::__construct($app);
}
2023-12-08 16:38:18 +08:00
public function login()
2023-12-08 15:42:52 +08:00
{
$account = $this->request->post('account', '');
$password = $this->request->post('password', '');
if (!$account){
return app('json')->fail('请输入账号');
}
$user = Db::name('dataview_account')->where('account', $account)->find();
if (!$user) {
throw new ValidateException("账号不存在");
}
if (!md5($password) === $user['password']) {
$msg = '账号或密码错误';
throw new ValidateException($msg);
}
$expire = time()+ 3600 * 24;
$token = md5($expire);
2023-12-08 16:38:18 +08:00
// 缓存token
Cache::set($token.'_'.$user['uid'], $expire);
2023-12-08 15:42:52 +08:00
return app('json')->success(compact('user','token', 'expire'));
}
}