feat: 邀请码
This commit is contained in:
@@ -68,28 +68,31 @@ module.exports = {
|
||||
signUpReqInviter: inject(function(SysConfig) {
|
||||
const { JWT_SECRET, PWD_HASH_SALT } = SysConfig;
|
||||
return async (req, res) => {
|
||||
let { phone, code, inviter, password } = req.body;
|
||||
if (!phone || !code || !password || !inviter) return res.json({
|
||||
let { phone, code, inviteCode, password } = req.body;
|
||||
if (!phone || !code || !password || !inviteCode) return res.json({
|
||||
code: 1,
|
||||
msg: 'Missing required parameters'
|
||||
});
|
||||
const validInviter = await User.findById(inviter).catch(err => null);
|
||||
const invitePhone = '+86-' + parseInt(inviteCode);
|
||||
const validInviter = await User.findOne({ phone: invitePhone }).catch(null);
|
||||
if (!validInviter) return res.json({
|
||||
code: 4,
|
||||
msg: '邀请人不存在'
|
||||
});
|
||||
const isCodeValid = await Code.verifyCode({ id: phone, code, type: CODE_TYPE['signUp'] });
|
||||
password = Crypto.hash(password, { salt: PWD_HASH_SALT });
|
||||
const newInviteCode = typeof phone === 'string' && parseInt(phone.split('-')[1]).toString(36);
|
||||
if (isCodeValid) {
|
||||
await createNewUser({
|
||||
phone,
|
||||
inviter,
|
||||
password
|
||||
inviter: validInviter.id,
|
||||
password,
|
||||
inviteCode: newInviteCode
|
||||
}).then(user => {
|
||||
User.findByIdAndUpdate(inviter, { $push: { group: user.id } }).catch(err => {
|
||||
mylog.error('将被邀请人加入到邀请人队列失败', err && err.errmsg);
|
||||
User.findByIdAndUpdate(validInviter.id, { $push: { group: user.id } }).catch(err => {
|
||||
mylog.error('将被邀请人加入到邀请人队列失败 uid', user.id, err && err.errmsg);
|
||||
});
|
||||
new Fund({userId: user.id }).save().catch(err => mylog.error('用户资金文档创建失败', err && err.errmsg));
|
||||
new Fund({userId: user.id }).save().catch(err => mylog.error('用户资金文档创建失败 uid', user.id, err && err.errmsg));
|
||||
const token = createToken({
|
||||
userId: user.id
|
||||
}, JWT_SECRET, {});
|
||||
|
||||
Reference in New Issue
Block a user