oss.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <view class="Oss" @click="fileUpload">
  3. <slot></slot>
  4. </view>
  5. </template>
  6. <script>
  7. import {upload} from '../../api/upload'
  8. export default {
  9. props:{
  10. // uploadSuccess: {
  11. // type: Function,
  12. // default: null
  13. // },
  14. filename:String,
  15. tenantId:{
  16. defalut:'',
  17. type:String,
  18. },
  19. mustTenantText:{
  20. defalut:'',
  21. type:String,
  22. },
  23. },
  24. data(){
  25. return {
  26. bucketName : '',
  27. }
  28. },
  29. methods:{
  30. fileUpload(){
  31. let _this = this
  32. if(this.mustTenantText && !this.tenantId){
  33. uni.showToast({
  34. title: _this.mustTenantText,
  35. icon: 'none',
  36. duration: 2000
  37. })
  38. return
  39. }
  40. let tenantId = this.tenantId || uni.getStorageSync('tenantId')
  41. uni.chooseImage({
  42. count:1,
  43. success:(res)=>{
  44. // debugger
  45. console.log(res);
  46. // #ifdef MP-WEIXIN
  47. let index = res.tempFilePaths[0].lastIndexOf('.')
  48. res.name ='01'+res.tempFilePaths[0].substring(index)
  49. // #endif
  50. // #ifndef MP-WEIXIN
  51. res.name = res.tempFiles[0].name
  52. // #endif
  53. console.log('skdjagskjd:',res);
  54. upload(_this.filename,res.name,res.tempFiles[0].size,tenantId).then(result=>{
  55. if (result.data.code === 0) {
  56. _this.ossUpload(res, result.data.data);
  57. } else {
  58. uni.showToast({
  59. title: result.data.error || result.data.msg ,
  60. icon: 'none',
  61. duration: 2000
  62. })
  63. }
  64. })
  65. }
  66. })
  67. },
  68. ossUpload(file, ossToken) {
  69. let _this = this
  70. let tenantId = this.tenantId || uni.getStorageSync('tenantId')
  71. console.log(file,ossToken);
  72. // #ifndef MP-WEIXIN
  73. _this.bucketName = ossToken.bucket
  74. let formData = new FormData();
  75. formData.append('key', ossToken.dir +'/'+ file.name); //存储在oss的文件路径
  76. formData.append('OSSAccessKeyId', ossToken.accessid); //accessKeyId
  77. formData.append('policy', ossToken.policy); //policy
  78. formData.append('Signature', ossToken.signature); //签名
  79. formData.append('callback', ossToken.callback); //回调
  80. formData.append('success_action_status', 200); //成功后返回的操作码
  81. formData.append("file", file.tempFiles[0]);
  82. formData.append("x:tenant", tenantId);
  83. formData.append('x:bucket_name', ossToken.bucketName); //tenantId
  84. let xhr = new XMLHttpRequest();
  85. xhr.withCredentials = false;
  86. xhr.open("POST", ossToken.host);
  87. xhr.onload = () => {
  88. console.log(xhr.response); //输出结果
  89. _this.ossUploadFinished(JSON.parse(xhr.response))
  90. };
  91. xhr.send(formData);
  92. // #endif
  93. // #ifdef MP-WEIXIN
  94. wx.uploadFile({
  95. url:ossToken.host,
  96. header: {
  97. 'Content-Type': 'multipart/form-data'
  98. },
  99. filePath:file.tempFilePaths[0],
  100. name:'file',
  101. formData:{
  102. key:ossToken.dir +'/'+ file.name,
  103. OSSAccessKeyId:ossToken.accessid,
  104. policy:ossToken.policy,
  105. Signature: ossToken.signature,
  106. callback: ossToken.callback,
  107. "x:tenant":tenantId,
  108. 'x:bucket_name':ossToken.bucketName
  109. // success_action_status: '200',
  110. },
  111. success: function (res) {
  112. console.log('res========',res);
  113. if (res.statusCode === 200) {
  114. _this.ossUploadFinished(JSON.parse(res.data))
  115. } else {
  116. console.log('上传失败');
  117. }
  118. },
  119. fail: function (err) {
  120. console.info(err)
  121. }
  122. })
  123. // #endif
  124. },
  125. ossUploadFinished(res) {
  126. console.log(res);
  127. if (res.code === 0) {
  128. console.log(res);
  129. let data = {
  130. original: res.data.name,
  131. fileSize: res.data.size,
  132. url: res.data.url,
  133. fileName: res.data.filename,
  134. bucketName: this.bucketName,
  135. mimeType:res.data.mimeType
  136. }
  137. this.$emit('upload',data,this.filename)
  138. } else {
  139. uni.showToast({
  140. title: res.data.error || res.data.msg ,
  141. icon: 'none',
  142. duration: 2000
  143. })
  144. }
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .Oss{
  151. display: flex;
  152. flex-direction: column;
  153. align-items: center;
  154. background-color: #f2f2f2;
  155. }
  156. </style>