const cheer = require('cheerio'); const axios = require('axios'); const http = require('https'); const sizeOf = require('image-size'); const { log } = require('console'); const qs = require('qs'); class utils { constructor() { this.ax = axios this.ResponseInterceptor() } /** * 返回 cheerio 数据结构 * @param html * @returns {*} */ cheerio(html) { return cheer.load(html) } /** * * @param url 请求的地址 * @returns {Promise>} */ async get(url) { return await this.ax.get(url, { headers: { 'Cookie': 'UM_distinctid=1755c44df43878-0c375e3b746b17-1e1d4b58-232800-1755c44df4439a; CNZZDATA1278970723=1349859603-1603567786-%7C1605270356', 'Referer': 'https://www.tujigu.com/', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Encoding': 'gzip, deflate, br' } }); } async AgentPost(url, params = {}) { return await this.ax.post(url, qs.stringify(params), { headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'refer': '', 'TOKEN': '/NWOYjE9WKn66bBitGVVEy7XfnW2sYpjGUJR5IzndI0l9yLhb/Btqd01ln1JWBA+8e8nmuivxpJrElXCnxPSJg==', } }); } /** * * 响应拦截器 * @constructor */ async ResponseInterceptor() { this.ax.interceptors.response.use((response) => { console.log(response.config.method == 'post'); if (response.config.method == 'post') { return response.data; } else { return this.cheerio(response.data); } }, (error) => { return Promise.reject(error) }) } async GetImageWidthOrHeight(url) { return new Promise((resolve,reject) => { http.get(url, function (response) { var chunks = []; response.on('data', function (chunk) { chunks.push(chunk); }).on('end', function () { var buffer = Buffer.concat(chunks); resolve(sizeOf(buffer)) }); }); }) } } module.exports = new utils();