登录功能接入数据库
This commit is contained in:
parent
6857d2c3de
commit
1ceaeb9214
106
app/api/controller/AdminController.php
Normal file
106
app/api/controller/AdminController.php
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
<?php
|
||||||
|
namespace app\api\controller;
|
||||||
|
|
||||||
|
use app\api\controller\BaseController;
|
||||||
|
use app\admin\model\AdminUser;
|
||||||
|
use app\index\model\AdminUserGroup;
|
||||||
|
|
||||||
|
use think\facade\Log;
|
||||||
|
use think\facade\Cache;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
|
class AdminController extends BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 生成用户token
|
||||||
|
*
|
||||||
|
* @param int $userId 用户ID
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function generateToken($userId)
|
||||||
|
{
|
||||||
|
// 生成一个简单的token,包含用户ID和时间戳
|
||||||
|
$data = [
|
||||||
|
'user_id' => $userId,
|
||||||
|
'timestamp' => time(),
|
||||||
|
'random' => mt_rand(100000, 999999)
|
||||||
|
];
|
||||||
|
|
||||||
|
// 使用base64编码,实际项目中建议使用JWT
|
||||||
|
return base64_encode(json_encode($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function login()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
return json(['code' => 1, 'msg' => '请求方法错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->request->post();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证数据
|
||||||
|
$validate = validate([
|
||||||
|
'account' => 'require|email',
|
||||||
|
'password' => 'require'
|
||||||
|
], [
|
||||||
|
'account.require' => '账户不能为空',
|
||||||
|
'account.email' => '邮箱格式不正确',
|
||||||
|
'password.require' => '密码不能为空'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$validate->check($data)) {
|
||||||
|
return json(['code' => 1, 'msg' => $validate->getError()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询用户
|
||||||
|
$user = AdminUser::where('account', $data['account'])->find();
|
||||||
|
if (!$user) {
|
||||||
|
return json(['code' => 1, 'msg' => '用户不存在']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证密码
|
||||||
|
if ($user->password !== md5($data['password'])) {
|
||||||
|
return json(['code' => 1, 'msg' => '密码错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成JWT token(这里使用简单的token,实际项目中建议使用JWT)
|
||||||
|
$token = $this->generateToken($user->id);
|
||||||
|
|
||||||
|
// 将token存储到缓存中,设置过期时间
|
||||||
|
Cache::set('user_token_' . $user->id, $token, 7 * 24 * 3600);
|
||||||
|
|
||||||
|
// 记录登录日志
|
||||||
|
Log::record('用户登录成功:' . $user->account, 'info');
|
||||||
|
|
||||||
|
// 返回用户信息和token
|
||||||
|
return json([
|
||||||
|
'code' => 0,
|
||||||
|
'msg' => '登录成功',
|
||||||
|
'data' => [
|
||||||
|
'token' => $token,
|
||||||
|
'user_info' => [
|
||||||
|
'id' => $user->id,
|
||||||
|
'account' => $user->account,
|
||||||
|
'name' => $user->name,
|
||||||
|
'avatar' => $user->avatar ?? '/static/images/avatar.png',
|
||||||
|
'phone' => $user->phone ?? '',
|
||||||
|
'sex' => $user->sex ?? 0,
|
||||||
|
'qq' => $user->qq ?? '',
|
||||||
|
'wechat' => $user->wechat ?? '',
|
||||||
|
'create_time' => $user->create_time
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::record('登录失败:' . $e->getMessage(), 'error');
|
||||||
|
return json(['code' => 1, 'msg' => '登录失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
90
app/api/controller/BaseController.php
Normal file
90
app/api/controller/BaseController.php
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 商业使用授权协议
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 [云泽网]. 保留所有权利.
|
||||||
|
*
|
||||||
|
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
|
||||||
|
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
|
||||||
|
*
|
||||||
|
* 授权购买请联系: 357099073@qq.com
|
||||||
|
* 官方网站: https://www.yunzer.cn
|
||||||
|
*
|
||||||
|
* 评估用户须知:
|
||||||
|
* 1. 禁止移除版权声明
|
||||||
|
* 2. 禁止用于生产环境
|
||||||
|
* 3. 禁止转售或分发
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\api\controller;
|
||||||
|
|
||||||
|
use think\App;
|
||||||
|
use think\Request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API控制器基础类
|
||||||
|
*/
|
||||||
|
abstract class BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Request实例
|
||||||
|
* @var \think\Request
|
||||||
|
*/
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 应用实例
|
||||||
|
* @var \think\App
|
||||||
|
*/
|
||||||
|
protected $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造方法
|
||||||
|
* @access public
|
||||||
|
* @param App $app 应用对象
|
||||||
|
*/
|
||||||
|
public function __construct(App $app)
|
||||||
|
{
|
||||||
|
$this->app = $app;
|
||||||
|
$this->request = $this->app->request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回JSON响应
|
||||||
|
* @param mixed $data 数据
|
||||||
|
* @param int $code 状态码
|
||||||
|
* @param string $msg 消息
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
protected function json($data = [], $code = 0, $msg = 'success')
|
||||||
|
{
|
||||||
|
return json([
|
||||||
|
'code' => $code,
|
||||||
|
'msg' => $msg,
|
||||||
|
'data' => $data
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回成功响应
|
||||||
|
* @param mixed $data 数据
|
||||||
|
* @param string $msg 消息
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
protected function success($data = [], $msg = 'success')
|
||||||
|
{
|
||||||
|
return $this->json($data, 0, $msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回错误响应
|
||||||
|
* @param string $msg 错误消息
|
||||||
|
* @param int $code 错误码
|
||||||
|
* @param mixed $data 数据
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
protected function error($msg = 'error', $code = 1, $data = [])
|
||||||
|
{
|
||||||
|
return $this->json($data, $code, $msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
402
app/api/controller/UserController.php
Normal file
402
app/api/controller/UserController.php
Normal file
@ -0,0 +1,402 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* 商业使用授权协议
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 [云泽网]. 保留所有权利.
|
||||||
|
*
|
||||||
|
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
|
||||||
|
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
|
||||||
|
*
|
||||||
|
* 授权购买请联系: 357099073@qq.com
|
||||||
|
* 官方网站: https://www.yunzer.cn
|
||||||
|
*
|
||||||
|
* 评估用户须知:
|
||||||
|
* 1. 禁止移除版权声明
|
||||||
|
* 2. 禁止用于生产环境
|
||||||
|
* 3. 禁止转售或分发
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace app\api\controller;
|
||||||
|
|
||||||
|
use app\api\controller\BaseController;
|
||||||
|
use app\index\model\User\Users as Users;
|
||||||
|
use app\index\model\User\UsersGroup as UsersGroup;
|
||||||
|
|
||||||
|
|
||||||
|
use think\facade\Log;
|
||||||
|
use think\facade\Cache;
|
||||||
|
use think\Response;
|
||||||
|
|
||||||
|
class UserController extends BaseController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 用户登录接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function login()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
return json(['code' => 1, 'msg' => '请求方法错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->request->post();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证数据
|
||||||
|
$validate = validate([
|
||||||
|
'account' => 'require|email',
|
||||||
|
'password' => 'require'
|
||||||
|
], [
|
||||||
|
'account.require' => '账户不能为空',
|
||||||
|
'account.email' => '邮箱格式不正确',
|
||||||
|
'password.require' => '密码不能为空'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$validate->check($data)) {
|
||||||
|
return json(['code' => 1, 'msg' => $validate->getError()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询用户
|
||||||
|
$user = Users::where('account', $data['account'])->find();
|
||||||
|
if (!$user) {
|
||||||
|
return json(['code' => 1, 'msg' => '用户不存在']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证密码
|
||||||
|
if ($user->password !== md5($data['password'])) {
|
||||||
|
return json(['code' => 1, 'msg' => '密码错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成JWT token(这里使用简单的token,实际项目中建议使用JWT)
|
||||||
|
$token = $this->generateToken($user->id);
|
||||||
|
|
||||||
|
// 将token存储到缓存中,设置过期时间
|
||||||
|
Cache::set('user_token_' . $user->id, $token, 7 * 24 * 3600);
|
||||||
|
|
||||||
|
// 记录登录日志
|
||||||
|
Log::record('用户登录成功:' . $user->account, 'info');
|
||||||
|
|
||||||
|
// 返回用户信息和token
|
||||||
|
return json([
|
||||||
|
'code' => 0,
|
||||||
|
'msg' => '登录成功',
|
||||||
|
'data' => [
|
||||||
|
'token' => $token,
|
||||||
|
'user_info' => [
|
||||||
|
'id' => $user->id,
|
||||||
|
'account' => $user->account,
|
||||||
|
'name' => $user->name,
|
||||||
|
'avatar' => $user->avatar ?? '/static/images/avatar.png',
|
||||||
|
'phone' => $user->phone ?? '',
|
||||||
|
'sex' => $user->sex ?? 0,
|
||||||
|
'qq' => $user->qq ?? '',
|
||||||
|
'wechat' => $user->wechat ?? '',
|
||||||
|
'create_time' => $user->create_time
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::record('登录失败:' . $e->getMessage(), 'error');
|
||||||
|
return json(['code' => 1, 'msg' => '登录失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户注册接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
return json(['code' => 1, 'msg' => '请求方法错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->request->post();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 验证数据
|
||||||
|
$validate = validate([
|
||||||
|
'account' => 'require|email|unique:users',
|
||||||
|
'code' => 'require|number|length:6',
|
||||||
|
'password' => 'require|min:6|max:20',
|
||||||
|
'repassword' => 'require|confirm:password'
|
||||||
|
], [
|
||||||
|
'account.require' => '账户不能为空',
|
||||||
|
'account.email' => '邮箱格式不正确',
|
||||||
|
'account.unique' => '该邮箱已注册',
|
||||||
|
'code.require' => '验证码不能为空',
|
||||||
|
'code.number' => '验证码必须为数字',
|
||||||
|
'code.length' => '验证码长度必须为6位',
|
||||||
|
'password.require' => '密码不能为空',
|
||||||
|
'password.min' => '密码长度不能小于6个字符',
|
||||||
|
'password.max' => '密码长度不能超过20个字符',
|
||||||
|
'repassword.require' => '确认密码不能为空',
|
||||||
|
'repassword.confirm' => '两次输入的密码不一致'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$validate->check($data)) {
|
||||||
|
return json(['code' => 1, 'msg' => $validate->getError()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证邮箱验证码
|
||||||
|
$emailCode = Cache::get('email_code_' . $data['account']);
|
||||||
|
if (!$emailCode || $emailCode != $data['code']) {
|
||||||
|
return json(['code' => 1, 'msg' => '验证码错误或已过期']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建用户
|
||||||
|
$user = new Users;
|
||||||
|
$user->account = $data['account'];
|
||||||
|
$user->password = md5($data['password']);
|
||||||
|
$user->name = $this->generateRandomName();
|
||||||
|
$user->create_time = time();
|
||||||
|
$user->save();
|
||||||
|
|
||||||
|
// 清除验证码缓存
|
||||||
|
Cache::delete('email_code_' . $data['account']);
|
||||||
|
|
||||||
|
return json(['code' => 0, 'msg' => '注册成功']);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return json(['code' => 1, 'msg' => '注册失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 退出登录接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function logout()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$token = $this->request->header('Authorization');
|
||||||
|
if ($token) {
|
||||||
|
// 从token中获取用户ID
|
||||||
|
$userId = $this->getUserIdFromToken($token);
|
||||||
|
if ($userId) {
|
||||||
|
// 清除token缓存
|
||||||
|
Cache::delete('user_token_' . $userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::record('用户退出登录', 'info');
|
||||||
|
return json(['code' => 0, 'msg' => '退出成功']);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::record('退出登录失败:' . $e->getMessage(), 'error');
|
||||||
|
return json(['code' => 1, 'msg' => '退出失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取用户信息接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function getUserInfo()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$token = $this->request->header('Authorization');
|
||||||
|
if (!$token) {
|
||||||
|
return json(['code' => 1, 'msg' => '请先登录']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$userId = $this->getUserIdFromToken($token);
|
||||||
|
if (!$userId) {
|
||||||
|
return json(['code' => 1, 'msg' => 'token无效']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证token是否在缓存中
|
||||||
|
$cachedToken = Cache::get('user_token_' . $userId);
|
||||||
|
if (!$cachedToken || $cachedToken !== $token) {
|
||||||
|
return json(['code' => 1, 'msg' => 'token已过期']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取用户信息
|
||||||
|
$user = Users::find($userId);
|
||||||
|
if (!$user) {
|
||||||
|
return json(['code' => 1, 'msg' => '用户不存在']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json([
|
||||||
|
'code' => 0,
|
||||||
|
'msg' => '获取成功',
|
||||||
|
'data' => [
|
||||||
|
'id' => $user->id,
|
||||||
|
'account' => $user->account,
|
||||||
|
'name' => $user->name,
|
||||||
|
'avatar' => $user->avatar ?? '/static/images/avatar.png',
|
||||||
|
'phone' => $user->phone ?? '',
|
||||||
|
'sex' => $user->sex ?? 0,
|
||||||
|
'qq' => $user->qq ?? '',
|
||||||
|
'wechat' => $user->wechat ?? '',
|
||||||
|
'create_time' => $user->create_time
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return json(['code' => 1, 'msg' => '获取用户信息失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送邮箱验证码接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function sendEmailCode()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
return json(['code' => 1, 'msg' => '请求方法错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$email = $this->request->post('account');
|
||||||
|
if (empty($email)) {
|
||||||
|
return json(['code' => 1, 'msg' => '邮箱不能为空']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证邮箱格式
|
||||||
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
|
return json(['code' => 1, 'msg' => '邮箱格式不正确']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查邮箱是否已注册
|
||||||
|
$exists = Users::where('account', $email)->find();
|
||||||
|
if ($exists) {
|
||||||
|
return json(['code' => 1, 'msg' => '该邮箱已注册']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成6位随机验证码
|
||||||
|
$code = mt_rand(100000, 999999);
|
||||||
|
|
||||||
|
// 这里应该调用邮件发送服务
|
||||||
|
// 为了演示,我们直接返回成功
|
||||||
|
// 实际项目中需要实现邮件发送逻辑
|
||||||
|
|
||||||
|
// 将验证码存入缓存,有效期5分钟
|
||||||
|
Cache::set('email_code_' . $email, $code, 300);
|
||||||
|
|
||||||
|
return json(['code' => 0, 'msg' => '验证码已发送']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改密码接口
|
||||||
|
*
|
||||||
|
* @return \think\Response
|
||||||
|
*/
|
||||||
|
public function updatePassword()
|
||||||
|
{
|
||||||
|
if (!$this->request->isPost()) {
|
||||||
|
return json(['code' => 1, 'msg' => '请求方法错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$token = $this->request->header('Authorization');
|
||||||
|
if (!$token) {
|
||||||
|
return json(['code' => 1, 'msg' => '请先登录']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$userId = $this->getUserIdFromToken($token);
|
||||||
|
if (!$userId) {
|
||||||
|
return json(['code' => 1, 'msg' => 'token无效']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = $this->request->post();
|
||||||
|
|
||||||
|
// 验证数据
|
||||||
|
$validate = validate([
|
||||||
|
'old_password' => 'require',
|
||||||
|
'new_password' => 'require|min:6|max:20',
|
||||||
|
'confirm_password' => 'require|confirm:new_password'
|
||||||
|
], [
|
||||||
|
'old_password.require' => '旧密码不能为空',
|
||||||
|
'new_password.require' => '新密码不能为空',
|
||||||
|
'new_password.min' => '新密码长度不能小于6个字符',
|
||||||
|
'new_password.max' => '新密码长度不能超过20个字符',
|
||||||
|
'confirm_password.require' => '确认密码不能为空',
|
||||||
|
'confirm_password.confirm' => '两次输入的密码不一致'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!$validate->check($data)) {
|
||||||
|
return json(['code' => 1, 'msg' => $validate->getError()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取用户信息
|
||||||
|
$user = Users::find($userId);
|
||||||
|
if (!$user) {
|
||||||
|
return json(['code' => 1, 'msg' => '用户不存在']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 验证旧密码
|
||||||
|
if ($user->password !== md5($data['old_password'])) {
|
||||||
|
return json(['code' => 1, 'msg' => '旧密码错误']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新密码
|
||||||
|
$user->password = md5($data['new_password']);
|
||||||
|
$user->update_time = time();
|
||||||
|
|
||||||
|
if ($user->save()) {
|
||||||
|
// 清除token,要求重新登录
|
||||||
|
Cache::delete('user_token_' . $userId);
|
||||||
|
return json(['code' => 0, 'msg' => '密码修改成功,请重新登录']);
|
||||||
|
} else {
|
||||||
|
return json(['code' => 1, 'msg' => '密码修改失败']);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return json(['code' => 1, 'msg' => '密码修改失败:' . $e->getMessage()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成简单的token
|
||||||
|
*
|
||||||
|
* @param int $userId
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function generateToken($userId)
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'user_id' => $userId,
|
||||||
|
'timestamp' => time(),
|
||||||
|
'random' => mt_rand(100000, 999999)
|
||||||
|
];
|
||||||
|
|
||||||
|
return base64_encode(json_encode($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从token中获取用户ID
|
||||||
|
*
|
||||||
|
* @param string $token
|
||||||
|
* @return int|null
|
||||||
|
*/
|
||||||
|
private function getUserIdFromToken($token)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$data = json_decode(base64_decode($token), true);
|
||||||
|
if ($data && isset($data['user_id'])) {
|
||||||
|
return $data['user_id'];
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成随机用户名
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function generateRandomName()
|
||||||
|
{
|
||||||
|
return '云朵_' . mt_rand(100000, 999999);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
VITE_APP_ENV=development
|
VITE_APP_ENV=development
|
||||||
VITE_APP_DEBUG_MODE=true
|
VITE_APP_DEBUG_MODE=true
|
||||||
VITE_APP_TITLE=项目管理系统
|
VITE_APP_TITLE=项目管理系统
|
||||||
VITE_APP_API_BASE_URL=https://www.yunzer.cn/api
|
VITE_APP_API_BASE_URL=https://www.yunzer.cn/api
|
||||||
|
|||||||
@ -10,24 +10,9 @@ const api = axios.create({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// 调试信息
|
|
||||||
console.log('API配置:', {
|
|
||||||
baseURL: ENV_CONFIG.API_BASE_URL,
|
|
||||||
timeout: ENV_CONFIG.REQUEST_TIMEOUT,
|
|
||||||
'环境变量VITE_APP_API_BASE_URL': import.meta.env.VITE_APP_API_BASE_URL
|
|
||||||
})
|
|
||||||
|
|
||||||
// 请求拦截器 - 添加token
|
// 请求拦截器 - 添加token
|
||||||
api.interceptors.request.use(
|
api.interceptors.request.use(
|
||||||
(config) => {
|
(config) => {
|
||||||
// 调试信息
|
|
||||||
console.log('API请求:', {
|
|
||||||
method: config.method,
|
|
||||||
url: config.url,
|
|
||||||
fullURL: (config.baseURL || '') + (config.url || ''),
|
|
||||||
baseURL: config.baseURL
|
|
||||||
})
|
|
||||||
|
|
||||||
const token = localStorage.getItem(ENV_CONFIG.TOKEN_KEY)
|
const token = localStorage.getItem(ENV_CONFIG.TOKEN_KEY)
|
||||||
if (token) {
|
if (token) {
|
||||||
config.headers.Authorization = `Bearer ${token}`
|
config.headers.Authorization = `Bearer ${token}`
|
||||||
@ -42,8 +27,16 @@ api.interceptors.request.use(
|
|||||||
// 响应拦截器 - 处理错误
|
// 响应拦截器 - 处理错误
|
||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
(response) => {
|
(response) => {
|
||||||
// 直接返回响应数据,而不是整个response对象
|
const data = response.data
|
||||||
return response.data
|
|
||||||
|
// 检查后端返回的状态码
|
||||||
|
if (data.code === 0) {
|
||||||
|
// 成功,返回data字段的内容
|
||||||
|
return data.data
|
||||||
|
} else {
|
||||||
|
// 失败,抛出错误
|
||||||
|
return Promise.reject(new Error(data.msg || '请求失败'))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
@ -57,8 +50,8 @@ api.interceptors.response.use(
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 用户登录接口
|
// 用户登录接口
|
||||||
export const login = (data: { username: string; password: string }) => {
|
export const login = (data: { account: string; password: string }) => {
|
||||||
return api.post('/user/login', data)
|
return api.post('/admin/login', data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取用户信息接口
|
// 获取用户信息接口
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const ENV_CONFIG = {
|
const ENV_CONFIG = {
|
||||||
// API配置
|
// API配置
|
||||||
API_BASE_URL: import.meta.env.VITE_APP_API_BASE_URL || 'https://www.yunzer.cn/api',
|
API_BASE_URL: import.meta.env.VITE_APP_API_BASE_URL,
|
||||||
REQUEST_TIMEOUT: 10000,
|
REQUEST_TIMEOUT: 10000,
|
||||||
|
|
||||||
// 应用配置
|
// 应用配置
|
||||||
|
|||||||
@ -3,27 +3,25 @@ import { login as loginApi, getUserInfo as getUserInfoApi, logout as logoutApi }
|
|||||||
import ENV_CONFIG from '@/config/env'
|
import ENV_CONFIG from '@/config/env'
|
||||||
|
|
||||||
interface LoginForm {
|
interface LoginForm {
|
||||||
username: string
|
account: string
|
||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface UserInfo {
|
interface UserInfo {
|
||||||
id: number
|
id: number
|
||||||
username: string
|
account: string
|
||||||
name: string
|
name: string
|
||||||
avatar?: string
|
avatar?: string
|
||||||
role: string
|
phone?: string
|
||||||
}
|
sex?: number
|
||||||
|
qq?: string
|
||||||
interface ApiResponse<T> {
|
wechat?: string
|
||||||
code: number
|
create_time?: number
|
||||||
message: string
|
|
||||||
data: T
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LoginResponse {
|
interface LoginResponse {
|
||||||
token: string
|
token: string
|
||||||
userInfo: UserInfo
|
user_info: UserInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
const useUserStore = defineStore('user', {
|
const useUserStore = defineStore('user', {
|
||||||
@ -48,29 +46,29 @@ const useUserStore = defineStore('user', {
|
|||||||
// 用户登录
|
// 用户登录
|
||||||
async userLogin(loginForm: LoginForm) {
|
async userLogin(loginForm: LoginForm) {
|
||||||
try {
|
try {
|
||||||
const response = await loginApi(loginForm) as unknown as ApiResponse<LoginResponse>
|
const response = await loginApi(loginForm) as unknown as LoginResponse
|
||||||
|
|
||||||
// 假设API返回格式为 { code: 200, data: { token: string, userInfo: UserInfo } }
|
// 后端返回格式为 { token: string, user_info: UserInfo }
|
||||||
if (response.code === 200 && response.data) {
|
if (response && response.token && response.user_info) {
|
||||||
const { token, userInfo } = response.data
|
const { token, user_info } = response
|
||||||
|
|
||||||
// 保存登录状态
|
// 保存登录状态
|
||||||
this.token = token
|
this.token = token
|
||||||
this.userInfo = userInfo
|
this.userInfo = user_info
|
||||||
this.isLogin = true
|
this.isLogin = true
|
||||||
|
|
||||||
// 保存到 localStorage
|
// 保存到 localStorage
|
||||||
localStorage.setItem(ENV_CONFIG.TOKEN_KEY, token)
|
localStorage.setItem(ENV_CONFIG.TOKEN_KEY, token)
|
||||||
localStorage.setItem(ENV_CONFIG.USER_INFO_KEY, JSON.stringify(userInfo))
|
localStorage.setItem(ENV_CONFIG.USER_INFO_KEY, JSON.stringify(user_info))
|
||||||
|
|
||||||
return userInfo
|
return user_info
|
||||||
} else {
|
} else {
|
||||||
throw new Error(response.message || '登录失败')
|
throw new Error('登录响应数据格式错误')
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
// 处理不同类型的错误
|
// 处理不同类型的错误
|
||||||
if (error.response?.data?.message) {
|
if (error.response?.data?.msg) {
|
||||||
throw new Error(error.response.data.message)
|
throw new Error(error.response.data.msg)
|
||||||
} else if (error.message) {
|
} else if (error.message) {
|
||||||
throw new Error(error.message)
|
throw new Error(error.message)
|
||||||
} else {
|
} else {
|
||||||
@ -104,10 +102,10 @@ const useUserStore = defineStore('user', {
|
|||||||
if (token && userInfoStr) {
|
if (token && userInfoStr) {
|
||||||
try {
|
try {
|
||||||
// 验证token是否有效
|
// 验证token是否有效
|
||||||
const response = await getUserInfoApi() as unknown as ApiResponse<UserInfo>
|
const response = await getUserInfoApi() as unknown as UserInfo
|
||||||
if (response.code === 200 && response.data) {
|
if (response && response.id) {
|
||||||
this.token = token
|
this.token = token
|
||||||
this.userInfo = response.data
|
this.userInfo = response
|
||||||
this.isLogin = true
|
this.isLogin = true
|
||||||
} else {
|
} else {
|
||||||
// token无效,清除本地存储但不调用logout API
|
// token无效,清除本地存储但不调用logout API
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-input
|
<el-input
|
||||||
:prefix-icon="User"
|
:prefix-icon="User"
|
||||||
v-model="loginForm.username"
|
v-model="loginForm.account"
|
||||||
></el-input>
|
></el-input>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
@ -45,7 +45,7 @@ import { ElNotification } from 'element-plus'
|
|||||||
import useUserStore from '@/store/modules/user'
|
import useUserStore from '@/store/modules/user'
|
||||||
|
|
||||||
// 收集账号与密码数据
|
// 收集账号与密码数据
|
||||||
const loginForm = reactive({ username: '', password: '' })
|
const loginForm = reactive({ account: '', password: '' })
|
||||||
// 按钮加载状态
|
// 按钮加载状态
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
// 获取路由实例
|
// 获取路由实例
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user