web-server 注册、登录、获取服务器列表;game-server token 校验
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { UserModel } from './../db/User';
|
||||
import { SMS_IN_60S, SMS_CNT_LIMIT, STATUS_SUC, STATUS_WRONG_PARMS, SMS_INVALID } from './../../../shared/statusCode';
|
||||
import { smsModel } from './../db/Sms';
|
||||
import { Service } from 'egg';
|
||||
const _ = require('underscore');
|
||||
|
||||
/**
|
||||
* Test Service
|
||||
*/
|
||||
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: '手机号合法'};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取手机验证码
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户获取到手机验证码之后发送验证登录请求
|
||||
* @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 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 }};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user