You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.3 KiB
90 lines
2.3 KiB
<?php
|
|
namespace app\api\controller;
|
|
|
|
use app\api\service\UserService;
|
|
use think\Controller;
|
|
use think\response\Json;
|
|
|
|
/**
|
|
* 用户账户控制器
|
|
* 功能:MVC 中的C,处理用户输入和反馈给用户
|
|
*/
|
|
class Passport extends ApiController{
|
|
/**
|
|
* 登录
|
|
*/
|
|
public function login():Json
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
return $this->renderError('不支持GET请求');
|
|
}
|
|
$data = $this->postData();
|
|
$model = new UserService;
|
|
//
|
|
$data = $this->postData();
|
|
if (($userInfo = $model->login($data['uname'],$data['upass'])) === false) {
|
|
return $this->renderError($model->getError() ?: '登录失败');
|
|
}
|
|
return $this->renderSuccess([
|
|
'userId' => $userInfo['uid'],
|
|
'token' => $model->getToken($userInfo['uid'])
|
|
], '');
|
|
}
|
|
|
|
/**
|
|
* 用户注册
|
|
* @return Json
|
|
*/
|
|
public function register():Json
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
return $this->renderError('不支持GET请求');
|
|
}
|
|
$model = new UserService;
|
|
if (($userInfo = $model->register($this->postData())) === false) {
|
|
return $this->renderError($model->getError() ?: '注册失败');
|
|
}
|
|
return $this->renderSuccess("注册成功");
|
|
}
|
|
|
|
}
|
|
|
|
// app/controller/Index.php
|
|
// namespace app\controller;
|
|
|
|
// use app\service\JWTService;
|
|
// use think\Request;
|
|
|
|
// class IndexController
|
|
// {
|
|
// protected $jwtService;
|
|
|
|
// public function __construct(JWTService $jwtService)
|
|
// {
|
|
// $this->jwtService = $jwtService;
|
|
// }
|
|
|
|
// public function login(Request $request)
|
|
// {
|
|
// $username = $request->param('username');
|
|
// $password = $request->param('password');
|
|
|
|
// // 假设验证用户名和密码成功
|
|
// $claims = ['id' => 1, 'username' => $username];
|
|
|
|
// $token = $this->jwtService->createToken($claims);
|
|
|
|
// return json(['token' => $token]);
|
|
// }
|
|
|
|
// public function protectedRoute(Request $request)
|
|
// {
|
|
// $claims = $request->attributes->get('claims');
|
|
|
|
// if (empty($claims)) {
|
|
// return json(['error' => 'Unauthorized'], 401);
|
|
// }
|
|
|
|
// return json(['message' => 'Welcome, ' . $claims['username']]);
|
|
// }
|
|
// }
|