feat: 邀请码

This commit is contained in:
chaosBreaking
2019-09-05 14:07:55 +08:00
parent a61d6017e9
commit 005368f0d5
3 changed files with 16 additions and 10 deletions

View File

@@ -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, {});

View File

@@ -5,8 +5,8 @@ const changePwd = require('../sign/sign.controller').changePasswd;
module.exports = {
changePwd,
async createNewUser (data) {
let { phone, inviter = '', password } = data;
return await (new User({ phone, inviter, password })).save().then(user => user).catch(err => {
const { phone, inviter = '', password, inviteCode } = data;
return await (new User({ phone, inviter, password, inviteCode })).save().then(user => user).catch(err => {
mylog.error('创建新用户失败', err);
throw new Error('创建新用户失败', err.errmsg);
});

View File

@@ -19,6 +19,9 @@ const UserSchema = new mongoose.Schema({
inviter: {
type: String,
},
inviteCode: {
type: String
},
asset: {
type: {}
},