packServer/backend/scripts/set-password.js
liufumingren 43dae198db chore: Android 扫码台设置页、发布脚本健壮性与文档同步
- Android 扫码台:设置页新增「设为默认桌面 / 解除默认桌面 / 回到盒子桌面 / 打开系统设置」等现场免 adb 操作
- android_client/build.ps1:gradle 的 javac 提示走 stderr,改成 2>&1 逐行 Write-Host,避免被当成构建失败
- deploy-lib.cjs:ssh 加 ServerAliveInterval/CountMax 与 -n,避免发布脚本卡在远端命令结束前不退出
- set-password.js:重置密码时补写 password_changed_at(否则旧 JWT 不失效)
- .gitignore:忽略 video_split/(调试用的视频拆帧产物)
- 文档同步:README / backend README(微信网关、成员扫码绑定、平台管理员、顶栏下载入口、已下线的自建微信表)/
  database README / desktop README

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:21:41 +08:00

71 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 创建用户 / 重置登录密码(本机没装 mysql 客户端也能用)
*
* 用法:
* npm run user:pass -- <账号> <密码> [昵称]
* node scripts/set-password.js admin 123456 管理员
*
* 账号不存在时自动创建(username = 账号,status = 1);
* 账号存在时只重置密码。密码经 bcrypt 哈希后写入 users.password_hash。
*
* ⚠️ 重置密码时**同时写 `password_changed_at`**:与 app/service/user.js#setPassword 一致,
* 这样改密前签发的旧 JWT 会立即失效(middleware/api_auth.js 比对 `iat`)。
*/
const mysql = require('mysql2/promise');
const bcrypt = require('bcryptjs');
const resolveDbConfig = require('./db-config');
/** 与 app/service/auth.js 的 BCRYPT_ROUNDS 保持一致 */
const BCRYPT_ROUNDS = 10;
function usage() {
console.log('用法: npm run user:pass -- <账号> <密码> [昵称]');
console.log('示例: npm run user:pass -- admin 123456 管理员');
}
async function main() {
const [ account, password, nickname ] = process.argv.slice(2);
if (!account || !password) {
usage();
process.exit(1);
}
const hash = await bcrypt.hash(String(password), BCRYPT_ROUNDS);
const config = resolveDbConfig();
const conn = await mysql.createConnection(config);
try {
const [rows] = await conn.query(
'select id, username, phone, email from users where (username = ? or phone = ? or email = ?) and deleted_at is null limit 1',
[ account, account, account ],
);
if (rows.length) {
const user = rows[0];
await conn.query(
'update users set password_hash = ?, password_changed_at = now(), updated_at = now() where id = ?',
[ hash, user.id ],
);
console.log(`[user:pass] 已重置密码: ${user.username || user.phone || user.email} (id=${user.id})`);
} else {
const [result] = await conn.query(
'insert into users (username, nickname, password_hash, password_changed_at, status) values (?, ?, ?, now(), 1)',
[ account, nickname || account, hash ],
);
console.log(`[user:pass] 已创建用户: ${account} (id=${result.insertId}),昵称: ${nickname || account}`);
}
console.log(`[user:pass] 连接: ${config.host}:${config.port}/${config.database} as ${config.user}`);
} finally {
await conn.end();
}
}
main().catch((err) => {
console.error(`[user:pass] 失败: ${err.code || ''} ${err.message}`);
process.exit(1);
});