|
|
@ -19,31 +19,10 @@ async function login (ctx, next) { |
|
|
|
attributes: { exclude: ['password'] }, |
|
|
|
where: { |
|
|
|
username: params.username, |
|
|
|
password: password, |
|
|
|
del: false, |
|
|
|
}, |
|
|
|
}); |
|
|
|
} else if (params.phone && params.code) { |
|
|
|
const record = await models.PhoneValidateCode.findOne({ |
|
|
|
where: { |
|
|
|
phone: phone, |
|
|
|
code: code |
|
|
|
} |
|
|
|
}); |
|
|
|
if (!record) { |
|
|
|
throw '验证码错误' |
|
|
|
} else if (record.expired < new Date()) { |
|
|
|
throw '验证码已失效' |
|
|
|
} |
|
|
|
userRes = await models.User.findOne({ |
|
|
|
attributes: { exclude: ['password'] }, |
|
|
|
where: { |
|
|
|
phone: phone, |
|
|
|
del: false, |
|
|
|
password: password |
|
|
|
}, |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (userRes) { |
|
|
|
if (userRes.forbidden) { |
|
|
|
throw '用户已禁用' |
|
|
@ -79,56 +58,6 @@ async function login (ctx, next) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function varfiyCode (ctx) { |
|
|
|
try { |
|
|
|
const { models } = ctx.fs.dc; |
|
|
|
const { pushBySms, pushByEmail } = ctx.app.fs.utils |
|
|
|
const { phone, sig, r } = ctx.request.body |
|
|
|
|
|
|
|
// 伪造的请求可能由相同的sig参数组成
|
|
|
|
const checkSigUsed = await models.PhoneValidateCode.findOne({ |
|
|
|
where: { sig: sig } |
|
|
|
}); |
|
|
|
if (checkSigUsed) { |
|
|
|
throw '参数错误!' |
|
|
|
} |
|
|
|
|
|
|
|
// 验证sig正确性
|
|
|
|
const checkSig = Hex.stringify(SHA1(phone + r)); |
|
|
|
if (!r || !sig || sig != checkSig) { |
|
|
|
throw '参数错误!' |
|
|
|
} |
|
|
|
|
|
|
|
let varifyCode = '' |
|
|
|
for (let i = 0; i < 6; i++) { |
|
|
|
varifyCode += Math.floor(Math.random() * 10) |
|
|
|
} |
|
|
|
|
|
|
|
// await pushBySms({
|
|
|
|
// phone: phone,
|
|
|
|
// templateCode: 'SMS_248250074',
|
|
|
|
// templateParam: {
|
|
|
|
// code: varifyCode
|
|
|
|
// },
|
|
|
|
// })
|
|
|
|
|
|
|
|
await models.PhoneValidateCode.create({ |
|
|
|
phone: phone, |
|
|
|
code: varifyCode, |
|
|
|
sig: sig, |
|
|
|
expired: moment().add(10, 'minutes').format('YYYY-MM-DD HH:mm:ss') |
|
|
|
}) |
|
|
|
|
|
|
|
ctx.status = 204; |
|
|
|
} catch (error) { |
|
|
|
ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); |
|
|
|
ctx.status = 400; |
|
|
|
ctx.body = { |
|
|
|
message: typeof error == 'string' ? error : '获取验证码失败' |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async function logout (ctx) { |
|
|
|
try { |
|
|
|
const models = ctx.fs.dc.models; |
|
|
@ -152,6 +81,5 @@ async function logout (ctx) { |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
login, |
|
|
|
varfiyCode, |
|
|
|
logout, |
|
|
|
}; |