79 lines
3.0 KiB
PHP
79 lines
3.0 KiB
PHP
<?php
|
||
/**
|
||
* 企业列表接口 GET/POST /api/company/list.php
|
||
* v1.0.18 检索改造:
|
||
* - field + keyword 联动:field=具体字段(如 name_zh/registration_number/…),keyword 只匹配该字段;field 为空=全部字段
|
||
* - industry:行业下拉(精确匹配)
|
||
* - company_type:企业类型(companies.legal_form,取自 data_dict_company_type)
|
||
* 参数:page / limit / keyword / field / industry / company_type / is_active
|
||
* 返回:{ list, total, page, limit }
|
||
*/
|
||
require_once __DIR__ . '/../common/db.php';
|
||
require_once __DIR__ . '/../common/response.php';
|
||
require_once __DIR__ . '/../common/auth.php';
|
||
|
||
checkPermission('company');
|
||
|
||
[$page, $limit] = pageParams();
|
||
$keyword = trim($_REQUEST['keyword'] ?? '');
|
||
$field = trim($_REQUEST['field'] ?? '');
|
||
$industry = trim($_REQUEST['industry'] ?? '');
|
||
$companyType = trim($_REQUEST['company_type'] ?? '');
|
||
$active = isset($_REQUEST['is_active']) && $_REQUEST['is_active'] !== '' ? (int)$_REQUEST['is_active'] : null;
|
||
|
||
// 可指定筛选的字段白名单(下拉选项来自前端「筛选」)
|
||
$searchableFields = [
|
||
'name_zh', 'name_en', 'registration_number', 'legal_representative',
|
||
'address', 'industry', 'industry_subdivision', 'website',
|
||
'source_channel', 'source_detail', 'business_scope', 'stock_code',
|
||
];
|
||
|
||
$where = ['is_active = 1'];
|
||
$params = [];
|
||
if ($active !== null) {
|
||
$where = ['is_active = ?'];
|
||
$params = [$active];
|
||
}
|
||
if ($keyword !== '') {
|
||
if ($field !== '' && in_array($field, $searchableFields, true)) {
|
||
$where[] = "$field LIKE ?";
|
||
$params[] = "%$keyword%";
|
||
} else {
|
||
// 全部字段:匹配常用字段
|
||
$where[] = '(name_zh LIKE ? OR name_en LIKE ? OR display_name LIKE ? OR registration_number LIKE ? OR legal_representative LIKE ? OR industry LIKE ? OR industry_subdivision LIKE ? OR website LIKE ? OR source_detail LIKE ? OR stock_code LIKE ?)';
|
||
$like = "%$keyword%";
|
||
array_push($params, $like, $like, $like, $like, $like, $like, $like, $like, $like, $like);
|
||
}
|
||
}
|
||
if ($industry !== '') {
|
||
$where[] = 'industry = ?';
|
||
$params[] = $industry;
|
||
}
|
||
if ($companyType !== '') {
|
||
$where[] = 'legal_form = ?';
|
||
$params[] = $companyType;
|
||
}
|
||
$whereSql = implode(' AND ', $where);
|
||
|
||
$pdo = DB::getInstance()->getPdo();
|
||
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM companies WHERE $whereSql");
|
||
$stmt->execute($params);
|
||
$total = (int)$stmt->fetchColumn();
|
||
|
||
$offset = ($page - 1) * $limit;
|
||
$stmt = $pdo->prepare(
|
||
"SELECT id, name_zh, name_en, display_name, business_role, legal_form, country, registration_number,
|
||
address, legal_representative, industry, industry_subdivision, website,
|
||
latest_employee_count, latest_annual_revenue, is_listed, stock_code,
|
||
source_channel, source_detail, is_active, created_at, updated_at
|
||
FROM companies
|
||
WHERE $whereSql
|
||
ORDER BY id DESC
|
||
LIMIT $limit OFFSET $offset"
|
||
);
|
||
$stmt->execute($params);
|
||
$list = $stmt->fetchAll();
|
||
|
||
Response::success(['list' => $list, 'total' => $total, 'page' => $page, 'limit' => $limit]);
|