#!/usr/bin/env node import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { createDbPool } from '../db.mjs'; const root = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); function loadEnvFile(filePath) { if (!fs.existsSync(filePath)) return; for (const line of fs.readFileSync(filePath, 'utf8').split('\n')) { const trimmed = line.trim(); if (!trimmed || trimmed.startsWith('#')) continue; const eq = trimmed.indexOf('='); if (eq < 0) continue; const key = trimmed.slice(0, eq).trim(); const value = trimmed.slice(eq + 1).trim(); if (!process.env[key]) process.env[key] = value; } } loadEnvFile(path.join(root, '../../.env.local')); loadEnvFile(path.join(root, '.env')); const username = process.argv[2] ?? 'admin'; const role = process.argv[3] ?? 'ops_admin'; const allowed = new Set(['none', 'reviewer', 'editor', 'ops_admin']); if (!allowed.has(role)) { console.error(`无效角色: ${role},可选: ${[...allowed].join(', ')}`); process.exit(1); } const pool = createDbPool(); const [rows] = await pool.query( `SELECT id, username, ops_role FROM h5_users WHERE username = ? LIMIT 1`, [username], ); const user = rows[0]; if (!user) { console.error(`用户不存在: ${username}`); await pool.end(); process.exit(1); } await pool.query(`UPDATE h5_users SET ops_role = ?, updated_at = ? WHERE id = ?`, [ role, Date.now(), user.id, ]); await pool.end(); console.log(`已为 ${user.username} 设置 ops_role=${role}(原值: ${user.ops_role ?? 'none'})`);