修改了登录验证方式

This commit is contained in:
lik
2026-06-01 21:16:23 +08:00
parent 40f2c21bfa
commit ca147ac2a8
2 changed files with 33 additions and 18 deletions

View File

@@ -74,34 +74,49 @@ class HandlerUser {
return ResponseUtil.error(ctx, "微信登录失败,未获取到 openid", null, 400);
}
let user = await DBModel.User.findOne({ "social.wechat.openid": openid });
// 使用openid和phoneNumber查询用户
let key = `app.${appId}.wxopenid`;
let user = await DBModel.User.findOne({ [key]: openid });
if (!user) {
if (!phoneNumber) {
return ResponseUtil.badRequest(ctx, "缺少手机号");
}
user = await DBModel.User.findOne({ "profile.mobile": phoneNumber });
if (!user) {
const newUser = {
profile: { name: name || phoneNumber, mobile: phoneNumber, },
social: {
wechat: { openid: openid },
},
status: { account: "normal", },
app: {},
};
newUser.app[appId] = { role: ["user"], };
newUser.app[appId] = { role: ["user"], wxopenid: openid };
user = await DBModel.User.setUser(newUser);
} else {
}
}
//
if (user) {
if (phoneNumber && phoneNumber.length > 0 && user.profile.mobile !== phoneNumber) {
user.profile.mobile = phoneNumber;
}
if (!(appId in user.app)) {
user.app[appId].role = ["user"];
user.app[appId] = { role: ["user"], wxopenid: openid };
}
user.app[appId].wxopenid = openid;
} else {
return ResponseUtil.internalError(ctx, "用户不存在");
}
// 更新Token
const isTokenValid = user.security.token &&
user.security.tokenExpiry &&
new Date() < user.security.tokenExpiry;
if (!isTokenValid) {
const token = await this.genToken(user._id.toString());
user.security.token = token;
}
user.security.tokenExpiry = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
await user.save();

View File

@@ -15,7 +15,7 @@ const UserSchema = mongoose.Schema(
name: { type: String, comment: '用户姓名' },
pinyin: { type: String, default: '', comment: '姓名的拼音,用于搜索' },
pinyinFL: { type: String, default: '', comment: '姓名拼音的首字母,用于搜索' },
mobile: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '手机号码' },
mobile: { type: String, index: true, trim: true, comment: '手机号码' },
email: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '电子邮箱' },
idnumber: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '身份证号码' },
ssn: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '社保卡号' },
@@ -58,7 +58,6 @@ const UserSchema = mongoose.Schema(
wechat:{
account: { type: String, default: "", comment: '微信账号' },
unionid: { type: String, index: true, unique: true, sparse: true, comment: '微信UnionID' },
openid: { type: String, index: true, unique: true, sparse: true, comment: '微信OpenID' },
}
},
@@ -67,6 +66,7 @@ const UserSchema = mongoose.Schema(
account: { type: String, enum: ["normal", "lock"], default: "normal", comment: '账户状态' },
},
// 带wxopenid
app: {
},