Connection.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * 在 ajax request 前修改url,增加服务端`basePath`
  3. * `basePath`在`app.json`配置
  4. */
  5. Ext.define('saas.override.data.Connection', {
  6. override: 'Ext.data.Connection',
  7. urlRegexp: /(http|ftp|https):\/\//,
  8. config: {
  9. /**
  10. * @cfg {Object} defaultServerHeaders
  11. * 与defaultHeaders有区别,只在调用server api的时候才添加的headers
  12. */
  13. defaultServerHeaders: null
  14. },
  15. privates: {
  16. parseBasePath: function (basePath) {
  17. if (Ext.isObject(basePath)) {
  18. var protocol = window.location.protocol.split(":")[0];
  19. if (protocol === 'file') {
  20. protocol = 'https';
  21. }
  22. return basePath[protocol];
  23. }
  24. return basePath;
  25. },
  26. setupServerOptions: function (options) {
  27. var serverOptions = Ext.manifest.server, originUrl = options.url;
  28. if (serverOptions && serverOptions.basePath && !this.urlRegexp.test(originUrl) &&
  29. (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) {
  30. Ext.Object.merge(options, {
  31. url: this.parseBasePath(serverOptions.basePath)
  32. + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl,
  33. headers: this.getDefaultServerHeaders() || {}
  34. });
  35. }
  36. },
  37. parseResponseStatus: function (status) {
  38. switch (status) {
  39. case 0:
  40. return "无法连接服务";
  41. }
  42. }
  43. },
  44. request: function (options) {
  45. if (options && options.url) {
  46. this.setupServerOptions(options);
  47. if (options.headers && !options.headers['Authorization']) {
  48. // 没有身份信息的情况下,尝试使用cookie信息
  49. options.withCredentials = true;
  50. }
  51. }
  52. return this.callParent([options]);
  53. },
  54. onRequestComplete: function (request) {
  55. let headers = request.result.getAllResponseHeaders() || {},
  56. Authorization = headers['authorization'],
  57. result = request.result,
  58. res;
  59. try {
  60. res = JSON.parse(result.responseText);
  61. } catch (e) {
  62. res = result.responseJson
  63. // console.error(e);
  64. }
  65. if (res && res.code == 40001) {
  66. // 如果token超时则显示重新登陆弹窗
  67. saas.util.BaseUtil.showLoginWin();
  68. throw new Error('会话已过期');
  69. }
  70. if (Authorization) {
  71. // 如果返回了token则刷新token
  72. let headers = Ext.Ajax.getDefaultHeaders() || {};
  73. let session = saas.util.State.get('session');
  74. headers['Authorization'] = Authorization;
  75. Ext.Ajax.setDefaultHeaders(headers);
  76. session.token = Authorization;
  77. session.timestamp = Ext.now();
  78. saas.util.State.set('session', session);
  79. }
  80. },
  81. promise: function (config) {
  82. var me = this, url = config.url,
  83. params = config.params,
  84. async = (Ext.isBoolean(config.async) ? config.async : true),
  85. method = config.method || 'GET',
  86. timeout = config.timeout || 8000,
  87. defaultHeaders = {
  88. 'Access-Control-Allow-Origin': '*',
  89. 'Content-Type': 'application/json;charset=UTF-8'
  90. };
  91. return new Ext.Promise(function (resolve, reject) {
  92. Ext.Ajax.request({
  93. url: url,
  94. params: params,
  95. jsonData: config.jsonData,
  96. async: async,
  97. method: method,
  98. timeout: timeout,
  99. headers: Ext.apply(defaultHeaders, config.headers),
  100. success: function (response) {
  101. var res = Ext.decode(response.responseText);
  102. if (res.success) {
  103. return resolve(res);
  104. } else {
  105. res.message = res.message || '未知错误';
  106. return reject(res);
  107. }
  108. },
  109. failure: function (response) {
  110. var res, status = response.status;
  111. try {
  112. res = JSON.parse(response.responseText);
  113. } catch (e) {
  114. res = response.responseJson || {
  115. message: me.parseResponseStatus(status)
  116. };
  117. }
  118. res.message = res.message || '未知错误';
  119. reject(res);
  120. }
  121. });
  122. })
  123. },
  124. /**
  125. * form post
  126. *
  127. * @param {*} config
  128. */
  129. form: function (config) {
  130. config.headers = config.headers || {};
  131. Ext.apply(config.headers, {
  132. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  133. });
  134. return this.post(config);
  135. },
  136. /**
  137. * post
  138. *
  139. * @param {*} config
  140. */
  141. post: function (config) {
  142. config.method = 'POST';
  143. return this.promise(config);
  144. },
  145. /**
  146. * payload
  147. *
  148. * @param {*} config
  149. */
  150. payload: function (config) {
  151. if (Ext.isObject(config.params)) {
  152. config.params = JSON.stringify(config.params);
  153. }
  154. return this.post(config);
  155. },
  156. getServerBasePath: function () {
  157. const basePath = Ext.manifest.server.basePath;
  158. if (Ext.isObject(basePath)) {
  159. let protocol = window.location.protocol.split(":")[0];
  160. if (protocol === 'file') {
  161. protocol = 'https';
  162. }
  163. return basePath[protocol];
  164. }
  165. return basePath;
  166. }
  167. });