Files
superlink/api/common/helpers.php
T
nanguaboss 71c6e8d9c1 v1.0.10
2026-08-03 00:07:01 +08:00

93 lines
3.0 KiB
PHP

<?php
/**
* 通用辅助函数与字段白名单(供各模块 add/update/import 复用)
*/
require_once __DIR__ . '/db.php';
require_once __DIR__ . '/response.php';
/** 企业表可写入字段白名单(key => 类型标记:s字符串/i整数/d日期/n数字) */
const COMPANY_FIELDS = [
'name_zh' => 's', 'name_en' => 's', 'display_name' => 's',
'business_role' => 's', 'country' => 's', 'registration_number' => 's',
'address' => 's', 'legal_form' => 's', 'legal_representative' => 's',
'business_scope' => 's', 'established_date' => 'd', 'registered_capital' => 's',
'industry' => 's', 'industry_subdivision' => 's',
'latest_employee_count' => 'i', 'latest_annual_revenue' => 's',
'is_listed' => 'i', 'stock_code' => 's', 'website' => 's',
];
/** 人员表可写入字段白名单 */
const PERSON_FIELDS = [
'union_id' => 's', 'full_name' => 's', 'gender' => 's', 'nationality' => 's',
'id_type' => 's', 'id_number' => 's', 'education' => 's', 'graduated_from' => 's',
'hometown' => 's', 'work_location' => 's',
];
/** 媒体(社交账号)表可写入字段白名单 */
const MEDIA_FIELDS = [
'owner_type' => 's', 'owner_id' => 'i', 'platform' => 's', 'account_id' => 's',
'profile_url' => 's', 'remark' => 's', 'is_primary' => 'i',
];
/** 媒体商业属性表可写入字段白名单 */
const MEDIA_ATTR_FIELDS = [
'account_level' => 's', 'content_categories' => 's', 'follower_count' => 'i',
'avg_read_count' => 'i', 'certification_type' => 's',
'special_requirements' => 's', 'media_remark' => 's',
];
/** 需求表可写入字段白名单 */
const NEED_FIELDS = [
'company_id' => 'i', 'contact_person' => 's', 'need_category' => 's',
'target_product_category' => 's', 'application_scenario' => 's', 'description' => 's',
];
/**
* 从 POST 中按白名单提取并清洗数据
* @param array $allowlist
* @return array
*/
function extractFields($allowlist)
{
$data = [];
foreach ($allowlist as $key => $type) {
$val = $_POST[$key] ?? null;
if ($val === null) {
continue;
}
$val = trim((string)$val);
if ($val === '') {
continue;
}
switch ($type) {
case 'i':
$data[$key] = (int)$val;
break;
case 'd':
$data[$key] = (strtotime($val) !== false) ? date('Y-m-d', strtotime($val)) : null;
break;
default:
$data[$key] = $val;
}
}
return $data;
}
/** 生成 INSERT 语句片段:字段名列表 + 占位符 */
function buildInsert($data)
{
$cols = array_keys($data);
$sql = '(`' . implode('`,`', $cols) . '`) VALUES (' . rtrim(str_repeat('?,', count($cols)), ',') . ')';
return [$sql, array_values($data)];
}
/** 生成 UPDATE SET 片段 */
function buildUpdate($data)
{
$sets = [];
foreach (array_keys($data) as $col) {
$sets[] = "`$col` = ?";
}
return [implode(',', $sets), array_values($data)];
}