HttpClient.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const axios = require('axios');
  2. const axiosRetry = require('axios-retry').default;
  3. const {HttpsProxyAgent} = require("https-proxy-agent");
  4. class HttpClient {
  5. constructor(options = {}) {
  6. this.client = axios.create({
  7. timeout: options.timeout || 5000,
  8. headers: options.headers || {},
  9. validateStatus: function (status) {
  10. return status >= 200 && status < 300;
  11. }
  12. });
  13. this.setupRetry();
  14. }
  15. setupRetry() {
  16. axiosRetry(this.client, {
  17. retries: 3,
  18. retryDelay: (retryCount) => {
  19. return retryCount * 1000;
  20. },
  21. retryCondition: (error) => {
  22. return axiosRetry.isNetworkOrIdempotentRequestError(error)
  23. || error.code === 'ECONNRESET';
  24. }
  25. });
  26. }
  27. // POST 方法
  28. async post(url, data = {}, config = {}) {
  29. try {
  30. const response = await this.client.post(url, data, {
  31. headers: {
  32. 'Content-Type': 'application/json',
  33. ...config.headers
  34. },
  35. ...config
  36. });
  37. return response.data;
  38. } catch (error) {
  39. this.handleError(error);
  40. throw error;
  41. }
  42. }
  43. // POST 表单数据
  44. async postForm(url, formData, config = {}) {
  45. try {
  46. const response = await this.client.post(url, formData, {
  47. headers: {
  48. 'Content-Type': 'multipart/form-data',
  49. ...config.headers
  50. },
  51. ...config
  52. });
  53. return response.data;
  54. } catch (error) {
  55. this.handleError(error);
  56. throw error;
  57. }
  58. }
  59. async request(url, options = {}) {
  60. try {
  61. const response = await this.client.request({
  62. url,
  63. ...options
  64. });
  65. return response.data;
  66. } catch (error) {
  67. this.handleError(error);
  68. throw error;
  69. }
  70. }
  71. // POST 方法
  72. async otherPost(options,data) {
  73. try {
  74. const response = await this.client.post(options.url, data, {
  75. headers:options.headers
  76. });
  77. return response.data;
  78. } catch (error) {
  79. this.handleError(error);
  80. throw error;
  81. }
  82. }
  83. async proxyRequest(options){
  84. try{
  85. let tunnelHost = 'd528.kdltps.com'
  86. let tunnelPort = '15818'
  87. // 配置用户名和密码
  88. let username = 't13322192973984'
  89. let password = '7ntreedr'
  90. return await axios({
  91. url: options.url,
  92. method: "post",
  93. headers: options.headers,
  94. httpAgent: new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`),
  95. httpsAgent: new HttpsProxyAgent(`http://${username}:${password}@${tunnelHost}:${tunnelPort}`),
  96. }).then(
  97. res => {
  98. return res.data
  99. }
  100. ).catch(err => {
  101. console.log(err);
  102. })
  103. }catch(error){
  104. this.handleError(error);
  105. throw error;
  106. }
  107. }
  108. handleError(error) {
  109. if (error.code === 'ECONNRESET') {
  110. console.error('连接被重置');
  111. } else if (error.code === 'ETIMEDOUT') {
  112. console.error('请求超时');
  113. } else if (error.response) {
  114. console.error(`服务器响应错误: ${error.response.status}`);
  115. } else if (error.request) {
  116. console.error('未收到响应');
  117. } else {
  118. console.error('请求配置错误');
  119. }
  120. }
  121. }
  122. module.exports = HttpClient;