2026-02-25 15:22:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
namespace app\controller;
|
2026-02-25 15:22:12 +08:00
|
|
|
|
2026-02-25 18:09:04 +08:00
|
|
|
use app\service\FeishuService;
|
|
|
|
|
use app\util\JwtUtil;
|
|
|
|
|
use app\model\User;
|
|
|
|
|
use app\model\Company;
|
2026-02-25 15:22:12 +08:00
|
|
|
use Webman\Http\Request;
|
|
|
|
|
use Webman\Http\Response;
|
|
|
|
|
|
|
|
|
|
class AuthController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 用户登录
|
|
|
|
|
*/
|
|
|
|
|
public function login(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$username = $request->post('username');
|
|
|
|
|
$password = $request->post('password');
|
|
|
|
|
|
|
|
|
|
// TODO: 实现真实的登录逻辑
|
|
|
|
|
if (!$username || !$password) {
|
|
|
|
|
return $this->error('用户名或密码不能为空');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 模拟用户数据(用于测试)
|
|
|
|
|
$user = User::first();
|
|
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return $this->error('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$token = JwtUtil::encode([
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'company_id' => $user->company_id,
|
|
|
|
|
'username' => $user->name,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'user' => $user,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 飞书免登
|
|
|
|
|
*/
|
|
|
|
|
public function feishuLogin(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$authCode = $request->post('auth_code');
|
|
|
|
|
$tenantKey = $request->post('tenant_key');
|
|
|
|
|
|
|
|
|
|
if (!$authCode) {
|
|
|
|
|
return $this->error('缺少授权码');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$feishuService = new FeishuService();
|
|
|
|
|
|
|
|
|
|
// 根据 tenant_key 获取公司
|
|
|
|
|
$company = $feishuService->getCompanyByTenantKey($tenantKey);
|
|
|
|
|
|
|
|
|
|
if (!$company) {
|
|
|
|
|
return $this->error('公司不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置公司并获取用户信息
|
|
|
|
|
$feishuService->setCompany($company->id);
|
|
|
|
|
$feishuData = $feishuService->getUserInfo($authCode);
|
|
|
|
|
|
|
|
|
|
// 创建或更新用户
|
|
|
|
|
$user = $feishuService->createOrUpdateUser($feishuData, $company->id);
|
|
|
|
|
|
|
|
|
|
// 生成 JWT Token
|
|
|
|
|
$token = JwtUtil::encode([
|
|
|
|
|
'user_id' => $user->id,
|
|
|
|
|
'company_id' => $user->company_id,
|
|
|
|
|
'feishu_user_id' => $user->feishu_user_id,
|
|
|
|
|
'union_id' => $user->union_id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
return $this->success([
|
|
|
|
|
'token' => $token,
|
|
|
|
|
'user' => $user->load('company'),
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->error($e->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取当前用户信息
|
|
|
|
|
*/
|
|
|
|
|
public function me(Request $request): Response
|
|
|
|
|
{
|
|
|
|
|
$userId = $this->getUserId($request);
|
|
|
|
|
|
|
|
|
|
if (!$userId) {
|
|
|
|
|
return $this->error('未登录', 401);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = User::with('company')->find($userId);
|
|
|
|
|
|
|
|
|
|
if (!$user) {
|
|
|
|
|
return $this->error('用户不存在');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->success($user);
|
|
|
|
|
}
|
|
|
|
|
}
|