Files
ZYZ/shared/pubUtils/httpUtil.ts
2021-11-08 17:47:30 +08:00

166 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import * as request from "request-promise";
import * as crypto from 'crypto'
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 包名
*/
export async function reportOnline(userCode: string, packageName: string) {
if (!packageName || packageName == '') {
packageName = 'com.bantu.nfsg'
}
let result = await vidHttpRequest(BANTU_VID_ADDR.REPORT_ONLINE, {
account: userCode,
package: packageName
});
if (result && result.code !== 1) {
console.error(result.msg);
}
return result;
}
/**
* 实名认证 暂时不使用
* @param name 真实姓名
* @param idNum 身份证号
* @param userCode 账号
* @param packageName 包名
*/
export async function authenticate(name: string, idNum: string, userCode: string, packageName: string) {
if (!packageName || packageName == '') {
packageName = 'com.bantu.nfsg'
}
let result = await vidHttpRequest(BANTU_VID_ADDR.IDCARD, {
account: userCode,
cardno: idNum,
name,
appkey: BANTU_VID_APP_KEY,
package: packageName
});
if (result && result.code !== 1) {
console.error(result.msg);
}
return result && result.code == 1;
}
export async function vidHttpRequest(addr: string, body: any) {
body['sign'] = getVidObjSign(body);
console.log('body: ', JSON.stringify(body))
let options = {
url: BANTU_VID_ADDR.HOST + addr,
method: 'POST',
body: body,
json: true
}
try {
let res = await request(options);
let check = checkVidObjSign(res);
if (!check) return false;
// 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);
}
function checkVidObjSign(obj: any) {
return checkMd5Sign(obj, treatVid);
}
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;
}