From cde380782fa3183a7a23267f3e73f4d688de0eb5 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 15 Feb 2020 18:08:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8Erequest=E6=A8=A1=E5=9D=97=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E5=88=B0needle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/utils/music/kw/util.js | 2 +- src/renderer/utils/request.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/renderer/utils/music/kw/util.js b/src/renderer/utils/music/kw/util.js index 854773b8..0d9f34ac 100644 --- a/src/renderer/utils/music/kw/util.js +++ b/src/renderer/utils/music/kw/util.js @@ -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('获取失败')) diff --git a/src/renderer/utils/request.js b/src/renderer/utils/request.js index 164b14ba..563630e9 100644 --- a/src/renderer/utils/request.js +++ b/src/renderer/utils/request.js @@ -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', }