oos.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import axios from 'axios'
  2. let OSS = require('ali-oss');
  3. export default {
  4. mounted() {
  5. },
  6. data() {
  7. return {
  8. uploadFileList: [],
  9. fileUploadCallBackParams: {return_data: {data: {}, status: 0}, upload_id: ""},
  10. tempCheckpoint: {},
  11. uploadPercentage: 0,
  12. oosClient: null,
  13. isUploading: false
  14. }
  15. },
  16. methods: {
  17. uploadFileChange(file) {
  18. console.log(file);
  19. if (file.size / 1024 > 1024 * 1000) {
  20. this.$message.error("作品大小不得超过1G,请重新选择");
  21. this.$refs.upload.clearFiles();
  22. return false;
  23. }
  24. this.prepareUpload(file);
  25. },
  26. initUpload(file) {
  27. if (this.isUploading) {
  28. this.oosClient.cancel();
  29. }
  30. this.uploadPercentage = 0;
  31. this.uploadFileList = [];
  32. if (file)
  33. this.uploadFileList.push({name: file.name, url: "", path: ""});
  34. },
  35. uploadFileOnProgress(event, file, fileList) {
  36. },
  37. uploadFileDelete(file = null) {
  38. this.initUpload();
  39. },
  40. /**
  41. * 上传文件出错
  42. */
  43. uploadFileError() {
  44. },
  45. /**
  46. * 预上传
  47. * @param file
  48. * @param callback
  49. */
  50. prepareUpload(file) {
  51. //初始化上传状态
  52. this.initUpload(file);
  53. const formData = new FormData();
  54. let wid = this.$route.query.wid;
  55. formData.append("target", `{ "type": 5 ,"work_id":${wid || -1}}`);
  56. formData.append("filename", file.name);
  57. this.$axios.$post(`/file/prepareUpload`, formData, {headers: {"Content-Type": "multipart/form-data"}}).then(data => {
  58. console.log("data", data);
  59. this.fileUploadCallBackParams.upload_id = data.data.upload_id;
  60. this.fileUploadCallBackParams.return_data.data._upload_id = data.data.upload_id;
  61. // this._ossUpload(data.data, file);
  62. this._ossMultipartUpload(data.data, file)
  63. }).catch(err => {
  64. });
  65. },
  66. /**
  67. * 阿里云上传
  68. * @param data
  69. * @param file
  70. * @private
  71. */
  72. _ossUpload(data, file) {
  73. try {
  74. this.oosClient = OSS({
  75. region: "oss-cn-beijing",
  76. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  77. accessKeyId: data.sts.Credentials.AccessKeyId,
  78. accessKeySecret: data.sts.Credentials.AccessKeySecret,
  79. stsToken: data.sts.Credentials.SecurityToken,
  80. bucket: data.post_params.bucket
  81. });
  82. // object-key可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
  83. this.oosClient.put(data.post_params.policy['save-key'], file.raw).then(res => {
  84. console.log("*******", res);
  85. if (res.res.status === 200) {
  86. this.setUploadComplete(res, file);
  87. }
  88. }).catch(err => {
  89. console.log(err);
  90. });
  91. } catch (e) {
  92. console.log(e);
  93. }
  94. },
  95. _ossMultipartUpload(data, file) {
  96. try {
  97. this.oosClient = OSS({
  98. region: "oss-cn-beijing",
  99. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录RAM控制台创建RAM账号。
  100. accessKeyId: data.sts.Credentials.AccessKeyId,
  101. accessKeySecret: data.sts.Credentials.AccessKeySecret,
  102. stsToken: data.sts.Credentials.SecurityToken,
  103. bucket: data.post_params.bucket
  104. });
  105. let tempCheckpoint;
  106. // object-key可以自定义为文件名(例如file.txt)或目录(例如abc/test/file.txt)的形式,实现将文件上传至当前Bucket或Bucket下的指定目录。
  107. this.oosClient.multipartUpload(data.post_params.policy['save-key'], file.raw, {
  108. progress: (p, checkpoint) => {
  109. // 断点记录点。浏览器重启后无法直接继续上传,您需要手动触发上传操作。
  110. tempCheckpoint = checkpoint;
  111. this.uploadPercentage = p * 100;
  112. this.isUploading = p !== 1;
  113. console.log(this.uploadPercentage);
  114. },
  115. meta: {year: 2020, people: 'test'},
  116. mime: file.type,
  117. }).then(res => {
  118. console.log("multipartUpload res", res);
  119. this.setUploadComplete(res, file);
  120. }).catch(err => {
  121. console.log("multipartUpload err", err);
  122. })
  123. } catch (e) {
  124. console.log(e);
  125. }
  126. },
  127. /**
  128. * 设置上传结果检查的参数
  129. * @param res
  130. * @param file
  131. */
  132. setUploadComplete(res, file) {
  133. this.fileUploadCallBackParams.return_data.status = 1;
  134. this.fileUploadCallBackParams.return_data.data.url = "https:\/\/filescdn.proginn.com/" + res.name;
  135. this.fileUploadCallBackParams.return_data.data.path = res.name;
  136. this.fileUploadCallBackParams.return_data.data.size = file.size;
  137. this.fileUploadCallBackParams.return_data.data.name = file.name;
  138. this.fileUploadCallBackParams.return_data = JSON.stringify(this.fileUploadCallBackParams.return_data);
  139. console.log("~~~~~~~~~~~~~~", this.fileUploadCallBackParams);
  140. this.uploadCallBack();
  141. },
  142. /**
  143. * 阿里云上传回调
  144. */
  145. uploadCallBack() {
  146. this.$axios.$post("/file/uploadCallBack", this.fileUploadCallBackParams).then((res) => {
  147. this.fileUploadCallBackParams = {return_data: {data: {}, status: 0}, upload_id: ""};
  148. if (res.status === 1) {
  149. this.uploadFileList = [];
  150. this.uploadFileList.push({name: res.data.name, url: res.data.url, path: res.data.path});
  151. this.$message.success("上传成功");
  152. } else {
  153. this.$message.success(`上传失败-${res.info}`);
  154. }
  155. }).catch(err => {
  156. console.log(err);
  157. })
  158. },
  159. }
  160. }