request.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. function camSafeUrlEncode(str) {
  2. return encodeURIComponent(str)
  3. .replace(/!/g, '%21')
  4. .replace(/'/g, '%27')
  5. .replace(/\(/g, '%28')
  6. .replace(/\)/g, '%29')
  7. .replace(/\*/g, '%2A');
  8. }
  9. function getObjectKeys(obj, forKey) {
  10. var list = [];
  11. for (var key in obj) {
  12. if (obj.hasOwnProperty(key)) {
  13. list.push(forKey ? camSafeUrlEncode(key).toLowerCase() : key);
  14. }
  15. }
  16. return list.sort(function (a, b) {
  17. a = a.toLowerCase();
  18. b = b.toLowerCase();
  19. return a === b ? 0 : (a > b ? 1 : -1);
  20. });
  21. };
  22. var obj2str = function (obj, lowerCaseKey) {
  23. var i, key, val;
  24. var list = [];
  25. var keyList = getObjectKeys(obj);
  26. for (i = 0; i < keyList.length; i++) {
  27. key = keyList[i];
  28. val = (obj[key] === undefined || obj[key] === null) ? '' : ('' + obj[key]);
  29. key = lowerCaseKey? camSafeUrlEncode(key).toLowerCase() : camSafeUrlEncode(key);
  30. val = camSafeUrlEncode(val) || '';
  31. list.push(key + '=' + val)
  32. }
  33. return list.join('&');
  34. };
  35. var request = function (params, callback) {
  36. var filePath = params.filePath;
  37. var headers = params.headers || {};
  38. var url = params.url || params.Url;
  39. var method = params.method;
  40. var onProgress = params.onProgress;
  41. var httpDNSServiceId = params.httpDNSServiceId;
  42. var requestTask;
  43. var cb = function (err, response) {
  44. var H = response.header
  45. var headers = {};
  46. if (H) for (var key in H) {
  47. if (H.hasOwnProperty(key)) headers[key.toLowerCase()] = H[key];
  48. }
  49. callback(err, {statusCode: response.statusCode, headers: headers}, response.data);
  50. };
  51. if (filePath) {
  52. var fileKey;
  53. var m = url.match(/^(https?:\/\/[^/]+\/)([^/]*\/?)(.*)$/);
  54. if (params.pathStyle) {
  55. fileKey = decodeURIComponent(m[3] || '');
  56. url = m[1] + m[2];
  57. } else {
  58. fileKey = decodeURIComponent(m[2] + m[3] || '');
  59. url = m[1];
  60. }
  61. // 整理 postObject 参数
  62. var formData = {
  63. 'key': fileKey,
  64. 'success_action_status': 200,
  65. 'Signature': headers.Authorization,
  66. };
  67. var headerKeys = [
  68. 'Cache-Control',
  69. 'Content-Type',
  70. 'Content-Disposition',
  71. 'Content-Encoding',
  72. 'Expires',
  73. 'x-cos-storage-class',
  74. 'x-cos-security-token',
  75. 'x-ci-security-token',
  76. ];
  77. for (var i in params.headers) {
  78. if (params.headers.hasOwnProperty(i) && (i.indexOf('x-cos-meta-') > -1 || headerKeys.indexOf(i) > -1)) {
  79. formData[i] = params.headers[i];
  80. }
  81. }
  82. headers['x-cos-acl'] && (formData.acl = headers['x-cos-acl']);
  83. !formData['Content-Type'] && (formData['Content-Type'] = '');
  84. requestTask = wx.uploadFile({
  85. url: url,
  86. method: method,
  87. name: 'file',
  88. header: headers,
  89. filePath: filePath,
  90. formData: formData,
  91. timeout: params.timeout,
  92. success: function (response) {
  93. cb(null, response);
  94. },
  95. fail: function (response) {
  96. cb(response.errMsg, response);
  97. }
  98. });
  99. requestTask.onProgressUpdate(function (res) {
  100. onProgress && onProgress({
  101. loaded: res.totalBytesSent,
  102. total: res.totalBytesExpectedToSend,
  103. progress: res.progress / 100
  104. });
  105. });
  106. } else {
  107. var qsStr = params.qs && obj2str(params.qs) || '';
  108. if (qsStr) {
  109. url += (url.indexOf('?') > -1 ? '&' : '?') + qsStr;
  110. }
  111. headers['Content-Length'] && (delete headers['Content-Length']);
  112. var requestParams = {
  113. url: url,
  114. method: method,
  115. header: headers,
  116. dataType: 'text',
  117. data: params.body,
  118. timeout: params.timeout,
  119. success: function (response) {
  120. cb(null, response);
  121. },
  122. fail: function (response) {
  123. cb(response.errMsg, response);
  124. }
  125. };
  126. if (httpDNSServiceId) {
  127. Object.assign(requestParams, {
  128. enableHttpDNS: true,
  129. httpDNSServiceId: httpDNSServiceId,
  130. });
  131. }
  132. requestTask = wx.request(requestParams);
  133. }
  134. return requestTask;
  135. };
  136. module.exports = request;