my-upload.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <u-upload @afterRead="afterRead" :maxCount="maxCount" :accept="accept" multiple>
  3. <view v-if="showLoading">
  4. <u-loading-icon v-if="loading" />
  5. <slot v-else />
  6. </view>
  7. <slot v-else />
  8. </u-upload>
  9. </template>
  10. <script>
  11. export default {
  12. name: "MyUpload",
  13. props: {
  14. accept: { //接受的文件类型,file只支持H5(只有微信小程序才支持把accept配置为all、media)
  15. type: String,
  16. default: "image"
  17. },
  18. maxCount: {
  19. type: String,
  20. default: "99"
  21. },
  22. parentid: { //上传文件夹ID
  23. type: [String, Number],
  24. default: uni.getStorageSync('siteP').appfolderid
  25. },
  26. uploadCallback: { //上传回调
  27. type: Function
  28. },
  29. showLoading: {
  30. type: Boolean,
  31. default: true
  32. },
  33. onLoading: {
  34. type: Function
  35. }
  36. },
  37. data() {
  38. return {
  39. loading: false
  40. }
  41. },
  42. watch: {
  43. loading: function (newVal) {
  44. this.$emit("onLoading", newVal)
  45. }
  46. },
  47. methods: {
  48. afterRead({ file }) {
  49. const that = this;
  50. file.forEach(v => {
  51. // #ifdef H5
  52. this.getArrayBuffer(v).then(data => that.handleUploadFile(this.requestType(v), data))
  53. // #endif
  54. // #ifndef H5
  55. uni.getFileSystemManager().readFile({
  56. filePath: v.url,
  57. success: data => that.handleUploadFile(this.requestType(v), data.data),
  58. fail: console.error
  59. })
  60. // #endif
  61. });
  62. },
  63. /* 请求类型 */
  64. requestType(file) {
  65. // #ifdef H5
  66. var ext = file.name.substr(file.name.lastIndexOf(".") + 1);
  67. // #endif
  68. // #ifndef H5
  69. var ext = file.type.split("/")[1] || file.url.substr(file.url.lastIndexOf(".") + 1) || file.name.substr(file.name.lastIndexOf(".") + 1);
  70. // #endif
  71. //文件名称
  72. return {
  73. "classname": "system.attachment.huawei.OBS",
  74. "method": "getFileName",
  75. "content": {
  76. "filename": `${Date.now() + file.size}.${ext}`,
  77. "filetype": ext,
  78. "parentid": this.parentid
  79. }
  80. }
  81. },
  82. handleUploadFile(file, data) {
  83. this.loading = true;
  84. this.$Http.basic(file).then(res => {
  85. if (res.msg == "成功") {
  86. this.uploadFile(res.data, data)
  87. } else {
  88. uni.showToast({
  89. title: `${data.filename}.${data.serialfilename}`,
  90. icon: "none"
  91. })
  92. }
  93. })
  94. },
  95. getArrayBuffer(file) {
  96. return new Promise((resolve, reject) => {
  97. let xhr = new XMLHttpRequest()
  98. xhr.open('GET', file.url, true)
  99. xhr.responseType = 'blob'
  100. xhr.onload = function () {
  101. if (this.status == 200) {
  102. let myBlob = this.response
  103. let files = new window.File([myBlob], file.name, { type: file.url.substr(file.url.lastIndexOf(".") + 1) }) // myBlob.type 自定义文件名
  104. const reader = new FileReader();
  105. reader.readAsArrayBuffer(files);
  106. reader.onload = () => resolve(reader.result);
  107. reader.onerror = (error) => reject(error);
  108. } else {
  109. reject(false)
  110. }
  111. }
  112. xhr.send()
  113. })
  114. },
  115. /* 上传成功反馈 */
  116. uploadFile(res, data) {
  117. var that = this;
  118. that.loading = true;
  119. uni.request({
  120. url: res.uploadurl,
  121. method: "PUT",
  122. data,
  123. header: {
  124. 'content-type': 'application/octet-stream'
  125. },
  126. success() {
  127. that.$Http.basic({
  128. "classname": "system.attachment.huawei.OBS",
  129. "method": "uploadSuccess",
  130. "content": {
  131. "serialfilename": res.serialfilename
  132. }
  133. }).then(s => {
  134. console.log("文件上传反馈", s)
  135. that.loading = false;
  136. if (!that.cutoff(s.msg)) that.$emit("uploadCallback", s.data.attachmentids);
  137. }).catch(err => {
  138. that.loading = false;
  139. console.error(err)
  140. })
  141. },
  142. fail(err) {
  143. that.loading = false;
  144. console.log(err)
  145. }
  146. })
  147. }
  148. },
  149. }
  150. </script>