从request模块切换到needle

pull/166/head
lyswhut 2020-02-15 18:08:04 +08:00
parent d557149326
commit cde380782f
2 changed files with 29 additions and 2 deletions

View File

@ -24,7 +24,7 @@ export const getToken = () => new Promise((resolve, reject) => {
if (window.kw_token.isGetingToken) return wait(1000).then(() => getToken().then(token => resolve(token)))
if (window.kw_token.token) return resolve(window.kw_token.token)
window.kw_token.isGetingToken = true
httpGet('http://www.kuwo.cn', (err, resp) => {
httpGet('http://www.kuwo.cn/', (err, resp) => {
window.kw_token.isGetingToken = false
if (err) return reject(err)
if (resp.statusCode != 200) return reject(new Error('获取失败'))

View File

@ -1,4 +1,4 @@
import request from 'request'
import needle from 'needle'
// import progress from 'request-progress'
import { debugRequest } from './env'
import { requestMsg } from './message'
@ -7,6 +7,33 @@ import { deflateRawSync } from 'zlib'
import { getProxyInfo } from './index'
// import fs from 'fs'
const request = (url, options, callback) => {
let data
if (options.method == 'get') options.headers['Content-Length'] = 0
if (options.body) {
data = options.body
} else if (options.form) {
data = options.form
// data.content_type = 'application/x-www-form-urlencoded'
options.json = false
} else if (options.formData) {
data = options.formData
// data.content_type = 'multipart/form-data'
options.json = false
}
return needle.request(options.method || 'get', url, data, options, (err, resp, body) => {
if (!err) {
body = resp.body = resp.raw.toString()
try {
resp.body = JSON.parse(resp.body)
} catch (_) {}
body = resp.body
}
callback(err, resp, body)
}).request
}
const defaultHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
}