gengx
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 邀请码与对应后台权限迁移(可重复执行)。
|
||||
* 用法: php database/migrate_invitation_codes.php
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$app = new think\App();
|
||||
$app->initialize();
|
||||
|
||||
use app\service\PermissionCatalog;
|
||||
use think\facade\Db;
|
||||
|
||||
function invitationTableExists(string $table): bool
|
||||
{
|
||||
$db = Db::getConfig('database') ?: env('DB_NAME', 'ai_chat');
|
||||
$rows = Db::query(
|
||||
'SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? LIMIT 1',
|
||||
[$db, $table]
|
||||
);
|
||||
return !empty($rows);
|
||||
}
|
||||
|
||||
try {
|
||||
if (!invitationTableExists('invitation_codes')) {
|
||||
Db::execute("CREATE TABLE invitation_codes (
|
||||
id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
code VARCHAR(32) NOT NULL UNIQUE,
|
||||
department_id INT UNSIGNED NULL,
|
||||
created_by INT UNSIGNED NULL,
|
||||
used_by INT UNSIGNED NULL,
|
||||
status ENUM('active','used','revoked') NOT NULL DEFAULT 'active',
|
||||
expires_at TIMESTAMP NULL,
|
||||
used_at TIMESTAMP NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_invitation_status (status),
|
||||
INDEX idx_invitation_department (department_id),
|
||||
INDEX idx_invitation_created (created_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");
|
||||
echo "Created table invitation_codes\n";
|
||||
}
|
||||
|
||||
if (invitationTableExists('sys_permissions')) {
|
||||
$parent = Db::name('sys_permissions')->where('code', 'dir:org')->find();
|
||||
if ($parent) {
|
||||
$menu = Db::name('sys_permissions')->where('code', 'menu:invitations')->find();
|
||||
if (!$menu) {
|
||||
$menuId = Db::name('sys_permissions')->insertGetId([
|
||||
'type' => 'menu',
|
||||
'code' => 'menu:invitations',
|
||||
'name' => '邀请码管理',
|
||||
'parent_id' => (int) $parent['id'],
|
||||
'path' => '/invitations',
|
||||
'icon' => 'ticket',
|
||||
'sort_order' => 25,
|
||||
'is_system' => 1,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
$menu = ['id' => $menuId];
|
||||
echo "Added menu:invitations\n";
|
||||
}
|
||||
|
||||
foreach ([
|
||||
['btn:invitation:create', '生成邀请码'],
|
||||
['btn:invitation:revoke', '作废邀请码'],
|
||||
] as [$code, $name]) {
|
||||
if (!Db::name('sys_permissions')->where('code', $code)->find()) {
|
||||
Db::name('sys_permissions')->insert([
|
||||
'type' => 'btn',
|
||||
'code' => $code,
|
||||
'name' => $name,
|
||||
'parent_id' => (int) $menu['id'],
|
||||
'sort_order' => 0,
|
||||
'is_system' => 1,
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
echo "Added {$code}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (invitationTableExists('roles')) {
|
||||
Db::name('roles')->where('slug', 'super_admin')->update([
|
||||
'permissions' => json_encode(PermissionCatalog::fullPermissions(), JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
echo "Invitation migration completed.\n";
|
||||
} catch (\Throwable $e) {
|
||||
echo 'Error: ' . $e->getMessage() . "\n";
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user