fetch2.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import Vue from 'vue'
  2. import util from '../src/assets/js/util';
  3. import '../src/assets/js/encry.min.js' //接口加密工具
  4. function initData(params) {
  5. var URLSearchParams = require('url-search-params');
  6. //360浏览器不支持URLSearchParams
  7. if (typeof URLSearchParams === "function") {
  8. var formMap = new URLSearchParams();
  9. for (var key in params) {
  10. if (typeof (params[key]) == "undefined") {
  11. params[key] = '';
  12. }
  13. formMap.append(key, params[key]);
  14. }
  15. return formMap;
  16. } else {
  17. return params;
  18. }
  19. }
  20. export default async (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET', requestType = 'FORMDATA', pathArr = [], method = 'fetch') => {
  21. type = type.toUpperCase();
  22. // 清除空参数
  23. //if(type == "GET") {
  24. // data = util.filter_param(data);
  25. //}
  26. if (process.env.NODE_ENV === 'development') {
  27. baseUrl = baseUrl
  28. } else if (process.env.NODE_ENV === 'production') {
  29. if (baseUrl) {
  30. switch (baseUrl) {
  31. case '/wuyecrm':
  32. baseUrl = '/wuyecrm'; // 物业crm云环境测试收费需要改成wuyecrm6093
  33. break;
  34. case '/iot':
  35. baseUrl = '/device'; // iot首页
  36. break;
  37. case '/datacenter':
  38. baseUrl = '/datacenter'; // 数据中心
  39. break;
  40. case '/datashow':
  41. baseUrl = '/datashow'; //综合门户
  42. break;
  43. case '/Edevice':
  44. baseUrl = '/device'; // 设备管理
  45. break;
  46. case '/fullscreen':
  47. baseUrl = '/datashow'; // 住总大屏、中启云大屏
  48. break;
  49. case '/warning':
  50. baseUrl = '/warning'; //预警管理
  51. break;
  52. case '/groupcontrol':
  53. baseUrl = '/groupcontrol'; //会议、机房进出等管理
  54. break;
  55. case '/dining':
  56. baseUrl = '/dining'; //订餐
  57. break;
  58. case '/cesesynchron':
  59. baseUrl = '/cesesynchron'; //消费管理
  60. break;
  61. case '/energy-charging':
  62. baseUrl = '/energy-charging'; //能源
  63. break;
  64. case '/activiti':
  65. baseUrl = '/activiti'; //流程
  66. break;
  67. case '/neikong':
  68. baseUrl = '/neikong'; //访问主数据
  69. break;
  70. case '/leasing':
  71. baseUrl = '/leasing'; //租赁系统
  72. break;
  73. case '/landcrm':
  74. baseUrl = '/landcrm';
  75. break;
  76. case '/wxmanage':
  77. baseUrl = '/wxmanage';
  78. break;
  79. default:
  80. baseUrl = "";
  81. break;
  82. }
  83. }
  84. }
  85. // 将wuyecrm替换成landcrm
  86. if (baseUrl === '/wuyecrm') {
  87. if (process.env.Url.indexOf('114.55.208.86') >= 0) {
  88. // 新收费云环境测试环境
  89. baseUrl = '/wuyecrm6903'
  90. } else {
  91. baseUrl = '/landcrm'
  92. }
  93. }
  94. url = baseUrl + url;
  95. if (pathArr && pathArr.length > 0) {
  96. url += "/" + pathArr.join("/");
  97. }
  98. if (type == 'GET') {
  99. let dataStr = ''; //数据拼接字符串
  100. Object.keys(data).forEach(key => {
  101. dataStr += key + '=' + data[key] + '&';
  102. })
  103. if (dataStr !== '') {
  104. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  105. url = url + '?' + dataStr + '&_t=' + new Date().getTime();
  106. } else {
  107. url = url + '?_t=' + new Date().getTime();
  108. }
  109. }
  110. if (window.fetch && method == 'fetch') {
  111. var header_content = {
  112. 'Accept': 'application/json',
  113. 'Content-Type': 'application/x-www-form-urlencoded',
  114. 'token': '',
  115. 'x-user-header': ''
  116. };
  117. if (dataType && dataType === "XML") {
  118. header_content = {
  119. 'Accept': 'application/xml',
  120. 'Content-Type': 'application/x-www-form-urlencoded',
  121. 'token': ''
  122. };
  123. } else {
  124. if (requestType === "JSON") {
  125. header_content = {
  126. 'Accept': 'application/json',
  127. 'Content-Type': 'application/json',
  128. 'token': '',
  129. 'x-user-header': localStorage.user_account ? localStorage.user_account : '',
  130. 'x-userId-header': localStorage.user_id ? localStorage.user_id : '',
  131. 'x-userAccount-header': localStorage.userAccount ? encodeURI(localStorage.userAccount) : (localStorage.user_account ? encodeURI(localStorage.user_account) : ''),
  132. 'x-userName-header': localStorage.userName ? encodeURI(localStorage.userName) : (localStorage.user_name ? encodeURI(localStorage.user_name) : ''),
  133. 'x-companyId-header': localStorage.companyId ? localStorage.companyId : '',
  134. };
  135. } else if (requestType === "FORMDATA") {
  136. header_content = {
  137. 'Accept': 'application/json',
  138. 'Content-Type': 'application/x-www-form-urlencoded',
  139. 'Authorization': "Basic ZWJlaTplYmVp",
  140. 'x-user-header': localStorage.user_account ? localStorage.user_account : '',
  141. 'x-userId-header': localStorage.user_id ? localStorage.user_id : '',
  142. 'x-userAccount-header': localStorage.userAccount ? encodeURI(localStorage.userAccount) : (localStorage.user_account ? encodeURI(localStorage.user_account) : ''),
  143. 'x-userName-header': localStorage.userName ? encodeURI(localStorage.userName) : (localStorage.user_name ? encodeURI(localStorage.user_name) : ''),
  144. 'x-companyId-header': localStorage.companyId ? localStorage.companyId : '',
  145. };
  146. }
  147. }
  148. if (localStorage.token != "" && typeof (localStorage.token) !== "undefined") {
  149. header_content.token = localStorage.token;
  150. }
  151. header_content.sign = key_gen.encode(key_gen.handlerData(type == 'GET' ? url.substring(url.indexOf("?") + 1) : data));
  152. let requestConfig = {
  153. credentials: 'include',
  154. method: type,
  155. headers: header_content,
  156. mode: "cors",
  157. cache: "force-cache"
  158. }
  159. if (type == 'POST') {
  160. if (requestType === "FORMDATA") {
  161. Object.defineProperty(requestConfig, 'body', {
  162. value: initData(data)
  163. })
  164. } else if (requestType === "JSON") {
  165. Object.defineProperty(requestConfig, 'body', {
  166. value: JSON.stringify(data)
  167. })
  168. }
  169. }
  170. try {
  171. const response = await fetch(url, requestConfig);
  172. if (response.headers.get("token_code") == -2 || response.headers.get("token_code") == -1) {
  173. Vue.prototype.$Message.config({
  174. top: 80,
  175. duration: 3
  176. });
  177. Vue.prototype.$Message.info("当前账号已过期,请重新登录!");
  178. router.push({
  179. name: 'login',
  180. params: {
  181. timeout: true
  182. }
  183. });
  184. Vue.prototype.$Message = function (str) {
  185. return;
  186. };
  187. setTimeout(function () {
  188. localStorage.clear();
  189. location.reload();
  190. return;
  191. }, 3000);
  192. } else {
  193. if (dataType && dataType === "XML") {
  194. const responseJson = await response.text();
  195. return responseJson
  196. } else {
  197. const responseJson = await response.json();
  198. return responseJson
  199. }
  200. }
  201. } catch (error) {
  202. Vue.prototype.$Spin.hide();
  203. //throw new Error(error)
  204. const responseJson = {
  205. errCode: 500,
  206. msg: "系统异常"
  207. };
  208. return responseJson;
  209. }
  210. } else {
  211. return new Promise((resolve, reject) => {
  212. let requestObj;
  213. if (window.XMLHttpRequest) {
  214. requestObj = new XMLHttpRequest();
  215. } else {
  216. requestObj = new ActiveXObject;
  217. }
  218. let sendData = '';
  219. if (type == 'POST') {
  220. console.log(data)
  221. Object.keys(data).forEach(key => {
  222. sendData += key + '=' + data[key] + '&';
  223. })
  224. sendData = sendData.substr(0, sendData.lastIndexOf('&'));
  225. console.log(sendData)
  226. }
  227. requestObj.open(type, url, true);
  228. requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  229. requestObj.setRequestHeader("Accept", "application/json");
  230. requestObj.send(sendData);
  231. requestObj.onreadystatechange = () => {
  232. if (requestObj.readyState == 4) {
  233. if (requestObj.status == 200) {
  234. let obj = requestObj.response
  235. if (typeof obj !== 'object' && obj.indexOf("<?xml") == -1) {
  236. obj = JSON.parse(obj);
  237. }
  238. resolve(obj)
  239. }
  240. }
  241. }
  242. })
  243. }
  244. }