request.js

  1. import axios from 'axios';
  2. const { NODE_ENV } = process.env;
  3. class AxiosRequest {
  4. // constructor公共的属性,不需要每次 new一下实例化
  5. constructor() {
  6. this.baseURL = (NODE_ENV === 'development') ? '' : '/';
  7. this.timeout = 5000; // 超时取消请求
  8. // withCredentials: true, // 默认 false
  9. this.headers = {
  10. 'Content-Type': 'application/json;charset=utf-8'
  11. }
  12. // this.loading = true; // toast
  13. this.queue = {};
  14. }
  15. request(options) {
  16. const config = {
  17. headers: this.headers,
  18. ...options,
  19. baseURL: this.baseURL,
  20. timeout: this.timeout,
  21. }
  22. const instance = axios.create(config);
  23. console.log(100, options)
  24. this.queue = {}; // 请求队列,可能有很多个请求
  25. this.setInterceptor(instance, options.url)
  26. return instance;
  27. }
  28. setInterceptor(instance, url) {
  29. console.log('instance', instance)
  30. // request请求拦截
  31. instance.interceptors.request.use(
  32. config => {
  33. // 显示 loading
  34. if(!Object.keys(this.queue).length) {
  35. this.loading = true;
  36. }
  37. // 把请求添加到队列里面
  38. this.queue[url] = url;
  39. console.log('请求拦截')
  40. return config;
  41. },
  42. error => {
  43. return Promise.reject(error);
  44. }
  45. );
  46. // response 响应拦截
  47. instance.interceptors.response.use(
  48. res => {
  49. this.removeQueue(url);
  50. console.log('response拦截', res)
  51. if(res.data.code === 0) {
  52. return res.data.data || [];
  53. }
  54. // 对返回的状态码,做各种异常处理
  55. return res;
  56. },
  57. error => {
  58. this.removeQueue(url);
  59. return Promise.reject(error);
  60. }
  61. )
  62. }
  63. // 请求成功后,从队列中删除 url
  64. removeQueue(url) {
  65. delete this.queue[url];
  66. // 如果队列为空,就删除 loading
  67. if(!Object.keys(this.queue).length) {
  68. this.loading = false;
  69. }
  70. }
  71. }
  72. export default new AxiosRequest;

index.js

  1. import $axios from './request';
  2. import { request } from 'umi';
  3. export function postId(data) {
  4. // return request('/posts', {
  5. // method: 'GET',
  6. // });
  7. // return request('/graphql', {
  8. // method: 'POST',
  9. // data: { "query": "query queryServices($duration: Duration!,$keyword: String!) {\n services: getAllServices(duration: $duration, group: $keyword) {\n key: id\n label: name\n group\n }\n }", "variables": { "duration": { "start": "2021-11-06 0242", "end": "2021-11-06 0342", "step": "MINUTE" }, "keyword": "" } }
  10. // })
  11. // return axios.post('/graphql', { "query": "query queryServices($duration: Duration!,$keyword: String!) {\n services: getAllServices(duration: $duration, group: $keyword) {\n key: id\n label: name\n group\n }\n }", "variables": { "duration": { "start": "2021-11-06 0242", "end": "2021-11-06 0342", "step": "MINUTE" }, "keyword": "" } },
  12. // {
  13. // headers: { 'Authorization': 'Basic c2t5d2Fsa2luZzpza3l3YWxraW5n' },
  14. // withCredentials: true,
  15. // },
  16. // );
  17. // return $axios.request('/graphql', {
  18. // method: 'post',
  19. // data: {
  20. // 'query': 'query queryGetAllTemplates {\n getAllTemplates(includingDisabled: false) {\n name,\n type\n configuration,\n activated,\n disabled,\n }\n }',
  21. // 'variables': {},
  22. // }
  23. // });
  24. return $axios.request({
  25. url: '/graphql',
  26. method: 'post',
  27. data: {
  28. 'query': 'query queryGetAllTemplates {\n getAllTemplates(includingDisabled: false) {\n name,\n type\n configuration,\n activated,\n disabled,\n }\n }',
  29. 'variables': {},
  30. }
  31. });
  32. }
  33. export function queryList(data) {
  34. return $axios.request('/graphql', {
  35. method: 'post',
  36. data: {
  37. 'query': 'query queryGetAllTemplates {\n getAllTemplates(includingDisabled: false) {\n name,\n type\n configuration,\n activated,\n disabled,\n }\n }',
  38. 'variables': {},
  39. },
  40. auth: {
  41. username: 'skywalking',
  42. password: 'skywalking',
  43. },
  44. });
  45. }