ajax.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* eslint-disable no-prototype-builtins */
  2. // 新鉴权 to open
  3. // import { getAuthorisation } from "./crypto";
  4. // function getStorage(key) {
  5. // const content = window.sessionStorage.getItem(key);
  6. // return content ? JSON.parse(content).value : null;
  7. // }
  8. // function getAuthorHeader(uri, method) {
  9. // const user = getStorage("user");
  10. // const infos = getAuthorisation(
  11. // {
  12. // token: getStorage("token"),
  13. // account: user && user["id"],
  14. // uri,
  15. // method
  16. // },
  17. // "token"
  18. // );
  19. // return {
  20. // Authorization: infos.Authorization,
  21. // time: infos.timestamp,
  22. // deviceId: user && user["id"],
  23. // domain: window.location.origin,
  24. // platform: "print-web"
  25. // };
  26. // }
  27. function getError(action, option, xhr) {
  28. let msg;
  29. if (xhr.response) {
  30. msg = `${xhr.response.error || xhr.response}`;
  31. } else if (xhr.responseText) {
  32. msg = `${xhr.responseText}`;
  33. } else {
  34. msg = `fail to post ${action} ${xhr.status}`;
  35. }
  36. const err = new Error(msg);
  37. err.status = xhr.status;
  38. err.method = "post";
  39. err.url = action;
  40. return err;
  41. }
  42. function getBody(xhr) {
  43. const text = xhr.responseText || xhr.response;
  44. if (!text) {
  45. return text;
  46. }
  47. try {
  48. return JSON.parse(text);
  49. } catch (e) {
  50. return text;
  51. }
  52. }
  53. export default function upload(option) {
  54. if (typeof XMLHttpRequest === "undefined") {
  55. return;
  56. }
  57. const xhr = new XMLHttpRequest();
  58. const action = option.action;
  59. if (xhr.upload) {
  60. xhr.upload.onprogress = function progress(e) {
  61. if (e.total > 0) {
  62. e.percent = (e.loaded / e.total) * 100;
  63. }
  64. option.onProgress(e);
  65. };
  66. }
  67. const formData = new FormData();
  68. if (option.data) {
  69. Object.keys(option.data).forEach(key => {
  70. formData.append(key, option.data[key]);
  71. });
  72. }
  73. formData.append(option.filename, option.file, option.file.name);
  74. xhr.onerror = function error(e) {
  75. option.onError(e);
  76. };
  77. xhr.onload = function onload() {
  78. if (xhr.status < 200 || xhr.status >= 300) {
  79. return option.onError(getError(action, option, xhr));
  80. }
  81. option.onSuccess(getBody(xhr));
  82. };
  83. xhr.open("post", action, true);
  84. if (option.withCredentials && "withCredentials" in xhr) {
  85. xhr.withCredentials = true;
  86. }
  87. let headers = option.headers || {};
  88. // const authorHeader = getAuthorHeader(action, "post");
  89. // for (let item in authorHeader) {
  90. // headers[item] = authorHeader[item];
  91. // }
  92. for (let item in headers) {
  93. if (headers.hasOwnProperty(item) && headers[item] !== null) {
  94. xhr.setRequestHeader(item, headers[item]);
  95. }
  96. }
  97. xhr.send(formData);
  98. return xhr;
  99. }