EventSource.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // EventSource that supports post data
  2. Ext.define('erp.util.EventSource', {
  3. onMessage: Ext.emptyFn,
  4. onProgress: Ext.emptyFn,
  5. onComplete: Ext.emptyFn,
  6. onError: function(e){
  7. showError(e);
  8. },
  9. showProgress: true,
  10. constructor: function (options) {
  11. Ext.apply(this, options);
  12. this.init();
  13. },
  14. init: function() {
  15. var formData = null, me = this, data = me.data;
  16. if (data) {
  17. formData = new FormData();
  18. for (var k in data) {
  19. formData.append(k, data[k]);
  20. }
  21. }
  22. var xhr = new XMLHttpRequest();
  23. xhr.open((formData ? 'POST' : 'GET'), me.url, true);
  24. xhr.onreadystatechange = function () {
  25. if (xhr.status != 200) {
  26. if (xhr.readyState == 4 && xhr.responseText) {
  27. me.updateText('出现错误');
  28. me.updateProgress(1);
  29. me.onError(xhr.responseText);
  30. this.showProgress = false;
  31. }
  32. } else {
  33. var state = xhr.readyState;
  34. var resText = {};
  35. if (xhr.readyState > 2) {
  36. if (xhr.readyState == 4 && !xhr.responseText) {
  37. me.updateText('执行完成');
  38. me.updateProgress(1);
  39. me.onComplete({});
  40. } else {
  41. if(xhr.responseText.indexOf("exceptionInfo")>=0){
  42. resText.error = xhr.responseText;
  43. me.parseResponseText(JSON.stringify(resText));
  44. }else{
  45. me.parseResponseText(xhr.responseText);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. xhr.setRequestHeader('Accept', 'text/event-stream, */*');
  52. xhr.send(formData);
  53. me.updateProgress(0);
  54. },
  55. parseResponseText: function(text) {
  56. var me = this, messages = text.split(/^data:|\ndata:/g).filter(function (msg) {
  57. return !!msg;
  58. });
  59. var o = me.receivedSize || 0, n = messages.length, msg;
  60. for (; o < n; o++) {
  61. try {
  62. msg = JSON.parse(messages[o]);
  63. } catch (e) {
  64. continue;
  65. }
  66. if (msg.error) {
  67. me.destroyMessageBox();
  68. me.onError(msg.error);
  69. } else if (msg.result) {
  70. me.updateText('执行完成');
  71. me.updateProgress(1);
  72. me.onComplete(msg.result);
  73. } else if (msg.progress) {
  74. if (msg.progress > 0 && msg.progress < 1) {
  75. me.updateProgress(msg.progress);
  76. }
  77. me.onProgress(msg.progress);
  78. } else if (msg.success) {
  79. me.updateText('处理成功');
  80. me.updateProgress(1);
  81. me.onComplete(msg);
  82. } else {
  83. me.updateText(msg.info || msg.warning);
  84. // info、warning
  85. me.onMessage(msg);
  86. }
  87. }
  88. me.receivedSize = n;
  89. },
  90. getMessageBox: function() {
  91. var me = this, box = me.messageBox;
  92. if (!box) {
  93. box = me.messageBox = Ext.Msg.show(Ext.apply({
  94. modal: true,
  95. msg: '请稍等...'
  96. }, me.showProgress ? {
  97. progress: true
  98. } : {
  99. title: '提示',
  100. icon: Ext.MessageBox.INFO
  101. }));
  102. }
  103. return box;
  104. },
  105. updateProgress: function(value) {
  106. var box = this.getMessageBox();
  107. if (this.showProgress) {
  108. box.updateProgress(value, Math.floor(value * 100) + '%');
  109. }
  110. if (value >= 1||!this.showProgress) {
  111. this.destroyMessageBox();
  112. }
  113. },
  114. updateText: function(text) {
  115. this.getMessageBox().updateText(text);
  116. },
  117. destroyMessageBox: function() {
  118. if (this.messageBox) {
  119. this.getMessageBox().close();
  120. this.messageBox = null;
  121. }
  122. }
  123. });