sdk: 登录

This commit is contained in:
luying
2021-11-08 17:47:30 +08:00
parent a251ce47fa
commit fb6a5a785e
12 changed files with 291 additions and 60 deletions

View File

@@ -1,9 +1,65 @@
import * as request from "request-promise";
import * as crypto from 'crypto'
import { BANTU_VID_ADDR, BANTU_VID_APP_KEY } from '../consts';
import { BANTU_VID_ADDR, BANTU_VID_APP_KEY, HTTP_METHOD } from '../consts';
// 通用请求http
export async function httpRequest(url: string, method: string, headers: any, body: any) {
console.log(`httpRequest*********: ${url}, ${method}, ${JSON.stringify(headers)}, ${JSON.stringify(body)}`)
let options = {
url,
method,
headers,
body,
json: true
}
try {
let res = await request(options);
console.log('*****request result*****');
console.log(JSON.stringify(res));
return res;
} catch (e) {
console.error(e);
return false
}
}
export function md5(str: string, treatStr?: (str: string) => string) {
if(treatStr) str = treatStr(str);
console.log('*****str', str)
return crypto.createHash('md5').update(str, 'utf8').digest("hex");
};
export function getMd5ObjSign(obj: any, joinMark: string, treatStr?: (str: string) => string) {
const str = [];
Object.keys(obj).sort().forEach((key) => {
if (key == 'sign') {
} else {
str.push(key + '=' + obj[key]);
}
});
if (str.length == 0) {
console.error('check obj', obj);
}
var strs = str.join(joinMark);
return md5(strs, treatStr);
}
export async function checkMd5Sign(obj: any, treatStr?: (str: string) => string) {
if (getMd5ObjSign(obj, '', treatStr) === obj.sign) {
return true;
}
console.warn('correct sign:', getMd5ObjSign(obj, '', treatStr), obj.sign);
return false;
}
/************** 厚土防沉迷接口 **************/
/**
* 在线报告
* 在线报告 暂时不使用
* @param userCode 账号
* @param packageName 包名
*/
@@ -24,7 +80,7 @@ export async function reportOnline(userCode: string, packageName: string) {
}
/**
* 实名认证
* 实名认证 暂时不使用
* @param name 真实姓名
* @param idNum 身份证号
* @param userCode 账号
@@ -50,7 +106,7 @@ export async function authenticate(name: string, idNum: string, userCode: string
}
export async function vidHttpRequest(addr: string, body: any) {
body['sign'] = getObjSign(body);
body['sign'] = getVidObjSign(body);
console.log('body: ', JSON.stringify(body))
let options = {
@@ -62,7 +118,7 @@ export async function vidHttpRequest(addr: string, body: any) {
try {
let res = await request(options);
let check = checkSign(res);
let check = checkVidObjSign(res);
if (!check) return false;
// console.log('*****request result*****');
// console.log(JSON.stringify(res));
@@ -73,54 +129,38 @@ export async function vidHttpRequest(addr: string, body: any) {
}
}
export async function httpRequest(url: string, method: string, headers: any, body: any) {
console.log(`httpRequest*********: ${url}, ${method}, ${JSON.stringify(headers)}, ${JSON.stringify(body)}`)
let options = {
url,
method,
headers,
body,
json: true
}
try {
let res = await request(options);
console.log('*****request result*****');
console.log(JSON.stringify(res));
return res;
} catch (e) {
console.error(e);
return false
}
function getVidObjSign(body: any) {
return getMd5ObjSign(body, '', treatVid);
}
export function md5Vid(str: string) {
str = 'vId' + str;
return crypto.createHash('md5').update(str, 'utf8').digest("hex");
};
export function getObjSign(obj: any) {
const str = [];
Object.keys(obj).sort().forEach((key) => {
if (key == 'sign') {
} else {
str.push(key + '=' + obj[key]);
}
});
if (str.length == 0) {
console.error('check obj', obj);
}
var strs = str.join('');
return md5Vid(strs);
function checkVidObjSign(obj: any) {
return checkMd5Sign(obj, treatVid);
}
export async function checkSign(obj: any) {
if (getObjSign(obj) === obj.sign) {
return true;
}
console.warn('correct sign:', getObjSign(obj), obj.sign);
return false;
function treatVid(str) {
return 'vId' + str;
}
// 37sdk请求
function get37ObjSign(body: any, key: string) {
return getMd5ObjSign(body, '&', (str) => `${str}&${key}`);
}
export async function request37(url: string, body: any, key: string) {
// body = {
// clientid: 'abscddsssssss',
// pid: '37h5',
// game_id: 30,
// pst: 'MWNmY2pweWZ6dFRhZmdnZ1k4aHhINVUyZnFra1',
// time: 1583821808,
// c_game_id: 'com.a.test'
// }
// key = 'FkM619)t,P7E3yK#44q85)!Sm5cv8j'
body['sign'] = get37ObjSign(body, key);
let result = await httpRequest(url, HTTP_METHOD.POST, {}, body);
console.log('******result', result)
return result;
}

View File

@@ -0,0 +1,34 @@
import { SDK_37_ADDR, SDK_37_CONST } from '../consts';
import { request37 } from './httpUtil';
import { nowSeconds } from './timeUtil';
import { LoginValidataReturn37 } from '../domain/sdk';
/************** 37sdk **************/
export async function loginValidate37(clientId: string, pst: string) {
let result: LoginValidataReturn37 = await request37(SDK_37_ADDR.LOGIN, {
clientid: clientId,
pid: SDK_37_CONST.PID,
game_id: SDK_37_CONST.GAME_ID,
pst,
time: nowSeconds(),
c_game_id: SDK_37_CONST.FX_C_GAME_ID
}, SDK_37_CONST.LOGIN_KEY);
if (result && result.code !== 1) {
console.error(result.msg);
}
return result;
}
export async function loginValidata(channelType: string, clientId: string, pst: string) {
if(channelType == '37') {
return await loginValidate37(clientId, pst);
} else {
return false;
}
}
export function getChannelId(channelType: string, uid: number|string) {
return `${channelType}: ${uid}`;
}

View File

@@ -412,7 +412,7 @@ export function readFileAndParseJson(path: string) {
let readResult = readFile(path);
return JSON.parse(readResult);
} catch(e) {
throw new Error(`connectors.json 格式错误:${e.message}`);
throw new Error(`connectors.json 格式错误:${(<Error>e).message}`);
}
}
@@ -447,7 +447,7 @@ export function readFileAndParse(fileName: string) {
let readResult = readJsonFile(fileName);
return JSON.parse(readResult);
} catch(e) {
throw new Error(`${fileName}格式错误:${e.message}`);
throw new Error(`${fileName}格式错误:${(<Error>e).message}`);
}
}
@@ -459,7 +459,7 @@ export function readWarJsonFileAndParse() {
let json = JSON.parse(str);
result.push(json);
} catch(e) {
throw new Error(`${name}格式错误:${e.message}`);
throw new Error(`${name}格式错误:${(<Error>e).message}`);
}
}
return result;