micro_request.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import axios from "axios";
  2. import errorCode from "@/utils/errorCode";
  3. axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";
  4. console.log("ip", uni.getStorageSync("labsop_ip"));
  5. const reqApi = uni.getStorageSync("labsop_ip")
  6. ? uni.getStorageSync("labsop_ip")
  7. : process.uniEnv.VUE_APP_MicroSrvProxy_API;
  8. const service = axios.create({
  9. // axios中请求配置有baseURL选项,表示请求URL公共部分
  10. baseURL: reqApi,
  11. // 超时
  12. timeout: 60000,
  13. });
  14. console.log(
  15. ' process.uniEnv.labsop_android_token")',
  16. process.uniEnv.VUE_APP_TOKEN
  17. );
  18. // request拦截器
  19. service.interceptors.request.use(
  20. (config) => {
  21. config.headers["Tenant"] = uni.getStorageSync("labsop_tenant")
  22. ? uni.getStorageSync("labsop_tenant")
  23. : process.uniEnv.VUE_APP_TENANT;
  24. // 是否需要设置 token
  25. config.headers["Authorization"] = uni.getStorageSync("labsop_android_token")
  26. ? "Bearer " + uni.getStorageSync("labsop_android_token")
  27. : "Bearer " + process.uniEnv.VUE_APP_TOKEN;
  28. return config;
  29. },
  30. (error) => {
  31. Promise.reject(error);
  32. }
  33. );
  34. // 响应拦截器
  35. service.interceptors.response.use(
  36. (res) => {
  37. if (res.request && res.request.responseType == "blob") {
  38. // 判断是否是文件流
  39. let fileReader = new FileReader();
  40. fileReader.readAsText(res.data);
  41. fileReader.onload = function () {
  42. try {
  43. let jsonData = JSON.parse(this.result); // 解析成功说明是普通对象数据,后端返回的是文件上传的错误信息
  44. res.data = jsonData;
  45. processResponse(res);
  46. return;
  47. } catch (err) {
  48. // 解析成对象失败,说明是文件流
  49. downLoadBlobFile(res);
  50. return;
  51. }
  52. };
  53. }
  54. // 常规响应处理
  55. return processResponse(res);
  56. },
  57. (error) => {
  58. uni.showToast({
  59. title: error.message,
  60. icon: "none",
  61. duration: 5 * 1000,
  62. });
  63. return Promise.reject(error);
  64. }
  65. );
  66. service.postRequest = function postRequest(basePath, srvName, funcName, data) {
  67. if (data == undefined) {
  68. let nullParam = {
  69. nullparam: 0,
  70. };
  71. data = nullParam;
  72. }
  73. var base_Path = "";
  74. if (basePath == process.uniEnv.VUE_APP_FOSHAN_PATH) {
  75. base_Path = process.uniEnv.VUE_APP_MicroSrvProxy_foshan_API + basePath;
  76. } else if (
  77. basePath == process.uniEnv.VUE_APP_AdminPath ||
  78. process.uniEnv.VUE_APP_ParentPath
  79. ) {
  80. base_Path = reqApi + basePath;
  81. } else {
  82. base_Path = reqApi + basePath;
  83. }
  84. return service.request({
  85. url: base_Path,
  86. method: "post",
  87. headers: {
  88. "Content-Type": "application/rpcx",
  89. "X-RPCX-SerializeType": "1",
  90. "X-RPCX-ServicePath": srvName,
  91. "X-RPCX-ServiceMethod": funcName,
  92. SrvEnv: "dev",
  93. },
  94. data: data,
  95. });
  96. };
  97. // 发出请求并要求把客户端信息传输给后端(IP和User-Agent)
  98. service.postRequestWithClientInfo = function postRequest(
  99. basePath,
  100. srvName,
  101. funcName,
  102. data
  103. ) {
  104. if (data == undefined) {
  105. let nullParam = { nullparam: 0 };
  106. data = nullParam;
  107. }
  108. return service.request({
  109. url: basePath,
  110. method: "post",
  111. headers: {
  112. "Content-Type": "application/rpcx",
  113. "X-RPCX-SerializeType": "1",
  114. "X-RPCX-ServicePath": srvName,
  115. "X-RPCX-ServiceMethod": funcName,
  116. "X-RPCX-Meta": "need_clint_Info=1",
  117. },
  118. data: data,
  119. });
  120. };
  121. // Excel文件下载(服务端生成文件流)
  122. service.downloadExcel = function downloadExcel(
  123. basePath,
  124. srvName,
  125. funcName,
  126. data
  127. ) {
  128. if (data == undefined) {
  129. let nullParam = {
  130. nullparam: 0,
  131. };
  132. data = nullParam;
  133. }
  134. var base_Path = "";
  135. if (basePath == process.uniEnv.VUE_APP_AdminPath) {
  136. base_Path = reqApi + basePath;
  137. } else if (basePath == process.uniEnv.VUE_APP_ParentPath) {
  138. base_Path = reqApi + basePath;
  139. }
  140. service.request({
  141. url: base_Path,
  142. method: "post",
  143. responseType: "blob",
  144. headers: {
  145. "Content-Type": "application/rpcx",
  146. "X-RPCX-SerializeType": "1",
  147. "X-RPCX-ServicePath": srvName,
  148. "X-RPCX-ServiceMethod": funcName,
  149. },
  150. data: data,
  151. });
  152. };
  153. axios.defaults.adapter = function (config) {
  154. return new Promise((resolve, reject) => {
  155. var settle = require("axios/lib/core/settle");
  156. var buildURL = require("axios/lib/helpers/buildURL");
  157. uni.request({
  158. method: config.method.toUpperCase(),
  159. url: buildURL(config.url, config.params, config.paramsSerializer),
  160. header: config.headers,
  161. data: config.data,
  162. dataType: config.dataType,
  163. responseType: config.responseType,
  164. sslVerify: config.sslVerify,
  165. complete: function complete(response) {
  166. response = {
  167. data: response.data,
  168. status: response.statusCode,
  169. errMsg: response.errMsg,
  170. header: response.header,
  171. config: config,
  172. };
  173. settle(resolve, reject, response);
  174. },
  175. });
  176. });
  177. };
  178. function processResponse(res) {
  179. // 未设置状态码则默认成功状态
  180. const code = res.data.code || 200;
  181. // 获取错误信息
  182. const message = errorCode[code] || res.data.msg || errorCode["default"];
  183. if (code === 401) {
  184. uni.showToast({
  185. title: message,
  186. icon: "none",
  187. duration: 3 * 1000,
  188. });
  189. setTimeout(() => {
  190. uni.reLaunch({
  191. url: "/pages/login/index",
  192. });
  193. }, 3000);
  194. } else if (code === 500) {
  195. uni.showToast({
  196. title: message,
  197. icon: "none",
  198. duration: 3 * 1000,
  199. });
  200. return Promise.reject(new Error(message));
  201. } else if (code !== 200) {
  202. uni.showToast({
  203. title: res.data.message ? res.data.message : message,
  204. icon: "none",
  205. duration: 3 * 1000,
  206. });
  207. return Promise.reject("error");
  208. } else {
  209. return res.data;
  210. }
  211. }
  212. function downLoadBlobFile(res, mimeType) {
  213. if (mimeType == undefined) {
  214. mimeType = "application/vnd.ms-excel";
  215. }
  216. const aLink = document.createElement("a");
  217. var blob = new Blob([res.data], {
  218. type: mimeType,
  219. });
  220. // //从response的headers中获取filename, 后端response.setHeader('Content-disposition', 'attachment filename=xxxx.docx') 设置的文件名
  221. var patt = new RegExp("filename=([^]+\\.[^\\.]+)*");
  222. var contentDisposition = decodeURI(
  223. res.headers["content-disposition"] || res.headers["Content-Disposition"]
  224. );
  225. var result = patt.exec(contentDisposition);
  226. var fileName = "data.xlsx";
  227. if (result != undefined) {
  228. fileName = result[1];
  229. fileName = decodeURI(escape(fileName));
  230. fileName = fileName.replace(/'/g, "");
  231. }
  232. aLink.href = URL.createObjectURL(blob);
  233. aLink.setAttribute("download", fileName); // 设置下载文件名称
  234. document.body.appendChild(aLink);
  235. aLink.click();
  236. document.body.appendChild(aLink);
  237. }
  238. export default service;