Files
work_dhd_back_end/app/controller/MatchController.php
2026-01-06 13:58:03 +08:00

123 lines
3.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\service\MatchService;
use think\response\Json;
/**
* 岗位简历匹配度控制器
*/
class MatchController extends BaseController
{
/**
* 计算岗位和简历的匹配度
* @return Json
*/
public function calculate(): Json
{
try {
// 获取请求参数支持JSON和表单数据
$input = $this->request->param();
$position = $input['position'] ?? [];
$resume = $input['resume'] ?? [];
// 如果是JSON请求尝试从JSON中获取
if (empty($position) && empty($resume)) {
$jsonData = json_decode($this->request->getContent(), true);
if (is_array($jsonData)) {
$position = $jsonData['position'] ?? [];
$resume = $jsonData['resume'] ?? [];
}
}
// 参数验证
if (empty($position) || empty($resume)) {
return json([
'code' => 400,
'msg' => '参数错误:岗位信息和简历信息不能为空',
'data' => null
]);
}
// 计算匹配度
$matchService = new MatchService();
$score = $matchService->calculateMatchScore($position, $resume);
return json([
'code' => 200,
'msg' => '计算成功',
'data' => [
'match_score' => $score
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '计算失败:' . $e->getMessage(),
'data' => null
]);
}
}
/**
* 批量匹配查询(基于数据库)
* @return Json
*/
public function batchMatch(): Json
{
try {
// 获取请求参数
$input = $this->request->param();
$userId = $input['user_id'] ?? 0;
// 如果是JSON请求尝试从JSON中获取
if (empty($userId)) {
$jsonData = json_decode($this->request->getContent(), true);
if (is_array($jsonData)) {
$userId = $jsonData['user_id'] ?? 0;
}
}
// 参数验证
if (empty($userId) || !is_numeric($userId)) {
return json([
'code' => 400,
'msg' => '参数错误user_id不能为空且必须为数字',
'data' => null
]);
}
// 分页参数
$page = (int)($input['page'] ?? $jsonData['page'] ?? 1);
$pageSize = (int)($input['page_size'] ?? $jsonData['page_size'] ?? 20);
$filterZero = (bool)($input['filter_zero'] ?? $jsonData['filter_zero'] ?? false);
// 验证分页参数
if ($page < 1) $page = 1;
if ($pageSize < 1 || $pageSize > 100) $pageSize = 20; // 限制每页最多100条
// 批量匹配查询
$matchService = new MatchService();
$result = $matchService->batchMatchFromDb((int)$userId, $page, $pageSize, $filterZero);
return json([
'code' => 200,
'msg' => '查询成功',
'data' => $result
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => '查询失败:' . $e->getMessage(),
'data' => null
]);
}
}
}