bootstrap.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //修改上传的接口调用
  2. require(['upload'], function (Upload) {
  3. //初始化中完成判断
  4. Upload.events.onInit = function () {
  5. //如果上传接口不是七牛云,则不处理
  6. if (this.options.url !== Config.upload.uploadurl) {
  7. return;
  8. }
  9. var _success = this.options.success;
  10. $.extend(this.options, {
  11. chunkSuccess: function (chunk, file, response) {
  12. this.contexts = this.contexts ? this.contexts : [];
  13. this.contexts.push(typeof response.ctx !== 'undefined' ? response.ctx : response.data.ctx);
  14. },
  15. chunksUploaded: function (file, done) {
  16. var that = this;
  17. Fast.api.ajax({
  18. url: "/addons/qiniu/index/upload",
  19. data: {
  20. action: 'merge',
  21. filesize: file.size,
  22. filename: file.name,
  23. chunkid: file.upload.uuid,
  24. chunkcount: file.upload.totalChunkCount,
  25. width: file.width || 0,
  26. height: file.height || 0,
  27. type: file.type,
  28. qiniutoken: Config.upload.multipart.qiniutoken,
  29. contexts: this.contexts
  30. },
  31. }, function (data, ret) {
  32. done(JSON.stringify(ret));
  33. return false;
  34. }, function (data, ret) {
  35. file.accepted = false;
  36. that._errorProcessing([file], ret.msg);
  37. return false;
  38. });
  39. },
  40. });
  41. //先移除已有的事件
  42. this.off("success", _success).on("success", function (file, response) {
  43. var ret = {code: 0, msg: response};
  44. try {
  45. ret = typeof response === 'string' ? JSON.parse(response) : response;
  46. if (file.xhr.status === 200) {
  47. if (typeof ret.key !== 'undefined') {
  48. console.log(Config.upload,1111)
  49. ret = {code: 1, msg: "", data: {url: '/' + ret.key, hash: ret.hash}};
  50. }
  51. Fast.api.ajax({
  52. url: "/addons/qiniu/index/notify",
  53. data: {name: file.name, url: ret.data.url, hash: ret.data.hash, size: file.size, width: file.width || 0, height: file.height || 0, type: file.type, qiniutoken: Config.upload.multipart.qiniutoken}
  54. }, function () {
  55. return false;
  56. }, function () {
  57. return false;
  58. });
  59. }
  60. } catch (e) {
  61. console.error(e);
  62. }
  63. ret.data.url= Config.upload.cdnurl + ret.data.url
  64. _success.call(this, file, ret);
  65. });
  66. //如果是直传模式
  67. if (Config.upload.uploadmode === 'client') {
  68. var _url = this.options.url;
  69. //分片上传时URL链接不同
  70. this.options.url = function (files) {
  71. this.options.headers = {"Authorization": "UpToken " + Config.upload.multipart.qiniutoken};
  72. if (files[0].upload.chunked) {
  73. var chunk = null;
  74. files[0].upload.chunks.forEach(function (item) {
  75. if (item.status === 'uploading') {
  76. chunk = item;
  77. }
  78. });
  79. if (!chunk) {
  80. return Config.upload.uploadurl + '/mkfile/' + files[0].size;
  81. } else {
  82. return Config.upload.uploadurl + '/mkblk/' + chunk.dataBlock.data.size;
  83. }
  84. }
  85. return _url;
  86. };
  87. this.options.params = function (files, xhr, chunk) {
  88. var params = Config.upload.multipart;
  89. if (chunk) {
  90. return $.extend({}, params, {
  91. filesize: chunk.file.size,
  92. filename: chunk.file.name,
  93. chunkid: chunk.file.upload.uuid,
  94. chunkindex: chunk.index,
  95. chunkcount: chunk.file.upload.totalChunkCount,
  96. chunkfilesize: chunk.dataBlock.data.size,
  97. width: chunk.file.width || 0,
  98. height: chunk.file.height || 0,
  99. type: chunk.file.type,
  100. });
  101. } else {
  102. var retParams = $.extend({}, params);
  103. //七牛云直传使用的是token参数
  104. retParams.token = retParams.qiniutoken;
  105. delete retParams.qiniutoken;
  106. return retParams;
  107. }
  108. };
  109. //分片上传时需要变更提交的内容
  110. this.on("sending", function (file, xhr, formData) {
  111. if (file.upload.chunked) {
  112. var _send = xhr.send;
  113. xhr.send = function () {
  114. var chunk = null;
  115. file.upload.chunks.forEach(function (item) {
  116. if (item.status == 'uploading') {
  117. chunk = item;
  118. }
  119. });
  120. if (chunk) {
  121. _send.call(xhr, chunk.dataBlock.data);
  122. }
  123. };
  124. }
  125. });
  126. }
  127. };
  128. });