67 行
3.3 KiB
PHP
67 行
3.3 KiB
PHP
<?php
|
|
// CLI only. First run schema, then publish files, then run with "menu".
|
|
if (PHP_SAPI !== 'cli') exit;
|
|
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
|
|
chdir('/www/wwwroot/grain.gynhc.com');
|
|
define('APP_PATH', getcwd() . '/application/');
|
|
require getcwd() . '/thinkphp/base.php';
|
|
\think\App::initCommon();
|
|
$prefix = config('database.prefix');
|
|
if (!preg_match('/^[a-zA-Z0-9_]+$/D', $prefix)) throw new Exception('Invalid prefix');
|
|
$backup = $argv[1];
|
|
if (!is_dir($backup)) throw new Exception('Backup directory required');
|
|
if (($argv[2] ?? '') !== 'menu') {
|
|
$sql = str_replace('`fa_', '`' . $prefix, file_get_contents(__DIR__ . '/20261028_grain_forecast.sql'));
|
|
foreach (preg_split('/;\s*(?:\r?\n|$)/', trim($sql)) as $statement) {
|
|
if (trim($statement)) \think\Db::execute($statement);
|
|
}
|
|
echo "FORECAST_SCHEMA_READY\n";
|
|
exit;
|
|
}
|
|
$rules = \think\Db::name('auth_rule')->select();
|
|
$groups = \think\Db::name('auth_group')->select();
|
|
$snapshot = $backup . '/forecast-auth-backup.json';
|
|
if (!file_exists($snapshot)) {
|
|
file_put_contents($snapshot, json_encode(['rules' => $rules, 'groups' => $groups], JSON_UNESCAPED_UNICODE));
|
|
chmod($snapshot, 0600);
|
|
}
|
|
\think\Db::transaction(function () use ($rules, $groups) {
|
|
$testing = \think\Db::name('auth_rule')->where('name', 'testing')->find();
|
|
$nodes = [
|
|
'forecast' => ['温湿度预测与实测', 1],
|
|
'forecast/index' => ['查看记录', 0],
|
|
'forecast/detail' => ['查看对比', 0],
|
|
'forecast/add' => ['录入温湿度并预测', 0],
|
|
'forecast/retry' => ['重新生成预测', 0],
|
|
'forecast/actual' => ['补录真实监测值', 0],
|
|
];
|
|
$ids = [];
|
|
foreach ($nodes as $name => $info) {
|
|
$id = \think\Db::name('auth_rule')->where('name', $name)->value('id');
|
|
if (!$id) {
|
|
$id = \think\Db::name('auth_rule')->insertGetId([
|
|
'name' => $name, 'title' => $info[0], 'ismenu' => $info[1], 'type' => 'file',
|
|
'pid' => $info[1] ? ($testing ? $testing['pid'] : 0) : $ids['forecast'],
|
|
'icon' => $info[1] ? 'fa fa-line-chart' : 'fa fa-circle-o', 'menutype' => 'addtabs',
|
|
'weigh' => $info[1] && $testing ? $testing['weigh'] - 1 : 0, 'status' => 'normal',
|
|
'createtime' => time(), 'updatetime' => time(),
|
|
]);
|
|
}
|
|
$ids[$name] = $id;
|
|
}
|
|
$oldIds = [];
|
|
foreach ($rules as $rule) $oldIds[$rule['name']] = (string)$rule['id'];
|
|
foreach ($groups as $group) {
|
|
if ($group['rules'] === '*') continue;
|
|
$granted = explode(',', $group['rules']);
|
|
$canRead = false;
|
|
foreach (['testing', 'testing/index'] as $name) if (isset($oldIds[$name]) && in_array($oldIds[$name], $granted, true)) $canRead = true;
|
|
$canWrite = isset($oldIds['testing/add']) && in_array($oldIds['testing/add'], $granted, true);
|
|
if (!$canRead && !$canWrite) continue;
|
|
$extra = [$ids['forecast'], $ids['forecast/index'], $ids['forecast/detail']];
|
|
if ($canWrite) $extra = array_merge($extra, [$ids['forecast/add'], $ids['forecast/retry'], $ids['forecast/actual']]);
|
|
\think\Db::name('auth_group')->where('id', $group['id'])->update(['rules' => implode(',', array_unique(array_merge($granted, $extra)))]);
|
|
}
|
|
});
|
|
echo "FORECAST_MENU_READY\n";
|