packServer/backend/scripts/set-password.js
liufumingren bd2363bf00 chore: 初始化仓库(backend / frontend / extension 三块)
- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权;
  已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF),
  开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。
  单测 91 个通过(不需要本机数据库)。
- frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 /
  首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。
- extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。
- 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、
  .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
2026-10-17 08:50:32 +08:00

65 lines
2.1 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。
*/
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 = ?, 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, status) values (?, ?, ?, 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);
});