UNPKG

@scu-xiaochuan/scu-pecourse-utils

Version:

65 lines 1.93 kB
import { app_key, getSign, app_secret, getTimestamp } from '../utils'; export class ApiService { url = 'http://211.83.159.5:8086'; headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0 WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', Origin: 'http://211.83.159.5:81', Referer: 'http://211.83.159.5:81/', }; constructor(token) { if (token) { this.initAuth(token); } } initAuth(token) { this.headers.Authorization = `bearer ${token}`; } async get({ path, params }) { const url = `${this.url}${path}`; const timestamp = getTimestamp(); const query = { app_key, timestamp, sign: getSign({ path, appsecret: app_secret, timestamp, }), ...params, }; const qs = new URLSearchParams(query).toString(); const req = await fetch(`${url}?${qs}`, { method: 'GET', headers: { ...this.headers, }, }); return req.json(); } async post({ path, data }) { const url = `${this.url}${path}`; const timestamp = getTimestamp(); const query = { app_key, timestamp, sign: getSign({ path, appsecret: app_secret, timestamp, data, }), ...data, }; const req = await fetch(`${url}`, { headers: { ...this.headers, 'Content-Type': 'application/x-www-form-urlencoded', }, method: 'POST', body: new URLSearchParams(query).toString(), }); return req.json(); } } export const apiService = new ApiService(); //# sourceMappingURL=apiService.js.map