Files
cunkebao_v3/Server/script/migration/backfill_kefu_privilege_ids.php
Manus AI 5517457929 sync: 以本地为准同步全量变更至 GitHub
含四端需求文档、前端/后端/超管/触客宝迭代及部署脚本更新;未拉取远程。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-30 04:24:56 +08:00

92 lines
2.6 KiB
PHP

<?php
/**
* 历史客服 privilegeIds 补全脚本
*
* 问题:早期 s2_company_account.privilegeIds 为 [] / 空,触客宝登录缺 S2 菜单
* 默认集与 CustomerServiceController::create 一致
*
* 用法:
* docker compose exec -T server php /var/www/html/script/migration/backfill_kefu_privilege_ids.php
* docker compose exec -T server php /var/www/html/script/migration/backfill_kefu_privilege_ids.php apply=1
* docker compose exec -T server php /var/www/html/script/migration/backfill_kefu_privilege_ids.php companyId=2130 apply=1
*
* 卡若 2026-05-29 · 存客宝_待办 §4.1
*/
declare(strict_types=1);
require __DIR__ . '/../../thinkphp/base.php';
use think\Db;
use think\App;
$app = new App(realpath(__DIR__ . '/../../application'));
$app->initialize();
$argMap = [];
foreach ($argv as $arg) {
if (strpos($arg, '=') !== false) {
list($k, $v) = explode('=', $arg, 2);
$argMap[trim($k)] = trim($v);
}
}
$apply = isset($argMap['apply']) && $argMap['apply'] === '1';
$companyId = isset($argMap['companyId']) ? (int)$argMap['companyId'] : 0;
/** 与 CustomerServiceController::DEFAULT_KEFU_PRIVILEGE_IDS 保持一致 */
$defaultIds = [
1001, 1002, 1004, 1023, 1406,
20003, 20021, 20022, 20023, 20032, 20041, 20049, 20054, 20055, 20060,
20100, 20102, 20107,
];
$defaultJson = json_encode($defaultIds, JSON_UNESCAPED_UNICODE);
$query = Db::table('s2_company_account')->where('status', 0);
if ($companyId > 0) {
$query->where('departmentId', $companyId);
}
$rows = $query
->field('id, departmentId, userName, privilegeIds')
->select();
$targets = [];
foreach ($rows ?: [] as $row) {
$raw = trim((string)($row['privilegeIds'] ?? ''));
$decoded = $raw === '' ? [] : (json_decode($raw, true) ?: []);
if (!is_array($decoded) || count($decoded) === 0) {
$targets[] = $row;
}
}
echo sprintf("[backfill_kefu_privilege_ids] mode=%s companyFilter=%d targets=%d\n",
$apply ? 'APPLY' : 'DRY-RUN',
$companyId,
count($targets)
);
foreach ($targets as $row) {
$line = sprintf(
" id=%d company=%d user=%s privilegeIds=%s",
(int)$row['id'],
(int)$row['departmentId'],
(string)$row['userName'],
(string)($row['privilegeIds'] ?? '')
);
echo $line . "\n";
if ($apply) {
Db::table('s2_company_account')->where('id', (int)$row['id'])->update([
'privilegeIds' => $defaultJson,
'updateTime' => time(),
]);
}
}
if (!$apply && count($targets) > 0) {
echo "\n追加 apply=1 执行写入\n";
}
exit(0);