康巴易测肤/伤疤uniapp小程序类
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 line
3.8KB

  1. const mimeMap = require('./mimeMap.js')
  2. function FormData(){
  3. let fileManager = wx.getFileSystemManager();
  4. let data = {};
  5. let files = [];
  6. this.append = (name, value)=>{
  7. data[name] = value;
  8. return true;
  9. }
  10. this.appendFile = (name, path, fileName)=>{
  11. let buffer = fileManager.readFileSync(path);
  12. if(Object.prototype.toString.call(buffer).indexOf("ArrayBuffer") < 0){
  13. return false;
  14. }
  15. if(!fileName){
  16. fileName = getFileNameFromPath(path);
  17. }
  18. files.push({
  19. name: name,
  20. buffer: buffer,
  21. fileName: fileName
  22. });
  23. return true;
  24. }
  25. this.getData = ()=>convert(data, files)
  26. }
  27. function getFileNameFromPath(path){
  28. let idx=path.lastIndexOf("/");
  29. return path.substr(idx+1);
  30. }
  31. function convert(data, files){
  32. let boundaryKey = 'wxmpFormBoundary' + randString(); // 数据分割符,一般是随机的字符串
  33. let boundary = '--' + boundaryKey;
  34. let endBoundary = boundary + '--';
  35. let postArray = [];
  36. //拼接参数
  37. if(data && Object.prototype.toString.call(data) == "[object Object]"){
  38. for(let key in data){
  39. postArray = postArray.concat(formDataArray(boundary, key, data[key]));
  40. }
  41. }
  42. //拼接文件
  43. if(files && Object.prototype.toString.call(files) == "[object Array]"){
  44. for(let i in files){
  45. let file = files[i];
  46. postArray = postArray.concat(formDataArray(boundary, file.name, file.buffer, file.fileName));
  47. }
  48. }
  49. //结尾
  50. let endBoundaryArray = [];
  51. endBoundaryArray.push(...endBoundary.toUtf8Bytes());
  52. postArray = postArray.concat(endBoundaryArray);
  53. return {
  54. contentType: 'multipart/form-data; boundary=' + boundaryKey,
  55. buffer: new Uint8Array(postArray).buffer
  56. }
  57. }
  58. function randString() {
  59. var result = '';
  60. var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  61. for (var i = 17; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
  62. return result;
  63. }
  64. function formDataArray(boundary, name, value, fileName){
  65. let dataString = '';
  66. let isFile = !!fileName;
  67. dataString += boundary + '\r\n';
  68. dataString += 'Content-Disposition: form-data; name="' + name + '"';
  69. if (isFile){
  70. dataString += '; filename="' + fileName + '"' + '\r\n';
  71. dataString += 'Content-Type: ' + getFileMime(fileName) + '\r\n\r\n';
  72. }
  73. else{
  74. dataString += '\r\n\r\n';
  75. dataString += value;
  76. }
  77. var dataArray = [];
  78. dataArray.push(...dataString.toUtf8Bytes());
  79. if (isFile) {
  80. let fileArray = new Uint8Array(value);
  81. dataArray = dataArray.concat(Array.prototype.slice.call(fileArray));
  82. }
  83. dataArray.push(..."\r".toUtf8Bytes());
  84. dataArray.push(..."\n".toUtf8Bytes());
  85. return dataArray;
  86. }
  87. function getFileMime(fileName){
  88. let idx = fileName.lastIndexOf(".");
  89. let mime = mimeMap[fileName.substr(idx)];
  90. return mime?mime:"application/octet-stream"
  91. }
  92. String.prototype.toUtf8Bytes = function(){
  93. var str = this;
  94. var bytes = [];
  95. for (var i = 0; i < str.length; i++) {
  96. bytes.push(...str.utf8CodeAt(i));
  97. if (str.codePointAt(i) > 0xffff) {
  98. i++;
  99. }
  100. }
  101. return bytes;
  102. }
  103. String.prototype.utf8CodeAt = function(i) {
  104. var str = this;
  105. var out = [], p = 0;
  106. var c = str.charCodeAt(i);
  107. if (c < 128) {
  108. out[p++] = c;
  109. } else if (c < 2048) {
  110. out[p++] = (c >> 6) | 192;
  111. out[p++] = (c & 63) | 128;
  112. } else if (
  113. ((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
  114. ((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
  115. // Surrogate Pair
  116. c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF);
  117. out[p++] = (c >> 18) | 240;
  118. out[p++] = ((c >> 12) & 63) | 128;
  119. out[p++] = ((c >> 6) & 63) | 128;
  120. out[p++] = (c & 63) | 128;
  121. } else {
  122. out[p++] = (c >> 12) | 224;
  123. out[p++] = ((c >> 6) & 63) | 128;
  124. out[p++] = (c & 63) | 128;
  125. }
  126. return out;
  127. };
  128. module.exports = FormData;