1. /*
    2. * 对Fetch的封装:让其支持params/请求主体的格式化/请求地址的公共前缀
    3. */
    4. /* const env = process.env.NODE_ENV || 'development',
    5. baseURL = '';
    6. switch (env) {
    7. case 'development':
    8. baseURL = 'http://127.0.0.1:9999';
    9. break;
    10. case 'test':
    11. baseURL = 'http://168.1.123.1:9999';
    12. break;
    13. case 'production':
    14. baseURL = 'http://api.zhufengpeixun.cn';
    15. break;
    16. } */
    17. // 公用前缀 & 默认配置
    18. let baseURL = 'http://127.0.0.1:9999',
    19. inital = {
    20. method: 'GET',
    21. params: null,
    22. body: null,
    23. headers: {
    24. 'Content-Type': 'application/x-www-form-urlencoded'
    25. },
    26. credentials: true,
    27. responseType: 'JSON',
    28. cache: 'no-cache'
    29. };
    30. // 校验是否为纯粹的对象
    31. const isPlainObject = function isPlainObject(obj) {
    32. var proto, Ctor;
    33. if (!obj || typeof obj !== "object") return false;
    34. proto = Object.getPrototypeOf(obj);
    35. if (!proto) return true;
    36. Ctor = proto.hasOwnProperty('constructor') && proto.constructor;
    37. return typeof Ctor === "function" && Ctor === Object;
    38. };
    39. // 发送数据请求
    40. const request = function request(url, config) {
    41. // 合并配置项{不要去更改inital中的内容}
    42. (config == null || typeof config !== "object") ? config = {}: null;
    43. if (config.headers && isPlainObject(config.headers)) {
    44. // 单独的给HEADERS进行深度合并
    45. config.headers = Object.assign({}, inital.headers, config.headers);
    46. }
    47. let {
    48. method,
    49. params,
    50. body,
    51. headers,
    52. credentials,
    53. responseType,
    54. cache
    55. } = Object.assign({}, inital, config);
    56. // 处理URL{格式校验 & 公共前缀 & 拼接params中的信息到URL的末尾}
    57. if (typeof url !== "string") throw new TypeError(`${url} is not an string!`);
    58. if (!/^http(s?):\/\//i.test(url)) url = baseURL + url;
    59. if (params != null) {
    60. if (isPlainObject(params)) {
    61. params = Qs.stringify(params);
    62. }
    63. url += `${url.includes('?')?'&':'?'}${params}`;
    64. }
    65. // 处理请求主体的数据格式{根据headers中的Content-Type处理成为指定的格式}
    66. if (body != null) {
    67. if (isPlainObject(body)) {
    68. let contentType = headers['Content-Type'] || 'application/json';
    69. if (contentType.includes('urlencoded')) body = Qs.stringify(body);
    70. if (contentType.includes('json')) body = JSON.stringify(body);
    71. }
    72. }
    73. // 处理credentials{如果传递的是true,我们让其为include,否则是same-origin}
    74. credentials = credentials ? 'include' : 'same-origin';
    75. // 基于fetch请求数据
    76. method = method.toUpperCase();
    77. responseType = responseType.toUpperCase();
    78. config = {
    79. method,
    80. credentials,
    81. cache,
    82. headers
    83. };
    84. /^(POST|PUT|PATCH)$/i.test(method) ? config.body = body : null;
    85. return fetch(url, config).then(function onfulfilled(response) {
    86. // 不一定是成功的:Fetch的特点的是,只要服务器有返回结果,不论状态码是多少,它都认为是成功
    87. let {
    88. status,
    89. statusText
    90. } = response;
    91. if (status >= 200 && status < 400) {
    92. // 真正成功获取数据
    93. let result;
    94. switch (responseType) {
    95. case 'TEXT':
    96. result = response.text();
    97. break;
    98. case 'JSON':
    99. result = response.json();
    100. break;
    101. case 'BLOB':
    102. result = response.blob();
    103. break;
    104. case 'ARRAYBUFFER':
    105. result = response.arrayBuffer();
    106. break;
    107. }
    108. return result;
    109. }
    110. // 应该是失败的处理
    111. return Promise.reject({
    112. code: 'STATUS ERROR',
    113. status,
    114. statusText
    115. });
    116. }).catch(function onrejected(reason) {
    117. // @1:状态码失败
    118. if (reason && reason.code === "STATUS ERROR") {
    119. switch (reason.status) {
    120. case 401:
    121. break;
    122. // ...
    123. }
    124. }
    125. // @2:断网
    126. if (!navigator.onLine) {
    127. // ...
    128. }
    129. // @3:处理返回数据格式失败
    130. // ...
    131. return Promise.reject(reason);
    132. });
    133. };
    134. export default request;

    使用:

    1. request("http://ww.xx.xx.com/asimov/subscriptions/recommended_collections")
    2. .then((response) => response.json())
    3. .then((data) => {
    4. console.log(data);
    5. });