Connection.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Ext.define('uas.override.data.Connection', {
  2. override: 'Ext.data.Connection',
  3. urlRegexp: /(http|ftp|https):\/\//,
  4. privates: {
  5. setupServerOptions: function (options) {
  6. var serverOptions = Ext.manifest.server, originUrl = options.url;
  7. if (serverOptions && serverOptions.url && !this.urlRegexp.test(originUrl) &&
  8. (!serverOptions.urlPattern || new RegExp(serverOptions.urlPattern).test(originUrl))) {
  9. Ext.Object.merge(options, {
  10. url: serverOptions.url + (originUrl.indexOf('/') == 0 ? '' : '/') + originUrl
  11. });
  12. }
  13. },
  14. parseResponseStatus: function (status) {
  15. switch (status) {
  16. case 0:
  17. return "无法连接服务";
  18. }
  19. }
  20. },
  21. request: function (options) {
  22. if (options && options.url) {
  23. this.setupServerOptions(options);
  24. }
  25. return this.callParent([options]);
  26. },
  27. promise: function (config) {
  28. var me = this, url = config.url,
  29. params = config.params,
  30. async = (Ext.isBoolean(config.async) ? config.async : true),
  31. method = config.method || 'GET',
  32. timeout = config.timeout || 8000,
  33. defaultHeaders = {
  34. 'Access-Control-Allow-Origin': '*',
  35. 'Content-Type': 'application/json;charset=UTF-8'
  36. };
  37. return new Ext.Promise(function (resolve, reject) {
  38. Ext.Ajax.request({
  39. url: url,
  40. params: params,
  41. async: async,
  42. method: method,
  43. timeout: timeout,
  44. headers: Ext.apply(defaultHeaders, config.headers),
  45. success: function (response) {
  46. var res = Ext.decode(response.responseText);
  47. if (res.success) {
  48. return resolve(res);
  49. } else {
  50. res.message = res.message || '未知错误';
  51. return reject(res);
  52. }
  53. },
  54. failure: function (response) {
  55. var res, status = response.status;
  56. try {
  57. res = JSON.parse(response.responseText);
  58. } catch (e) {
  59. res = response.responseJson || {
  60. message: me.parseResponseStatus(status)
  61. };
  62. }
  63. res.message = res.message || '未知错误';
  64. reject(res);
  65. }
  66. });
  67. })
  68. },
  69. /**
  70. * form post
  71. *
  72. * @param {*} config
  73. */
  74. form: function (config) {
  75. config.headers = config.headers || {};
  76. Ext.apply(config.headers, {
  77. 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
  78. });
  79. config.method = 'POST';
  80. return this.promise(config);
  81. },
  82. /**
  83. * payload
  84. *
  85. * @param {*} config
  86. */
  87. payload: function (config) {
  88. if (Ext.isObject(config.params)) {
  89. config.params = JSON.stringify(config.params);
  90. }
  91. config.method = 'POST';
  92. return this.promise(config);
  93. }
  94. });