登录注册功能
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\BaseApp;
|
||||
|
||||
use App\Service\UtilsService;
|
||||
use App\Service\common\UtilsService;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
namespace App\BaseApp;
|
||||
|
||||
use App\Models\UserModel;
|
||||
use App\Service\UtilsService;
|
||||
use App\Service\common\UtilsService;
|
||||
|
||||
class BaseService
|
||||
{
|
||||
|
||||
37
app/Http/Controllers/LoginController.php
Normal file
37
app/Http/Controllers/LoginController.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
use App\Service\LoginService;
|
||||
|
||||
class LoginController extends BaseController
|
||||
{
|
||||
//
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->service = LoginService::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* @Method POST
|
||||
* @return \Illuminate\Http\JsonResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$this->insertField = [
|
||||
'account', 'password', 'is_remember'
|
||||
];
|
||||
$param = $this->checkRequiredFields(request()->post());
|
||||
|
||||
return jok(
|
||||
$this->service->login($param['account'], $param['password']),
|
||||
'登录成功'
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
10
app/Http/Controllers/UserController.php
Normal file
10
app/Http/Controllers/UserController.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\BaseApp\BaseController;
|
||||
|
||||
class UserController extends BaseController
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* 分类表
|
||||
*/
|
||||
class CategoryModal extends Model
|
||||
class CategoryModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'categories';
|
||||
@@ -21,6 +21,6 @@ class CategoryModal extends Model
|
||||
*/
|
||||
public function projects(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectModal::class, 'category_id', 'id');
|
||||
return $this->hasMany(ProjectModel::class, 'category_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 核心功能表
|
||||
*/
|
||||
class FeatureModal extends Model
|
||||
class FeatureModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'features';
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* 框架表
|
||||
*/
|
||||
class FrameworkModal extends Model
|
||||
class FrameworkModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'frameworks';
|
||||
@@ -21,6 +21,6 @@ class FrameworkModal extends Model
|
||||
*/
|
||||
public function projects()
|
||||
{
|
||||
return $this->belongsToMany(ProjectModal::class, 'project_frameworks_relations', 'framework_id', 'project_id');
|
||||
return $this->belongsToMany(ProjectModel::class, 'project_frameworks_relations', 'framework_id', 'project_id');
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* 标签表
|
||||
*/
|
||||
class LabelModal extends Model
|
||||
class LabelModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'labels';
|
||||
@@ -22,6 +21,6 @@ class LabelModal extends Model
|
||||
*/
|
||||
public function projects(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(ProjectModal::class, 'project_labels_relations', 'label_id', 'project_id');
|
||||
return $this->belongsToMany(ProjectModel::class, 'project_labels_relations', 'label_id', 'project_id');
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 订单详情表
|
||||
*/
|
||||
class OrderItemModal extends Model
|
||||
class OrderItemModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'order_items';
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
/**
|
||||
* 订单表
|
||||
*/
|
||||
class OrderModal extends Model
|
||||
class OrderModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'orders';
|
||||
@@ -22,7 +22,7 @@ class OrderModal extends Model
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->hasOne(UserModal::class, 'id', 'user_id');
|
||||
return $this->hasOne(UserModel::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,6 +31,6 @@ class OrderModal extends Model
|
||||
*/
|
||||
public function items(): HasMany
|
||||
{
|
||||
return $this->hasMany(OrderItemModal::class, 'order_id', 'id');
|
||||
return $this->hasMany(OrderItemModel::class, 'order_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 合作伙伴表
|
||||
*/
|
||||
class PartnerModal extends Model
|
||||
class PartnerModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'partners';
|
||||
@@ -2,13 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 项目框架关联表
|
||||
*/
|
||||
class ProjectFrameworksRelationModal extends Model
|
||||
class ProjectFrameworksRelationModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'project_frameworks_relations';
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
/**
|
||||
* 项目标签关联表
|
||||
*/
|
||||
class ProjectLabelRelationModal extends Model
|
||||
class ProjectLabelRelationModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'project_label_relations';
|
||||
@@ -21,7 +21,7 @@ class ProjectLabelRelationModal extends Model
|
||||
*/
|
||||
public function project(): HasOne
|
||||
{
|
||||
return $this->hasOne(ProjectModal::class, 'id', 'project_id');
|
||||
return $this->hasOne(ProjectModel::class, 'id', 'project_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -30,6 +30,6 @@ class ProjectLabelRelationModal extends Model
|
||||
*/
|
||||
public function label(): HasOne
|
||||
{
|
||||
return $this->hasOne(LabelModal::class, 'id', 'label_id');
|
||||
return $this->hasOne(LabelModel::class, 'id', 'label_id');
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
@@ -10,7 +10,7 @@ use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
/**
|
||||
* 项目表
|
||||
*/
|
||||
class ProjectModal extends Model
|
||||
class ProjectModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'projects';
|
||||
@@ -23,7 +23,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function user(): HasOne
|
||||
{
|
||||
return $this->hasOne(UserModal::class, 'id', 'user_id');
|
||||
return $this->hasOne(UserModel::class, 'id', 'user_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,7 +32,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function author(): HasOne
|
||||
{
|
||||
return $this->hasOne(UserModal::class, 'id', 'author_id');
|
||||
return $this->hasOne(UserModel::class, 'id', 'author_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,7 +41,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function labels(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(LabelModal::class, 'project_label_relations', 'project_id', 'label_id');
|
||||
return $this->belongsToMany(LabelModel::class, 'project_label_relations', 'project_id', 'label_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +50,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function screenshots(): HasMany
|
||||
{
|
||||
return $this->hasMany(ScreenshotModal::class, 'project_id', 'id');
|
||||
return $this->hasMany(ScreenshotModel::class, 'project_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +59,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function frameworks(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(FrameworkModal::class, 'project_frameworks_relations', 'project_id', 'framework_id');
|
||||
return $this->belongsToMany(FrameworkModel::class, 'project_frameworks_relations', 'project_id', 'framework_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +68,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function technologyStacks(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(TechnologyStackModal::class, 'project_technology_stacks_relations', 'project_id', 'technology_stacks_id');
|
||||
return $this->belongsToMany(TechnologyStackModel::class, 'project_technology_stacks_relations', 'project_id', 'technology_stacks_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,7 +77,7 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function features(): HasMany
|
||||
{
|
||||
return $this->hasMany(FeatureModal::class, 'project_id', 'id');
|
||||
return $this->hasMany(FeatureModel::class, 'project_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,6 +86,6 @@ class ProjectModal extends Model
|
||||
*/
|
||||
public function category(): HasOne
|
||||
{
|
||||
return $this->hasOne(CategoryModal::class, 'id', 'category_id');
|
||||
return $this->hasOne(CategoryModel::class, 'id', 'category_id');
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 项目技术栈关联表
|
||||
*/
|
||||
class ProjectTechnologyStacksRelationModal extends Model
|
||||
class ProjectTechnologyStacksRelationModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'project_technology_stacks_relations';
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 角色表
|
||||
*/
|
||||
class RoleModal extends Model
|
||||
class RoleModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'roles';
|
||||
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
|
||||
/**
|
||||
* 截图表
|
||||
*/
|
||||
class ScreenshotModal extends Model
|
||||
class ScreenshotModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'screenshots';
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* 技术栈表
|
||||
*/
|
||||
class TechnologyStackModal extends Model
|
||||
class TechnologyStackModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'technology_stacks';
|
||||
@@ -21,6 +21,6 @@ class TechnologyStackModal extends Model
|
||||
*/
|
||||
public function projects(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(ProjectModal::class, 'project_technology_stack_relations', 'technology_stack_id', 'project_id');
|
||||
return $this->belongsToMany(ProjectModel::class, 'project_technology_stack_relations', 'technology_stack_id', 'project_id');
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\BaseApp\BaseModel;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
/**
|
||||
* 用户表
|
||||
*/
|
||||
class UserModal extends Model
|
||||
class UserModel extends BaseModel
|
||||
{
|
||||
//
|
||||
protected $table = 'users';
|
||||
@@ -22,7 +22,7 @@ class UserModal extends Model
|
||||
*/
|
||||
public function projects(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectModal::class, 'user_id', 'id');
|
||||
return $this->hasMany(ProjectModel::class, 'user_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,7 +31,7 @@ class UserModal extends Model
|
||||
*/
|
||||
public function projectsAuthor(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectModal::class, 'author_id', 'id');
|
||||
return $this->hasMany(ProjectModel::class, 'author_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,6 +40,6 @@ class UserModal extends Model
|
||||
*/
|
||||
public function roles(): HasOne
|
||||
{
|
||||
return $this->hasOne(RoleModal::class, 'id', 'role_id');
|
||||
return $this->hasOne(RoleModel::class, 'id', 'role_id');
|
||||
}
|
||||
}
|
||||
107
app/Service/LoginService.php
Normal file
107
app/Service/LoginService.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Models\UserModel;
|
||||
use App\Service\common\JWTService;
|
||||
use App\Service\common\UtilsService;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class LoginService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var mixed 单例实例,确保该类只有一个全局实例
|
||||
*/
|
||||
private static mixed $_instance;
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
|
||||
public function login($account, $password)
|
||||
{
|
||||
$userModel = UserModel::where('account', $account)->first();
|
||||
if (empty($userModel)) {
|
||||
return $this->register($account, $password);
|
||||
}
|
||||
|
||||
if (!password_verify($password, $userModel->password)) {
|
||||
UtilsService::getInstance()->errorThrow('用户或密码错误!');
|
||||
}
|
||||
|
||||
if ($userModel->ip !== get_ip()) {
|
||||
$ipTable = json_decode($userModel->ip_table, true);
|
||||
if (!in_array(get_ip(), $ipTable)) {
|
||||
$ipTable[] = get_ip();
|
||||
}
|
||||
$update = UserModel::where('id', $userModel->id)->update([
|
||||
'ip' => get_ip(),
|
||||
'ip_table' => json_encode($ipTable),
|
||||
'updated_at' => get_time()
|
||||
]);
|
||||
|
||||
if (!$update) {
|
||||
UtilsService::getInstance()->errorThrow('更新失败!');
|
||||
}
|
||||
|
||||
$userModel = UserModel::where('id', $userModel->id)->first();
|
||||
}
|
||||
$result = [
|
||||
'id' => $userModel->id,
|
||||
'account' => $userModel->account,
|
||||
'nick_name' => $userModel->nick_name,
|
||||
'avatar' => $userModel->avatar,
|
||||
'ip' => $userModel->ip,
|
||||
'ip_table' => $userModel->ip_table,
|
||||
];
|
||||
$token = JWTService::getInstance()->generateToken($result);
|
||||
$result['token'] = $token;
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function register($account, $password)
|
||||
{
|
||||
$userModel = UserModel::where('account', $account)->first();
|
||||
if ($userModel) {
|
||||
UtilsService::getInstance()->errorThrow('账号已被占用!');
|
||||
}
|
||||
|
||||
$createModel = UserModel::create([
|
||||
'account' => $account,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT),
|
||||
'nick_name' => '新用户'. Str::random(),
|
||||
'avatar' => 'https://pic.rmb.bdstatic.com/bjh/80852bfe7c321988191838517ba64e309354.jpeg@h_1280',
|
||||
'ip' => get_ip(),
|
||||
'ip_table' => json_encode([get_ip()]),
|
||||
'created_at' => get_time()
|
||||
]);
|
||||
|
||||
if (!$createModel) {
|
||||
UtilsService::getInstance()->errorThrow('注册失败!');
|
||||
}
|
||||
|
||||
$result = [
|
||||
'id' => $createModel->id,
|
||||
'account' => $createModel->account,
|
||||
'nick_name' => $createModel->nick_name,
|
||||
'avatar' => $createModel->avatar,
|
||||
'ip' => $createModel->ip,
|
||||
'ip_table' => $createModel->ip_table,
|
||||
];
|
||||
$token = JWTService::getInstance()->generateToken($result);
|
||||
$result['token'] = $token;
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
10
app/Service/UserService.php
Normal file
10
app/Service/UserService.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\BaseApp\BaseService;
|
||||
|
||||
class UserService extends BaseService
|
||||
{
|
||||
|
||||
}
|
||||
112
app/Service/common/JWTService.php
Executable file
112
app/Service/common/JWTService.php
Executable file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Exception;
|
||||
|
||||
class JWTService
|
||||
{
|
||||
private static mixed $_instance;
|
||||
private string $secretKey = 'cc_spa_jwt_secret_key'; // 请替换为你的密钥
|
||||
|
||||
private string $token;
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 Token
|
||||
* @return $this|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getToken(): null|static
|
||||
{
|
||||
$token = request()->bearerToken();
|
||||
if (empty($token)) return UtilsService::getInstance()->notAuth('请先登录');
|
||||
$this->token = $token;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 JWT Token
|
||||
* @param array $data 要嵌入到 token 中的数据
|
||||
* @return string
|
||||
*/
|
||||
public function generateToken(array $data): string
|
||||
{
|
||||
$issuedAt = time();
|
||||
$expirationTime = $issuedAt + config('xk.redis.jwt_ttl');
|
||||
|
||||
$payload = [
|
||||
'iat' => $issuedAt,
|
||||
'exp' => $expirationTime,
|
||||
'data' => $data
|
||||
];
|
||||
|
||||
return JWT::encode($payload, $this->secretKey, 'HS256');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 JWT Token
|
||||
* @return object|null 返回解码后的 payload 或者 null 如果验证失败
|
||||
* @throws Exception
|
||||
*/
|
||||
public function parseToken(): ?object
|
||||
{
|
||||
try {
|
||||
if (empty($this->token)) return UtilsService::getInstance()->notAuth('请先登录');
|
||||
return JWT::decode($this->token, new Key($this->secretKey, 'HS256'));
|
||||
} catch (Exception $e) {
|
||||
return UtilsService::getInstance()->notAuth('【1】Token解析失败!请重新登录:'. $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解析 JWT Token
|
||||
* @return array|null 返回解码后的 payload 或者 null 如果验证失败
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getUserInfo(): ?array
|
||||
{
|
||||
try {
|
||||
$jwt = $this->parseToken();
|
||||
$user = RedisService::getInstance()->init(config('xk.redis.jwt'))->get($jwt->data->id);
|
||||
if (empty($user)) return UtilsService::getInstance()->notAuth('登录状态过期');
|
||||
return json_decode($user, true);
|
||||
} catch (Exception $e) {
|
||||
return UtilsService::getInstance()->notAuth('【2】Token解析失败!请重新登录:'. $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 续签 JWT Token
|
||||
* @return string|null 新的 JWT 字符串或者 null 如果原 token 已过期或无效
|
||||
* @throws Exception
|
||||
*/
|
||||
public function refreshToken(): ?string
|
||||
{
|
||||
$decoded = $this->parseToken();
|
||||
if ($decoded === null || !property_exists($decoded, 'data')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->generateToken((array)$decoded->data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
94
app/Service/common/RedisService.php
Executable file
94
app/Service/common/RedisService.php
Executable file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\common;
|
||||
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
class RedisService
|
||||
{
|
||||
|
||||
private static mixed $_instance;
|
||||
|
||||
private $prefix = 'default';
|
||||
|
||||
private $redis;
|
||||
|
||||
/**
|
||||
* 获取实例
|
||||
* @return null|static
|
||||
*/
|
||||
public static function getInstance(): null|static
|
||||
{
|
||||
$name = get_called_class();
|
||||
if (!isset(self::$_instance[$name])) {
|
||||
self::$_instance[$name] = new static();
|
||||
}
|
||||
|
||||
return self::$_instance[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
* @param string $prefix
|
||||
* @return RedisService
|
||||
*/
|
||||
public function init(string $prefix = 'default'): static
|
||||
{
|
||||
$this->redis = Redis::class;
|
||||
$this->prefix = $prefix;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置缓存
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @param int $expire
|
||||
* @return true
|
||||
*/
|
||||
public function set($key, $value, int $expire = 0): true
|
||||
{
|
||||
$this->redis::set($this->prefix . $key, $value);
|
||||
if ($expire > 0) {
|
||||
$this->redis::expire($this->prefix . $key, $expire);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 累加
|
||||
* @param $key
|
||||
* @param int $value
|
||||
* @param int $expire
|
||||
* @return true
|
||||
*/
|
||||
public function incr($key, int $value = 1, int $expire = 0): true
|
||||
{
|
||||
$this->redis::incr($this->prefix . $key, $value);
|
||||
if ($expire > 0) {
|
||||
$this->redis::expire($this->prefix . $key, $expire);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key): mixed
|
||||
{
|
||||
return $this->redis::get($this->prefix . $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缓存
|
||||
* @param $key
|
||||
* @return bool
|
||||
*/
|
||||
public function del($key): bool
|
||||
{
|
||||
return $this->redis::del($this->prefix . $key);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
namespace App\Service\common;
|
||||
|
||||
use App\Enum\ErrorEnum;
|
||||
use DateTime;
|
||||
use Exception;
|
||||
use Illuminate\Routing\Route;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Random\RandomException;
|
||||
use ReflectionClass;
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
<?php
|
||||
|
||||
use App\Enum\ErrorEnum;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Foundation\Configuration\Exceptions;
|
||||
use Illuminate\Foundation\Configuration\Middleware;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
|
||||
|
||||
return Application::configure(basePath: dirname(__DIR__))
|
||||
->withRouting(
|
||||
@@ -15,5 +20,58 @@ return Application::configure(basePath: dirname(__DIR__))
|
||||
//
|
||||
})
|
||||
->withExceptions(function (Exceptions $exceptions) {
|
||||
//
|
||||
$exceptions->render(function (Throwable $e) {
|
||||
// 判断异常是否为Exception类型
|
||||
if ($e instanceof \Exception) {
|
||||
// \App\Models\new\log\ErrorLogModel::create([
|
||||
// 'type' => 0,
|
||||
// 'content' => json_encode([
|
||||
// 'message' => $e->getMessage(),
|
||||
// 'line' => $e->getLine(),
|
||||
// 'file' => $e->getFile(),
|
||||
// 'trace' => $e->getTraceAsString(),
|
||||
// ]),
|
||||
// 'created_at' => time()
|
||||
// ]);
|
||||
// 使用switch语句判断异常类型
|
||||
switch (true) { // 注意这里使用了 'true' 因为 PHP 不允许在 case 中使用表达式
|
||||
// 如果异常是NotFoundHttpException类型
|
||||
case $e instanceof NotFoundHttpException:
|
||||
// 返回一个自定义的JSON响应,状态码为404,错误信息为"路由未找到"
|
||||
return jInstanceof(ErrorEnum::NOT_FOUND, '路由未找到', $e->getMessage());
|
||||
|
||||
// 如果异常是MethodNotAllowedHttpException类型
|
||||
case $e instanceof MethodNotAllowedHttpException:
|
||||
Log::error($e->getMessage());
|
||||
// 返回一个自定义的JSON响应,状态码为405,错误信息为"请求方式错误"
|
||||
return jInstanceof(405, '请求方式错误', $e->getMessage());
|
||||
|
||||
// 如果异常是UnprocessableEntityHttpException类型
|
||||
case $e instanceof UnprocessableEntityHttpException:
|
||||
Log::warning($e->getMessage());
|
||||
// 返回一个自定义的JSON响应,状态码为422,错误信息为"服务器处理不过来啦"
|
||||
return jInstanceof(422, '服务器处理不过来啦', $e->getMessage());
|
||||
|
||||
// 如果异常是HttpException类型
|
||||
case $e instanceof HttpException:
|
||||
Log::error($e->getMessage());
|
||||
// 返回一个自定义的JSON响应,状态码为异常的状态码,错误信息为"服务器出错啦"
|
||||
return jInstanceof($e->getCode(), ErrorEnum::FAIL->description(), 'ERROR:'. $e->getMessage());
|
||||
|
||||
// 对于所有其他类型的异常,默认返回500内部服务器错误
|
||||
default:
|
||||
Log::critical($e->getMessage()); // 记录更严重的日志级别
|
||||
// 返回一个自定义的JSON响应,状态码为500,错误信息为"内部服务器错误"
|
||||
return jInstanceof($e->getCode(), $e->getMessage(), $e->getCode());
|
||||
}
|
||||
}
|
||||
|
||||
return jInstanceof(ErrorEnum::FAIL, '服务器出错啦~', [
|
||||
'message' => $e->getMessage(),
|
||||
'code' => $e->getCode(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'trace' => $e->getTrace(),
|
||||
]);
|
||||
});
|
||||
})->create();
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": "^8.2",
|
||||
"firebase/php-jwt": "^6.11",
|
||||
"laravel/framework": "^12.0",
|
||||
"laravel/tinker": "^2.10.1"
|
||||
"laravel/tinker": "^2.10.1",
|
||||
"ext-http": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"fakerphp/faker": "^1.23",
|
||||
|
||||
65
composer.lock
generated
65
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "88970a0117c062eed55fa8728fc43833",
|
||||
"content-hash": "bb43516eae1e73202940191fe341f64a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "brick/math",
|
||||
@@ -510,6 +510,69 @@
|
||||
],
|
||||
"time": "2025-03-06T22:45:56+00:00"
|
||||
},
|
||||
{
|
||||
"name": "firebase/php-jwt",
|
||||
"version": "v6.11.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/firebase/php-jwt.git",
|
||||
"reference": "d1e91ecf8c598d073d0995afa8cd5c75c6e19e66"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/d1e91ecf8c598d073d0995afa8cd5c75c6e19e66",
|
||||
"reference": "d1e91ecf8c598d073d0995afa8cd5c75c6e19e66",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^8.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"guzzlehttp/guzzle": "^7.4",
|
||||
"phpspec/prophecy-phpunit": "^2.0",
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"psr/cache": "^2.0||^3.0",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-factory": "^1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-sodium": "Support EdDSA (Ed25519) signatures",
|
||||
"paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Firebase\\JWT\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"BSD-3-Clause"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Neuman Vong",
|
||||
"email": "neuman+pear@twilio.com",
|
||||
"role": "Developer"
|
||||
},
|
||||
{
|
||||
"name": "Anant Narayanan",
|
||||
"email": "anant@php.net",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
|
||||
"homepage": "https://github.com/firebase/php-jwt",
|
||||
"keywords": [
|
||||
"jwt",
|
||||
"php"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/firebase/php-jwt/issues",
|
||||
"source": "https://github.com/firebase/php-jwt/tree/v6.11.1"
|
||||
},
|
||||
"time": "2025-04-09T20:32:01+00:00"
|
||||
},
|
||||
{
|
||||
"name": "fruitcake/php-cors",
|
||||
"version": "v1.3.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Service\UtilsService;
|
||||
use App\Service\common\UtilsService;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
@@ -11,4 +11,5 @@ Route::get('/', function () {
|
||||
* 自动注册路由
|
||||
*/
|
||||
UtilsService::class::getInstance()->autoRouteRegister([
|
||||
'' => \App\Http\Controllers\LoginController::class, // 登录控制器
|
||||
]);
|
||||
|
||||
@@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
|
||||
$framework = \App\Models\ProjectModal::with([
|
||||
$framework = \App\Models\ProjectModel::with([
|
||||
'frameworks',
|
||||
'labels',
|
||||
'technologyStacks',
|
||||
|
||||
Reference in New Issue
Block a user