request.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. import axios from 'axios';
  3. import { Loading } from 'element-ui';
  4. import qs from 'qs';
  5. import szjcomo from 'szjcomo-utils';
  6. import { globalConfig } from '@/service/common/config';
  7. import store from '@/store';
  8. import router from '@/router';
  9. import cache from '@/service/common/cache';
  10. /**
  11. * [reqConf 全局通用配置]
  12. * @type {Object}
  13. */
  14. const reqConf = {
  15. /**
  16. * [maxContentLength `定义http内容中允许的最大内容长度]
  17. * @type {[type]}
  18. */
  19. maxContentLength:5 * 1024 * 1024,
  20. /**
  21. * [timeout指定请求超时前的毫秒数。如果请求所用时间超过timeout,则请求将被中止请求。]
  22. * @type {Number}
  23. */
  24. timeout:20 * 1000,
  25. /**
  26. * 除非url是绝对的,否则baseURL将加在url前面。可以方便地为axios实例设置baseURL以传递相对url到该实例的方法。
  27. * @type {String}
  28. */
  29. baseURL:process.env.NODE_ENV == 'development' ? '/apis' : globalConfig.app_domain,
  30. /**
  31. * `transformResponse` allows changes to the response data to be made before it is passed to then/catch
  32. * @author szjcomo
  33. * @createTime 2020-09-12
  34. * @param {[type]} data [description]
  35. * @return {[type]} [description]
  36. */
  37. transformResponse:function(data) {
  38. try {
  39. return JSON.parse(data);
  40. } catch(err) {
  41. return data;
  42. }
  43. }
  44. };
  45. /**
  46. * [openLoading 打开加载框]
  47. * @author szjcomo
  48. * @createTime 2020-09-12
  49. * @param {Object} options [description]
  50. * @return {[type]} [description]
  51. */
  52. const openLoading = function(options = {}) {
  53. let defaults = {text:'加载中',background:'#000000cc'};
  54. return Loading.service(Object.assign(defaults,options));
  55. }
  56. /**
  57. * [closeLoading 关闭加载框]
  58. * @author szjcomo
  59. * @createTime 2020-09-12
  60. * @param {Object} options [description]
  61. * @return {[type]} [description]
  62. */
  63. const closeLoading = function(loadingInstance) {
  64. if(loadingInstance) loadingInstance.close();
  65. }
  66. /**
  67. * [getAxiosInstance 创建一个请求实例]
  68. * @author szjcomo
  69. * @createTime 2020-09-12
  70. * @param {Object} options [description]
  71. * @return {[type]} [description]
  72. */
  73. const getAxiosInstance = function(options = {}) {
  74. let instance = axios.create(Object.assign(reqConf,options));
  75. instance.interceptors.request.use(async (config) => {
  76. return config;
  77. }, (error) => {return Promise.reject(error);});
  78. instance.interceptors.response.use((response)=> {
  79. if(response && response.data && response.data.result_code == 10001) {
  80. return Promise.reject(new Error(response.data.message));
  81. } else {
  82. return Promise.resolve(response.data);
  83. }
  84. }, (error) => {
  85. return Promise.reject(error);
  86. });
  87. return instance;
  88. }
  89. /**
  90. * [szjcomoAxios 自定义请求对象]
  91. * @type {Object}
  92. */
  93. const szjcomoAxios = {
  94. /**
  95. * [get 发送get请求]
  96. * @author szjcomo
  97. * @createTime 2020-09-12
  98. * @param {[type]} url [description]
  99. * @param {Object} params [description]
  100. * @param {Object} instanceOptions [description]
  101. * @param {Boolean} loading [description]
  102. * @return {[type]} [description]
  103. */
  104. get:async function(url,params = {},instanceOptions = {},loading = true) {
  105. let loadingInstance,result;
  106. if(loading) loadingInstance = openLoading({text:'加载中'});
  107. try {
  108. let tmpUrl = url;
  109. if(params && Object.keys(params).length > 0) tmpUrl += ((tmpUrl.indexOf('?') > -1?'&':'?') + qs.stringify(params));
  110. result = await getAxiosInstance(instanceOptions).get(tmpUrl);
  111. } catch(err) {
  112. result = szjcomo.appResult(err.message,err.data,true,err.status);
  113. } finally {
  114. if(loadingInstance) closeLoading(loadingInstance);
  115. return result;
  116. }
  117. },
  118. /**
  119. * [post 发送post请求]
  120. * @author szjcomo
  121. * @createTime 2020-09-23
  122. * @param {[type]} url [description]
  123. * @param {Object} params [description]
  124. * @param {Object} instanceOptions [description]
  125. * @param {Boolean} loading [description]
  126. * @return {[type]} [description]
  127. */
  128. post:async function(url,params = {},instanceOptions = {},loading = true) {
  129. let loadingInstance,result;
  130. if(loading) loadingInstance = openLoading({text:'提交中'});
  131. try {
  132. result = await getAxiosInstance(instanceOptions).post(url,params);
  133. } catch(err) {
  134. result = szjcomo.appResult(err.message,err.data,true,err.status);
  135. } finally {
  136. if(loadingInstance) closeLoading(loadingInstance);
  137. return result;
  138. }
  139. },
  140. /**
  141. * [put 发送put请求]
  142. * @author szjcomo
  143. * @date 2020-10-22
  144. * @param {[type]} url [description]
  145. * @param {Object} params [description]
  146. * @param {Object} instanceOptions [description]
  147. * @param {Boolean} loading [description]
  148. * @return {[type]} [description]
  149. */
  150. put:async function(url,params = {},instanceOptions = {},loading = true){
  151. let loadingInstance,result;
  152. if(loading) loadingInstance = openLoading({text:'提交中'});
  153. try {
  154. result = await getAxiosInstance(instanceOptions).put(url,params);
  155. } catch(err) {
  156. result = szjcomo.appResult(err.message,err.data,true,err.status);
  157. } finally {
  158. if(loadingInstance) closeLoading(loadingInstance);
  159. return result;
  160. }
  161. },
  162. /**
  163. * [delete 发送delete请求]
  164. * @author szjcomo
  165. * @date 2020-10-22
  166. * @param {[type]} url [description]
  167. * @param {Object} params [description]
  168. * @param {Object} instanceOptions [description]
  169. * @param {Boolean} loading [description]
  170. * @return {[type]} [description]
  171. */
  172. delete:async function(url,params = {},instanceOptions = {},loading = true) {
  173. let loadingInstance,result;
  174. if(loading) loadingInstance = openLoading({text:'提交中'});
  175. try {
  176. let tmpUrl = url;
  177. if(params && Object.keys(params).length > 0) tmpUrl += ((tmpUrl.indexOf('?') > -1?'&':'?') + qs.stringify(params));
  178. result = await getAxiosInstance(instanceOptions).delete(tmpUrl);
  179. } catch(err) {
  180. result = szjcomo.appResult(err.message,err.data,true,err.status);
  181. } finally {
  182. if(loadingInstance) closeLoading(loadingInstance);
  183. return result;
  184. }
  185. },
  186. /**
  187. * [upload 实现文件上传功能]
  188. * @author szjcomo
  189. * @createTime 2020-10-06
  190. * @param {[type]} url [description]
  191. * @param {Object} params [description]
  192. * @param {Object} instanceOptions [description]
  193. * @param {Boolean} loading [description]
  194. * @return {[type]} [description]
  195. */
  196. upload:async function(url,params = {},filename = 'filename',instanceOptions = {},loading = true) {
  197. let loadingInstance,result;
  198. if(loading) loadingInstance = openLoading({text:'上传中'});
  199. try {
  200. let formData = new FormData();
  201. Object.keys(params).forEach(key => {
  202. if(key == filename) {
  203. formData.append(key,params[key],params[key].name);
  204. } else if(key != 'headers') {
  205. formData.append(key,params[key]);
  206. }
  207. })
  208. let headers = {'Content-Type': 'multipart/form-data'};
  209. if(params.headers) {
  210. Object.keys(params.headers).forEach(key => {
  211. headers[key] = params.headers[key];
  212. })
  213. }
  214. result = await getAxiosInstance(instanceOptions).request({
  215. method:'post',url:`${globalConfig.app_upload_domain}${url}`,data:formData,headers:headers
  216. });
  217. } catch(err) {
  218. result = szjcomo.appResult(err.message,err.data,true,err.status);
  219. } finally {
  220. if(loadingInstance) closeLoading(loadingInstance);
  221. return result;
  222. }
  223. }
  224. };
  225. export default szjcomoAxios;