修改代码格式;
数据库查询加 lean 判断; 其它内容微调。
This commit is contained in:
@@ -9,84 +9,95 @@ const _ = require('underscore');
|
||||
*/
|
||||
export default class Auth extends Service {
|
||||
|
||||
public checkTelNo(telNo) {
|
||||
if (!_.isString(telNo)) {
|
||||
return {status: 1, data: '参数类型错误'};
|
||||
}
|
||||
if (telNo.length !== 11) {
|
||||
return {status: 1, data: '手机号长度错误'};
|
||||
}
|
||||
return {status: 0, data: '手机号合法'};
|
||||
public checkTelNo(telNo) {
|
||||
if (!_.isString(telNo)) {
|
||||
return { status: 1, data: '参数类型错误' };
|
||||
}
|
||||
if (telNo.length !== 11) {
|
||||
return { status: 1, data: '手机号长度错误' };
|
||||
}
|
||||
return { status: 0, data: '手机号合法' };
|
||||
}
|
||||
|
||||
async sendSmsCodeByGuodu(tel, code) {
|
||||
const ctx = this.ctx;
|
||||
const url = `http://221.179.172.68:8000/QxtSms/QxtFirewall?OperID=bantu3&OperPass=c8XcTffG&DesMobile=${tel}&Content=${encodeURIComponent(`【同人游戏】验证码${code},您正在登录赵云传,若非本人操作,请勿泄露`)}&Content_Code=1`;
|
||||
const result = await ctx.curl(url, {
|
||||
method: 'GET',
|
||||
});
|
||||
return result.data;
|
||||
}
|
||||
|
||||
testLimit(sms, interval) {
|
||||
if (sms.updateTime.getTime() > Date.now() - interval) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取手机验证码
|
||||
* @param telNo - 用户手机号
|
||||
*/
|
||||
|
||||
public async getSms(tel: string) {
|
||||
|
||||
const telVerify = this.checkTelNo(tel);
|
||||
if (telVerify.status !== 0) {
|
||||
return telVerify;
|
||||
}
|
||||
|
||||
async sendSmsCodeByGuodu(tel, code) {
|
||||
const ctx = this.ctx;
|
||||
let url = `http://221.179.172.68:8000/QxtSms/QxtFirewall?OperID=bantu3&OperPass=c8XcTffG&DesMobile=${tel}&Content=${encodeURIComponent(`【同人游戏】验证码${code},您正在登录赵云传,若非本人操作,请勿泄露`)}&Content_Code=1`
|
||||
const result = await ctx.curl(url, {
|
||||
method: 'GET',
|
||||
});
|
||||
return result.data;
|
||||
const sms = await smsModel.findByTel(tel, false);
|
||||
if (sms) {
|
||||
if (await sms.timeLimit(10000)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_IN_60S);
|
||||
}
|
||||
if (await sms.cntLimit(8)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_CNT_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取手机验证码
|
||||
* @param telNo - 用户手机号
|
||||
*/
|
||||
|
||||
public async getSms(tel: string) {
|
||||
|
||||
const telVerify = this.checkTelNo(tel);
|
||||
if (telVerify.status !== 0) {
|
||||
return telVerify;
|
||||
}
|
||||
|
||||
let sms = await smsModel.findByTel(tel);
|
||||
if (!!sms) {
|
||||
if (sms.timeLimit(60000)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_IN_60S);
|
||||
}
|
||||
if (sms.cntLimit(8)) {
|
||||
return this.ctx.service.utils.exceptionResult(SMS_CNT_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
let code: string = '';
|
||||
if (!sms || !sms.used) {
|
||||
code = this.ctx.service.utils.generateNum(6);
|
||||
} else {
|
||||
code = sms.code;
|
||||
}
|
||||
|
||||
await this.sendSmsCodeByGuodu(tel, code);
|
||||
await smsModel.updateByTel(tel, code, false, new Date(), sms?.hasSendToday() ? sms.countToday + 1 : 1);
|
||||
return this.ctx.service.utils.exceptionResult(STATUS_SUC);
|
||||
let code = '';
|
||||
if (!sms || !sms.used) {
|
||||
code = this.ctx.service.utils.generateNum(6);
|
||||
} else {
|
||||
code = sms.code;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取到手机验证码之后发送验证登录请求
|
||||
* @param telNo 登录手机号
|
||||
* @param code 登录验证码
|
||||
*/
|
||||
public async smsLogin(tel: string, code: string, platform: string, pkgName: string, serverType: string) {
|
||||
const ctx = this.ctx;
|
||||
// 参数检查
|
||||
const telVerify = this.checkTelNo(tel);
|
||||
if (telVerify.status !== 0) {
|
||||
return telVerify;
|
||||
}
|
||||
if (!_.isString(code) || code.length !== 6) {
|
||||
return ctx.service.utils.exceptionResult(STATUS_WRONG_PARMS);
|
||||
}
|
||||
const smsResult = await this.sendSmsCodeByGuodu(tel, code);
|
||||
console.log(smsResult);
|
||||
await smsModel.updateByTel(tel, code, false, new Date(), sms?.hasSendToday() ? sms.countToday + 1 : 1);
|
||||
return this.ctx.service.utils.exceptionResult(STATUS_SUC);
|
||||
}
|
||||
|
||||
// 手机验证码核验
|
||||
const smsValid: boolean = await smsModel.validateSms(tel, code);
|
||||
if (!smsValid) {
|
||||
return ctx.service.utils.exceptionResult(SMS_INVALID);
|
||||
}
|
||||
|
||||
// 用户注册登录
|
||||
const token = ctx.service.utils.generateStr(256);
|
||||
const user = await UserModel.updateToken(tel, token, platform, pkgName, serverType);
|
||||
return {status: STATUS_SUC, data: { token, uid: user?.uid }};
|
||||
/**
|
||||
* 用户获取到手机验证码之后发送验证登录请求
|
||||
* @param tel 登录手机号
|
||||
* @param code 登录验证码
|
||||
* @param platform 平台
|
||||
* @param pkgName 包名
|
||||
* @param serverType 服务器类型
|
||||
*/
|
||||
public async smsLogin(tel: string, code: string, platform: string, pkgName: string, serverType: string) {
|
||||
const ctx = this.ctx;
|
||||
// 参数检查
|
||||
const telVerify = this.checkTelNo(tel);
|
||||
if (telVerify.status !== 0) {
|
||||
return telVerify;
|
||||
}
|
||||
if (!_.isString(code) || code.length !== 6) {
|
||||
return ctx.service.utils.exceptionResult(STATUS_WRONG_PARMS);
|
||||
}
|
||||
|
||||
// 手机验证码核验
|
||||
const smsValid: boolean = await smsModel.validateSms(tel, code);
|
||||
if (!smsValid) {
|
||||
return ctx.service.utils.exceptionResult(SMS_INVALID);
|
||||
}
|
||||
|
||||
// 用户注册登录
|
||||
const token = ctx.service.utils.generateStr(256);
|
||||
const user = await UserModel.updateToken(tel, token, platform, pkgName, serverType);
|
||||
return { status: STATUS_SUC, data: { token, uid: user?.uid } };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,81 +6,82 @@ const crypto = require('crypto');
|
||||
*/
|
||||
export default class TurboCore extends Service {
|
||||
|
||||
/**
|
||||
* 用户获取手机验证码
|
||||
* @param telNo - 用户手机号
|
||||
*/
|
||||
/**
|
||||
* 用户获取手机验证码
|
||||
* @param telNo - 用户手机号
|
||||
*/
|
||||
|
||||
public async getSms(telNo: string) {
|
||||
const ctx = this.ctx;
|
||||
let body = {
|
||||
telNo
|
||||
};
|
||||
const result = await ctx.curl(`${TURBO_CORE_URL}/user/getSms`, {
|
||||
method: 'POST',
|
||||
contentType: 'json',
|
||||
headers: {
|
||||
'AppId': APP_ID,
|
||||
'sign': this.getTurboSign( body, TURBO_PARM_SECRET)
|
||||
},
|
||||
data: body,
|
||||
dataType: 'json',
|
||||
});
|
||||
return result.data;
|
||||
}
|
||||
public async getSms(telNo: string) {
|
||||
const ctx = this.ctx;
|
||||
const body = {
|
||||
telNo,
|
||||
};
|
||||
const result = await ctx.curl(`${TURBO_CORE_URL}/user/getSms`, {
|
||||
method: 'POST',
|
||||
contentType: 'json',
|
||||
headers: {
|
||||
AppId: APP_ID,
|
||||
sign: this.getTurboSign(body, TURBO_PARM_SECRET),
|
||||
},
|
||||
data: body,
|
||||
dataType: 'json',
|
||||
});
|
||||
return result.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取到手机验证码之后发送验证登录请求
|
||||
* @param telNo 登录手机号
|
||||
* @param code 登录验证码
|
||||
*/
|
||||
public async smsLogin(telNo: string, code: string) {
|
||||
const ctx = this.ctx;
|
||||
let body = {
|
||||
telNo, code
|
||||
};
|
||||
const result = await ctx.curl(`${TURBO_CORE_URL}/user/getSms`, {
|
||||
method: 'POST',
|
||||
contentType: 'json',
|
||||
headers: {
|
||||
'AppId': APP_ID,
|
||||
'sign': this.getTurboSign( body, TURBO_PARM_SECRET)
|
||||
},
|
||||
data: body,
|
||||
dataType: 'json',
|
||||
});
|
||||
return result.data;
|
||||
}
|
||||
/**
|
||||
* 用户获取到手机验证码之后发送验证登录请求
|
||||
* @param telNo 登录手机号
|
||||
* @param code 登录验证码
|
||||
*/
|
||||
public async smsLogin(telNo: string, code: string) {
|
||||
const ctx = this.ctx;
|
||||
const body = {
|
||||
telNo, code,
|
||||
};
|
||||
const result = await ctx.curl(`${TURBO_CORE_URL}/user/getSms`, {
|
||||
method: 'POST',
|
||||
contentType: 'json',
|
||||
headers: {
|
||||
AppId: APP_ID,
|
||||
sign: this.getTurboSign(body, TURBO_PARM_SECRET),
|
||||
},
|
||||
data: body,
|
||||
dataType: 'json',
|
||||
});
|
||||
return result.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取多宝规则下的参数签名
|
||||
* @param params 参数列表
|
||||
* @param secret 密钥
|
||||
*/
|
||||
private getTurboSign(params, secret) {
|
||||
let paramsString = this.joinParamsStr(params);
|
||||
let stringToSign = paramsString;
|
||||
|
||||
if (secret) {
|
||||
stringToSign = `${stringToSign}&secret=${secret}`;
|
||||
return crypto.createHmac('sha256', secret)
|
||||
.update(stringToSign)
|
||||
.digest('hex');
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取多宝规则下的参数签名
|
||||
* @param params 参数列表
|
||||
* @param secret 密钥
|
||||
*/
|
||||
private getTurboSign(params, secret) {
|
||||
const paramsString = this.joinParamsStr(params);
|
||||
let stringToSign = paramsString;
|
||||
|
||||
/**
|
||||
* 将参数组合成字符串
|
||||
* @param params 参数列表
|
||||
*/
|
||||
private joinParamsStr(params) {
|
||||
let signString = Object.keys(params).filter(function (key) {
|
||||
return params[key] !== undefined && params[key] !== '' && ['pfx', 'partner_key', 'sign', 'key'].indexOf(key) < 0;
|
||||
}).sort().map(function (key) {
|
||||
return key + '=' + params[key];
|
||||
}).join("&");
|
||||
return signString;
|
||||
if (secret) {
|
||||
stringToSign = `${stringToSign}&secret=${secret}`;
|
||||
return crypto.createHmac('sha256', secret)
|
||||
.update(stringToSign)
|
||||
.digest('hex');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将参数组合成字符串
|
||||
* @param params 参数列表
|
||||
*/
|
||||
private joinParamsStr(params) {
|
||||
const signString = Object.keys(params).filter(function(key) {
|
||||
return params[key] !== undefined && params[key] !== '' && [ 'pfx', 'partner_key', 'sign', 'key' ].indexOf(key) < 0;
|
||||
}).sort()
|
||||
.map(function(key) {
|
||||
return key + '=' + params[key];
|
||||
})
|
||||
.join('&');
|
||||
return signString;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,29 +4,29 @@ const csprng = require('csprng');
|
||||
* Utils Service
|
||||
*/
|
||||
export default class Utils extends Service {
|
||||
/**
|
||||
* 生成 len 长度的随机字符串
|
||||
* @param len 长度
|
||||
* @param radix 基数
|
||||
*/
|
||||
public generateStr(len: number, radix: number = 36) {
|
||||
return csprng(len, radix);
|
||||
}
|
||||
/**
|
||||
* 生成 len 长度的随机字符串
|
||||
* @param len 长度
|
||||
* @param radix 基数
|
||||
*/
|
||||
public generateStr(len: number, radix = 36) {
|
||||
return csprng(len, radix);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定长度的随机数
|
||||
* @param len 随机数长度
|
||||
*/
|
||||
public generateNum(len: number) {
|
||||
let code = "";
|
||||
for (let i = 0; i < len; i++) {
|
||||
code += parseInt(`${Math.random() * 10}`);
|
||||
}
|
||||
return code;
|
||||
/**
|
||||
* 生成指定长度的随机数
|
||||
* @param len 随机数长度
|
||||
*/
|
||||
public generateNum(len: number) {
|
||||
let code = '';
|
||||
for (let i = 0; i < len; i++) {
|
||||
code += parseInt(`${Math.random() * 10}`);
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
public exceptionResult(status) {
|
||||
const { code, simStr} = status;
|
||||
return {status: code, data: simStr};
|
||||
}
|
||||
public exceptionResult(status) {
|
||||
const { code, simStr } = status;
|
||||
return { status: code, data: simStr };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user