fix(auth): 无微信 code 建号 openid 插 NULL——空串撞 UNIQUE 致所有无 code 新号注册失败

生产日志实证:SqliteError: UNIQUE constraint failed: users.openid(server.js:1164)
根因:SQLite 中 NULL 不参与唯一约束、'' 参与;openid||'' 在已有空串行时必撞
两处修复:手机号/微信号注册 + 邮箱 OTP 建号,openid||null
本地冒烟:连建两个无 openid 新号 2/2 OK
This commit is contained in:
2026-11-17 01:29:11 +08:00
parent 13f438c6ac
commit 7a67b64116
+2 -2
View File
@@ -991,7 +991,7 @@ app.post('/auth/email/login', async (req, res) => {
db.prepare(`
INSERT INTO users (id, has_account, openid, nickname, avatar_url, phone, position, industry, hometown, email, expertise, created_at, updated_at)
VALUES (?, 1, ?, ?, '', '', '', '', '', ?, '', ?, ?)
`).run(userId, openid || '', `用户${email.slice(0, email.indexOf('@'))}`.slice(0, 20), email, now, now);
`).run(userId, openid || null, `用户${email.slice(0, email.indexOf('@'))}`.slice(0, 20), email, now, now);
user = db.prepare('SELECT * FROM users WHERE id = ?').get(userId);
}
} else if (openid && !user.openid) {
@@ -1161,7 +1161,7 @@ app.post('/auth/login', async (req, res) => {
db.prepare(`
INSERT INTO users (id, has_account, openid, nickname, avatar_url, phone, weixin, position, industry, hometown, email, expertise, created_at, updated_at)
VALUES (?, 1, ?, ?, '', ?, ?, '', '', '', '', '', ?, ?)
`).run(userId, openid || '', isPhone ? `用户${phone.slice(-4)}` : phone, isPhone ? phone : '', isPhone ? '' : phone, now, now);
`).run(userId, openid || null, isPhone ? `用户${phone.slice(-4)}` : phone, isPhone ? phone : '', isPhone ? '' : phone, now, now);
user = db.prepare('SELECT * FROM users WHERE id = ?').get(userId);
} else {
if (openid && user.openid !== openid) {