账号登录

This commit is contained in:
2026-03-10 14:25:17 +08:00
parent cb692a8f90
commit 6dbcaecf68
5 changed files with 60 additions and 26 deletions
+2 -2
View File
@@ -67,10 +67,10 @@ class AuthController extends BaseController
try {
$data = $request->all();
if(empty($data['feishu_code']) || empty($data['app_id'])){
$data['login_type'] = $request->login_type;
if($data['login_type'] == 'feishu' && (empty($data['feishu_code']) || empty($data['app_id']))){
throw new BusinessException('参数错误');
}
$result = (new AuthService())->login($data);
return $this->success($result, '登录成功');
} catch (\Throwable $e) {
@@ -0,0 +1,32 @@
<?php
namespace app\common\controller;
use app\common\model\CorpFeishuApp;
use app\common\service\AuthService;
use app\common\validate\UserValidate;
use support\exception\BusinessException;
use support\Request;
class PresetController extends BaseController
{
/**
* 不需要头信息的方法
*/
protected $noNeedHeaderData = ['corps'];
protected $noNeedLogin = ['corps'];
// 调试
public function corps(Request $request)
{
try {
$fsAppInfo = CorpFeishuApp::where('status', 1)->select(['id','name','app_id'])->get();
return $this->success($fsAppInfo);
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
}
+16 -24
View File
@@ -23,10 +23,10 @@ class AuthLogic
public function uPwdLogin(array $data): array
{
$user = $this->validateUser($data['account'], $data['password']);
$system = $this->validateSystem($data['system_code']);
$this->validateSystemPermission($user, $system);
//$system = $this->validateSystem($data['system_code']);
//$this->validateSystemPermission($user, $system);
return $this->generateTokensAndReturn($user, $system, $data);
return $this->generateTokensAndReturn($user, $data);
}
/**
@@ -37,14 +37,14 @@ class AuthLogic
{
$feishuUserInfo = $this->getFeishuUserInfo($data);
return $feishuUserInfo;
$user = $this->getOrCreateUser($feishuUserInfo);
$system = $this->validateSystem($data['system_code']);
if ($user->isLogin) {
$this->validateSystemPermission($user, $system);
}
return $this->generateTokensAndReturn($user, $system, $data);
// $user = $this->getOrCreateUser($feishuUserInfo);
//
// $system = $this->validateSystem($data['system_code']);
// if ($user->isLogin) {
// $this->validateSystemPermission($user, $system);
// }
//
// return $this->generateTokensAndReturn($user, $system, $data);
}
/**
@@ -168,24 +168,16 @@ class AuthLogic
/**
* 生成并记录token,返回包含token的响应数据
*/
private function generateTokensAndReturn(User $user, Systems $system, array $data): array
private function generateTokensAndReturn(User $user, array $data): array
{
$payload = $this->buildJwtPayload($user, $system);
$token = $this->generateAccessToken($payload);
$refreshToken = $this->generateRefreshToken($user->id, $data['system_code']);
$homeUrl = $this->getHome($system->id, $payload);
// $refreshToken = $this->generateRefreshToken($user->id, $data['system_code']);
// 记录 token 到 Redis 和 MySQL
$this->recordUserToken($user->id, $token, $data['system_code']);
return [
'access_token' => $token,
'refresh_token' => $refreshToken,
'expires_in' => config('jwt.jwt_expiration'),
'system_code' => $data['system_code'],
'corp_abbr' => $data['corp_abbr'],
'union_id' => $user->union_id,
'role' => $user->role,
'role_wp' => $user->role_wp,
'login_type' => $data['login_type'],
'home_url' => $homeUrl,
// 'payload' => $payload,
];
}
+7
View File
@@ -46,4 +46,11 @@ class User extends Model
* 不允许自动填充模型字段
*/
protected $guarded = [];
// 账号查询的作用域
public function scopeByAccount($query, $account)
{
return $query->Where('phone', $account)
->orWhere('email', $account);
}
}
+3
View File
@@ -41,6 +41,9 @@ Route::any('/upload/delete', [app\common\controller\UploadController::class, 'de
// 公共
Route::group('/common', function () {
Route::group('/preset', function () {
Route::any('/corps', [app\common\controller\PresetController::class, 'corps']);
});
Route::group('/auth', function () {
Route::any('/login', [app\common\controller\AuthController::class, 'login']);
Route::any('/feishu/scope', [app\common\controller\AuthController::class, 'feishuScope']);