Guest User

Untitled

a guest
Feb 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. function wxRequest(options, callback) {
  2. var xhr, timer;
  3. var timedout = false;
  4.  
  5. var abortReq = function () {
  6. xhr.abort();
  7. cleanUp();
  8. };
  9.  
  10. var timeoutReq = function () {
  11. timedout = true;
  12. xhr.abort();
  13. cleanUp();
  14. };
  15.  
  16. var ret = {abort: abortReq};
  17.  
  18. var cleanUp = function () {
  19. clearTimeout(timer);
  20. ret.abort = function () {};
  21. xhr = undefined;
  22. };
  23.  
  24. if (options.method === 'GET') {
  25. delete options.headers['Content-Type'];
  26. } else if (options.json) {
  27. options.headers.Accept = 'application/json';
  28. options.headers['Content-Type'] = options.headers['Content-Type'] ||
  29. 'application/json';
  30. if (options.body &&
  31. options.processData &&
  32. typeof options.body !== "string") {
  33. options.body = JSON.stringify(options.body);
  34. }
  35. }
  36.  
  37. if (!('body' in options)) {
  38. options.body = null;
  39. }
  40. var dataType = options.binary ? 'arraybuffer' : 'json';
  41. var responseType = options.body && (options.body instanceof Blob) ? 'arraybuffer' : 'text';
  42. var headers = {};
  43. delete options.headers['Referer'];
  44. for (var key in options.headers) {
  45. if (options.headers.hasOwnProperty(key)) {
  46. headers[key] = options.headers[key];
  47. }
  48. }
  49.  
  50. if (options.timeout > 0) {
  51. timer = setTimeout(timeoutReq, options.timeout);
  52. }
  53.  
  54. var success = function(ret) {
  55. if (ret.statusCode >= 200 && ret.statusCode < 300) {
  56. var data = options.binary ? createBlob([ret.data || ''],{type: ret.header['Content-Type']}) : JSON.stringify(ret.data)
  57. callback(null,{statusCode: ret.statusCode},data)
  58. }
  59. else {
  60. var err = {};
  61. if (timedout) {
  62. err = new Error('ETIMEDOUT');
  63. err.code = 'ETIMEDOUT';
  64. } else if (typeof ret === 'object') {
  65. err = ret
  66. }
  67. err.status = ret.statusCode;
  68. callback(err);
  69. }
  70. }
  71.  
  72. var fail = function(ret) {
  73. var err = UNKNOWN_ERROR;
  74. if (timedout) {
  75. err = new Error('ETIMEDOUT');
  76. err.code = 'ETIMEDOUT';
  77. } else if (typeof ret === 'object') {
  78. err = ret;
  79. } else if (typeof ret === 'string') {
  80. err = new PouchError(500, 'wechat_error', ret);
  81. }
  82. callback(err);
  83. }
  84.  
  85. var complete = function() {
  86. cleanUp()
  87. }
  88.  
  89. if (options.body && (options.body instanceof Blob)) {
  90. readAsArrayBuffer(options.body, function (arrayBuffer) {
  91. xhr = wx.request({
  92. url : options.url,
  93. data : arrayBuffer,
  94. header : headers,
  95. method : options.method,
  96. dataType : dataType,
  97. responseType : responseType,
  98. success : success,
  99. fail : fail,
  100. complete : complete
  101. })
  102. });
  103. } else {
  104. xhr = wx.request({
  105. url : options.url,
  106. data : options.body,
  107. header : headers,
  108. method : options.method,
  109. dataType : dataType,
  110. responseType : responseType,
  111. success : success,
  112. fail : fail,
  113. complete : complete
  114. })
  115. }
  116.  
  117. return ret;
  118. }
Add Comment
Please, Sign In to add comment