修改了登录验证方式
This commit is contained in:
@@ -74,34 +74,49 @@ class HandlerUser {
|
|||||||
return ResponseUtil.error(ctx, "微信登录失败,未获取到 openid", null, 400);
|
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 (!user) {
|
||||||
if (!phoneNumber) {
|
if (!phoneNumber) {
|
||||||
return ResponseUtil.badRequest(ctx, "缺少手机号");
|
return ResponseUtil.badRequest(ctx, "缺少手机号");
|
||||||
}
|
}
|
||||||
|
|
||||||
const newUser = {
|
user = await DBModel.User.findOne({ "profile.mobile": phoneNumber });
|
||||||
profile: { name: name || phoneNumber, mobile: phoneNumber, },
|
if (!user) {
|
||||||
social: {
|
const newUser = {
|
||||||
wechat: { openid: openid },
|
profile: { name: name || phoneNumber, mobile: phoneNumber, },
|
||||||
},
|
status: { account: "normal", },
|
||||||
status: { account: "normal", },
|
app: {},
|
||||||
app: {},
|
};
|
||||||
};
|
newUser.app[appId] = { role: ["user"], wxopenid: openid };
|
||||||
newUser.app[appId] = { role: ["user"], };
|
|
||||||
|
|
||||||
user = await DBModel.User.setUser(newUser);
|
user = await DBModel.User.setUser(newUser);
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
if (user) {
|
||||||
if (phoneNumber && phoneNumber.length > 0 && user.profile.mobile !== phoneNumber) {
|
if (phoneNumber && phoneNumber.length > 0 && user.profile.mobile !== phoneNumber) {
|
||||||
user.profile.mobile = phoneNumber;
|
user.profile.mobile = phoneNumber;
|
||||||
}
|
}
|
||||||
if (!(appId in user.app)) {
|
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, "用户不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await this.genToken(user._id.toString());
|
// 更新Token
|
||||||
user.security.token = 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);
|
user.security.tokenExpiry = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
||||||
await user.save();
|
await user.save();
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ const UserSchema = mongoose.Schema(
|
|||||||
name: { type: String, comment: '用户姓名' },
|
name: { type: String, comment: '用户姓名' },
|
||||||
pinyin: { type: String, default: '', comment: '姓名的拼音,用于搜索' },
|
pinyin: { type: String, default: '', comment: '姓名的拼音,用于搜索' },
|
||||||
pinyinFL: { 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: '电子邮箱' },
|
email: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '电子邮箱' },
|
||||||
idnumber: { 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: '社保卡号' },
|
ssn: { type: String, index: true, trim: true, unique: true, sparse: true, comment: '社保卡号' },
|
||||||
@@ -58,7 +58,6 @@ const UserSchema = mongoose.Schema(
|
|||||||
wechat:{
|
wechat:{
|
||||||
account: { type: String, default: "", comment: '微信账号' },
|
account: { type: String, default: "", comment: '微信账号' },
|
||||||
unionid: { type: String, index: true, unique: true, sparse: true, comment: '微信UnionID' },
|
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: '账户状态' },
|
account: { type: String, enum: ["normal", "lock"], default: "normal", comment: '账户状态' },
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// 带wxopenid
|
||||||
app: {
|
app: {
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user