54 lines
2.0 KiB
PHP
54 lines
2.0 KiB
PHP
<?php
|
|
/**
|
|
* 渠道新增/编辑接口 POST /api/channel/save.php
|
|
* 入参:id(编辑时必传)/ channel_name / channel_type / contact_person / contact_phone / contact_email / efficiency_score / remark
|
|
* 数据源:channels 表(见 sql/system_tables.sql 第5节)
|
|
*/
|
|
require_once __DIR__ . '/../common/db.php';
|
|
require_once __DIR__ . '/../common/response.php';
|
|
require_once __DIR__ . '/../common/auth.php';
|
|
require_once __DIR__ . '/../common/logger.php';
|
|
require_once __DIR__ . '/../common/helpers.php';
|
|
|
|
checkAjax();
|
|
checkPermission('channel');
|
|
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
$channelName = trim($_POST['channel_name'] ?? '');
|
|
if ($channelName === '') {
|
|
Response::error('渠道名称为必填项', 400);
|
|
}
|
|
|
|
$fields = [
|
|
'channel_name' => $channelName,
|
|
'channel_type' => trim($_POST['channel_type'] ?? '') ?: null,
|
|
'contact_person' => trim($_POST['contact_person'] ?? '') ?: null,
|
|
'contact_phone' => trim($_POST['contact_phone'] ?? '') ?: null,
|
|
'contact_email' => trim($_POST['contact_email'] ?? '') ?: null,
|
|
'remark' => trim($_POST['remark'] ?? '') ?: null,
|
|
];
|
|
$score = (float)($_POST['efficiency_score'] ?? 0);
|
|
$fields['efficiency_score'] = max(0, min(100, $score));
|
|
|
|
$pdo = DB::getInstance()->getPdo();
|
|
|
|
if ($id > 0) {
|
|
$check = $pdo->prepare("SELECT * FROM channels WHERE id = ?");
|
|
$check->execute([$id]);
|
|
$old = $check->fetch();
|
|
if (!$old) {
|
|
Response::error('渠道不存在');
|
|
}
|
|
[$sets, $params] = buildUpdate($fields);
|
|
$params[] = $id;
|
|
$pdo->prepare("UPDATE channels SET $sets WHERE id = ?")->execute($params);
|
|
logCurrent('update', 'channel', 'channels', $id, ['before' => $old, 'after' => $fields]);
|
|
Response::success(['id' => $id], '更新成功');
|
|
}
|
|
|
|
[$sql, $params] = buildInsert($fields);
|
|
$pdo->prepare("INSERT INTO channels $sql")->execute($params);
|
|
$newId = (int)$pdo->lastInsertId();
|
|
logCurrent('add', 'channel', 'channels', $newId, $fields);
|
|
Response::success(['id' => $newId], '新增成功');
|