|
@@ -1,53 +1,99 @@
|
|
|
|
|
+import dotenv from 'dotenv';
|
|
|
|
|
+import https from 'https';
|
|
|
import axios from "axios";
|
|
import axios from "axios";
|
|
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
import { HttpsProxyAgent } from "https-proxy-agent";
|
|
|
import { Logs } from "./logs.js";
|
|
import { Logs } from "./logs.js";
|
|
|
|
|
+import { getAccountInfo } from "./getAccount.js";
|
|
|
|
|
+
|
|
|
|
|
+dotenv.config();
|
|
|
|
|
+
|
|
|
|
|
+const axiosDefaultOptions = {
|
|
|
|
|
+ baseURL: "",
|
|
|
|
|
+ url: "",
|
|
|
|
|
+ method: "GET",
|
|
|
|
|
+ headers: {},
|
|
|
|
|
+ params: {},
|
|
|
|
|
+ data: {},
|
|
|
|
|
+ timeout: 10000,
|
|
|
|
|
+};
|
|
|
|
|
|
|
|
const BaseURL = {
|
|
const BaseURL = {
|
|
|
pinnacle: "https://api.pinnacle888.com",
|
|
pinnacle: "https://api.pinnacle888.com",
|
|
|
|
|
+ ps3838: "https://api.ps3838.com",
|
|
|
pstery: "http://127.0.0.1:9055",
|
|
pstery: "http://127.0.0.1:9055",
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export const pinnacleRequest = async (options) => {
|
|
|
|
|
- const {
|
|
|
|
|
- endpoint,
|
|
|
|
|
- params = {},
|
|
|
|
|
- username,
|
|
|
|
|
- password,
|
|
|
|
|
- method = "GET",
|
|
|
|
|
- data,
|
|
|
|
|
- proxy,
|
|
|
|
|
- timeout = 10000,
|
|
|
|
|
- } = options;
|
|
|
|
|
-
|
|
|
|
|
- if (!endpoint || !username || !password) {
|
|
|
|
|
- throw new Error("endpoint、username、password is required");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+export const pinnacleRequest = async (options, channel) => {
|
|
|
|
|
|
|
|
- const authHeader = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
|
|
|
|
|
+ const { username, password, localAddress, platform } = getAccountInfo() ?? {};
|
|
|
|
|
+ const { url, ...optionsRest } = options;
|
|
|
|
|
+ if (!url || !channel && (!username || !password)) {
|
|
|
|
|
+ throw new Error("url、username、password、channel is required");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const axiosConfig = {
|
|
|
|
|
- baseURL: BaseURL.pinnacle,
|
|
|
|
|
- url: endpoint,
|
|
|
|
|
- method,
|
|
|
|
|
- headers: {
|
|
|
|
|
- "Authorization": authHeader,
|
|
|
|
|
- "Accept": "application/json",
|
|
|
|
|
- "Content-Type": "application/json",
|
|
|
|
|
- },
|
|
|
|
|
- params,
|
|
|
|
|
- data,
|
|
|
|
|
- timeout,
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ Logs.outDev('pinnacle request', { url, channel, username, password, localAddress, platform });
|
|
|
|
|
|
|
|
|
|
+ const authHeader = channel ? `Basic ${channel}` : `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
|
|
|
|
+ const axiosConfig = { ...axiosDefaultOptions, ...optionsRest, url, baseURL: BaseURL[platform] ?? BaseURL.pinnacle };
|
|
|
|
|
+ Object.assign(axiosConfig.headers, {
|
|
|
|
|
+ "Authorization": authHeader,
|
|
|
|
|
+ "Accept": "application/json",
|
|
|
|
|
+ });
|
|
|
|
|
+ const proxy = process.env.NODE_HTTP_PROXY;
|
|
|
if (proxy) {
|
|
if (proxy) {
|
|
|
axiosConfig.proxy = false;
|
|
axiosConfig.proxy = false;
|
|
|
axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
|
|
axiosConfig.httpsAgent = new HttpsProxyAgent(proxy);
|
|
|
|
|
+ // Logs.outDev('pinnacle request using proxy', axiosConfig.httpsAgent);
|
|
|
}
|
|
}
|
|
|
|
|
+ else if (localAddress) {
|
|
|
|
|
+ axiosConfig.httpsAgent = new https.Agent({ localAddress });
|
|
|
|
|
+ }
|
|
|
|
|
+ return axios(axiosConfig).then(res => {
|
|
|
|
|
+ // Logs.out('pinnacle request', url, axiosConfig, res.data);
|
|
|
|
|
+ return res.data;
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- return axios(axiosConfig).then(res => res.data);
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Pinnacle API Get请求
|
|
|
|
|
+ * @param {*} url
|
|
|
|
|
+ * @param {*} params
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+export const pinnacleGet = async (url, params, channel) => {
|
|
|
|
|
+ return pinnacleRequest({
|
|
|
|
|
+ url,
|
|
|
|
|
+ params
|
|
|
|
|
+ }, channel)
|
|
|
|
|
+ .catch(err => {
|
|
|
|
|
+ const source = { url, params };
|
|
|
|
|
+ if (err?.response?.data) {
|
|
|
|
|
+ const data = err.response.data;
|
|
|
|
|
+ Object.assign(source, { data });
|
|
|
|
|
+ }
|
|
|
|
|
+ err.source = source;
|
|
|
|
|
+ return Promise.reject(err);
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * Pinnacle API Post请求
|
|
|
|
|
+ * @param {*} url
|
|
|
|
|
+ * @param {*} data
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+export const pinnaclePost = async (url, data, channel) => {
|
|
|
|
|
+ return pinnacleRequest({
|
|
|
|
|
+ url,
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
|
|
+ },
|
|
|
|
|
+ data
|
|
|
|
|
+ }, channel);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
export const getPsteryRelations = async (mk=-1) => {
|
|
export const getPsteryRelations = async (mk=-1) => {
|
|
|
const axiosConfig = {
|
|
const axiosConfig = {
|
|
|
baseURL: BaseURL.pstery,
|
|
baseURL: BaseURL.pstery,
|
|
@@ -62,6 +108,7 @@ export const getPsteryRelations = async (mk=-1) => {
|
|
|
return axios(axiosConfig).then(res => res.data);
|
|
return axios(axiosConfig).then(res => res.data);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
export const updateBaseEvents = async (data) => {
|
|
export const updateBaseEvents = async (data) => {
|
|
|
const axiosConfig = {
|
|
const axiosConfig = {
|
|
|
baseURL: BaseURL.pstery,
|
|
baseURL: BaseURL.pstery,
|
|
@@ -83,6 +130,7 @@ export const updateBaseEvents = async (data) => {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
export const notifyException = async (message) => {
|
|
export const notifyException = async (message) => {
|
|
|
const axiosConfig = {
|
|
const axiosConfig = {
|
|
|
baseURL: BaseURL.pstery,
|
|
baseURL: BaseURL.pstery,
|