newRequest.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import store from '../store'
  2. let service = {
  3. baseURL: store.state.baseURL, // 请求头
  4. header: {
  5. 'Content-Type': "application/x-www-form-urlencoded"
  6. },
  7. // 请求拦截器
  8. interceptors: {
  9. // 安装请求前拦截
  10. request: function(success, iserror) {
  11. service.request.success = success;
  12. service.request.error = iserror;
  13. },
  14. // 安装请求完成后拦截
  15. response: function(success, iserror) {
  16. service.response.success = success;
  17. service.response.error = iserror;
  18. }
  19. },
  20. // 请求前拦截
  21. request: {
  22. // 回调成功执行方法
  23. success: function(e) {
  24. return e
  25. },
  26. // 回调失败
  27. error: function(e) {
  28. return e
  29. }
  30. },
  31. // 请求完成后拦截
  32. response: {
  33. // 回调成功执行方法
  34. success: function(e) {
  35. return e
  36. },
  37. // 回调失败
  38. error: function(e) {
  39. return e
  40. }
  41. },
  42. // 开始请求
  43. open: async function(data) {
  44. return await new Promise(function(resolve, reject) {
  45. let requestData = ''; //保存处理完成后请求数据
  46. //console.log(data);
  47. // 请求前数据处理
  48. requestData = service.request.success(data);
  49. // 数据复制用于请求
  50. let url = '';
  51. if (requestData.url.indexOf('http') > -1) {
  52. url = requestData.url
  53. } else {
  54. url = service.baseURL + requestData.url
  55. }
  56. uni.request({
  57. url: url,
  58. method: requestData.method || 'GET',
  59. data: requestData.data,
  60. header: data.header || service.header,
  61. success: res => {
  62. if (res.statusCode == 200) {
  63. try {
  64. resolve(service.response.success(res));
  65. } catch (e) {
  66. service.response.error(e)
  67. }
  68. }
  69. },
  70. // 请求失败数据处理
  71. fail: (err) => {
  72. service.response.error(err)
  73. }
  74. });
  75. })
  76. },
  77. // 上传文件
  78. upFilse: async function(data) {
  79. return await new Promise(function(resolve, reject) {
  80. let requestData = ''; //保存处理完成后请求数据
  81. try {
  82. // 请求前数据处理
  83. requestData = service.request.success(data);
  84. } catch (e) {
  85. // 请求报错拦截
  86. service.request.error(e);
  87. }
  88. // #ifdef H5
  89. let input = event.target.querySelector('input');
  90. input.setAttribute("type", 'file');
  91. input.setAttribute("name", Object.getOwnPropertyNames(requestData.data)[0]);
  92. input.click();
  93. input.addEventListener('change', function(e) {
  94. console.log(input.files);
  95. let f = new FormData();
  96. f.append("file", input.files[0]);
  97. let xhr = new XMLHttpRequest();
  98. xhr.open(requestData.method, service.baseURL + requestData.url, true);
  99. // xhr.setRequestHeader("content-type","multipart/form-data");
  100. xhr.send(f);
  101. xhr.onreadystatechange = function() {
  102. if (xhr.readyState == 4 && xhr.status == 200) {
  103. let data = JSON.parse(xhr.responseText)
  104. resolve(service.response.success(data));
  105. } else {
  106. reject(service.response.error(xhr.status));
  107. }
  108. }
  109. })
  110. // #endif
  111. // #ifndef H5
  112. // 获取请求key值
  113. let fileName = Object.getOwnPropertyNames(requestData.data)[0];
  114. // 获取传输的图片
  115. let file = requestData.data[fileName][0];
  116. // 数据复制用于请求
  117. uni.uploadFile({
  118. url: service.baseURL + requestData.url,
  119. filePath: file,
  120. name: fileName,
  121. header: {
  122. 'Content-Type': 'multipart/form-data'
  123. },
  124. success: res => {
  125. console.log(res);
  126. // 请求成功数据处理
  127. try {
  128. resolve(service.response.success(res));
  129. } catch (e) {
  130. reject(service.response.error(e));
  131. }
  132. },
  133. // 请求失败数据处理
  134. fail: (err) => {
  135. console.log(err);
  136. reject(service.response.error(err))
  137. }
  138. });
  139. // #endif
  140. })
  141. },
  142. };
  143. export default service