@ginstone/nga-api
Version:
214 lines (213 loc) • 7.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NgaClient = void 0;
const axios_1 = require("axios");
const NgaPhpApi_1 = require("../enums/NgaPhpApi");
const CookieUtils_1 = require("@ginstone/common-utils/dist/src/utils/CookieUtils");
const StrUtils_1 = require("@ginstone/common-utils/dist/src/utils/StrUtils");
const NgaException_1 = require("../exception/NgaException");
const ThreadBody_1 = require("../response/body/ThreadBody");
const ReadBody_1 = require("../response/body/ReadBody");
const ReadDocBody_1 = require("../response/body/ReadDocBody");
const UID_PATTERN = /ngaPassportUid=(\d+);/;
const USERNAME_PATTERN = /ngaPassportUrlencodedUname=(.+?);/;
// 令牌
const CID_PATTERN = /ngaPassportCid=(.+?);/;
function throwException(data, response) {
const url = new URL(response.request.responseURL);
if (response.status === 404 || url.pathname === '/read.php') {
throw url.search.substring(1);
}
if (!(data && data.hasOwnProperty("error"))) {
return;
}
if (data.hasOwnProperty("data")) {
if (data.data.hasOwnProperty('__MESSAGE') && !data.data.__MESSAGE[4]) {
throw new NgaException_1.NgaException(response.status, data.data.__MESSAGE);
}
}
else {
throw new NgaException_1.NgaException(response.status, [400, ...data.error]);
}
}
class NgaClient {
/**
* 构造客户端
* @param baseUrl nginx代理到官方域名的前缀
* @param cookie 从浏览器F12中复制得到的cookie
*/
constructor(baseUrl, cookie) {
this.baseUrl = baseUrl;
this.instance = axios_1.default.create({
baseURL: baseUrl,
// form-data
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
transformRequest: [
function (data) {
let ret = '';
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
}
ret = ret.substring(0, ret.lastIndexOf('&'));
return ret;
}
],
});
// 请求 拦截器
this.instance.interceptors.request.use((config) => {
config.params = Object.assign({}, config.params, { __inchst: "UTF8" });
return config;
}, error => Promise.reject(error));
// 响应 拦截器
this.instance.interceptors.response.use(response => {
const data = response.data;
throwException(data, response);
return data;
}, error => {
const response = error.response;
const data = response.data;
throwException(data, response);
return Promise.reject(error);
});
// 解析cookie
const parse = NgaClient.parseCookie(cookie);
this.userId = parse.userId;
this.username = parse.username;
this.cid = parse.cid;
this.cookie = parse.cookie;
}
/**
* 从cookie中解析userId,username,cid
* @param cookie 用户输入的cookie
*/
static parseCookie(cookie) {
let uidArr = UID_PATTERN.exec(cookie);
let nameArr = USERNAME_PATTERN.exec(cookie);
let cidArr = CID_PATTERN.exec(cookie);
if (uidArr && nameArr && cidArr) {
const userId = Number(uidArr[1]);
const username = (0, StrUtils_1.decodeGBK)(nameArr[1]);
const cid = cidArr[1];
const cookie = `ngaPassportUid=${userId}; ngaPassportUrlencodedUname=${nameArr[1]}; ngaPassportCid=${cid}; `;
return { userId, username, cid, cookie };
}
else {
throw "cookie非法";
}
}
/**
* get获取json数据
* @param api
* @param params queryString 参数
*/
getJson(api, params) {
params = Object.assign({}, params, { __output: 11 });
return this.get(api, params);
}
/**
* get请求获取网页数据
* @param api api
* @param params queryString 参数
*/
getDoc(api, params) {
params = Object.assign({}, params, { noBBCode: "" });
return this.get(api, params);
}
/**
* read.php
* @param params 参数
*/
read(params) {
return this.getJson(NgaPhpApi_1.NgaPhpApi.read, params).then(r => {
if (typeof r === 'object') {
return new ReadBody_1.ReadBody(r.data);
}
throw new NgaException_1.NgaException(200, [500, "JSON解析异常"]);
});
}
/**
* read.php 网页
* @param params 参数
*/
readDoc(params) {
return this.getDoc(NgaPhpApi_1.NgaPhpApi.read, params).then(res => {
const html = res.toString();
const doc = document.createElement("html");
doc.innerHTML = html;
const docBody = new ReadDocBody_1.ReadDocBody({ html, doc });
console.log(docBody);
return new ReadBody_1.ReadBody(docBody);
});
}
/**
* thread.php
* @param params 参数
*/
thread(params) {
return this.getJson(NgaPhpApi_1.NgaPhpApi.thread, params).then(r => new ThreadBody_1.ThreadBody(r.data));
}
/**
* forum.php
* @param params 参数
*/
forum(params) {
return this.getJson(NgaPhpApi_1.NgaPhpApi.forum, params).then(r => r.data);
}
/**
* nuke.php
* @param params 参数
*/
nuke(params) {
params = Object.assign({}, params, { __output: 11 });
return this.postJson(NgaPhpApi_1.NgaPhpApi.nuke, params, undefined).then(res => {
return res.data;
});
}
/**
* post.php
* @param params 参数
* @param data form-data
*/
post(params, data) {
return this.postJson(NgaPhpApi_1.NgaPhpApi.post, Object.assign(Object.assign({}, params), { __output: 8 }), data).then(res => res.data);
}
/**
* 请求一个回复Id的具体地址,使用to参数获取重定向地址
* @param replyId 回复id
*/
toReply(replyId) {
return this.instance.get(NgaPhpApi_1.NgaPhpApi.read + "?to=1&pid=" + replyId)
.then(res => {
console.log(res);
return res;
})
.catch(r => r);
}
/**
* post请求
* @param api api
* @param params queryString 参数
* @param data form-data 参数
*/
postJson(api, params, data) {
this.setCookie();
return this.instance.post(api, data, { params });
}
/**
* get请求
* @param api api
* @param params queryString 参数
*/
get(api, params) {
this.setCookie();
return this.instance.get(api, { params });
}
/**
* 每次请求之前设置cookie,这样可以允许多个客户端同时存在互不干扰
* @private
*/
setCookie() {
(0, CookieUtils_1.setCookies)(this.cookie, 30, this.baseUrl);
}
}
exports.NgaClient = NgaClient;