index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <u-upload sizeType="original" :fileList="fileList" :previewFullImage="true" @afterRead="afterRead" @delete="deletePic" multiple
  3. accept="image" :maxCount="maxCount">
  4. <view class="upload-fix flex justify-around align-center">
  5. <view class="flex">
  6. <u-icon name="camera" color="#cacaca" size="24"></u-icon>
  7. </view>
  8. </view>
  9. </u-upload>
  10. </template>
  11. <script>
  12. import {
  13. ossInfo
  14. } from '@/api/oss.js'
  15. import {
  16. getRandomNum
  17. } from '@/utils/common.js'
  18. import imgUpload from '@/utils/upload.js'
  19. export default {
  20. data() {
  21. return {
  22. uploadData: { //oss返回数据
  23. policy: undefined,
  24. Signature: undefined,
  25. OSSAccessKeyId: undefined,
  26. uploadImgUrl: undefined,
  27. dir: undefined,
  28. key: undefined
  29. },
  30. fileList: []
  31. }
  32. },
  33. props: {
  34. value: {
  35. type: Array,
  36. require: true,
  37. default () {
  38. return []
  39. }
  40. },
  41. maxCount: {
  42. type: Number,
  43. require: false,
  44. default: 4
  45. },
  46. },
  47. watch: {
  48. value: {
  49. handler(newVal, oldVal) {
  50. let newList = newVal.map(i => {
  51. return {
  52. url: i
  53. }
  54. })
  55. this.fileList = newList
  56. },
  57. immediate: true,
  58. deep: true
  59. }
  60. },
  61. methods: {
  62. oss() {
  63. return new Promise((resolve, reject) => {
  64. ossInfo().then(response => {
  65. this.uploadData.policy = response.data.policy;
  66. this.uploadData.Signature = response.data.signature;
  67. this.uploadData.OSSAccessKeyId = response.data.accessid;
  68. this.uploadData.uploadImgUrl = response.data.domain;
  69. this.uploadData.dir = response.data.dir;
  70. resolve(response)
  71. }).catch(err => {
  72. reject(err)
  73. })
  74. })
  75. },
  76. // 删除图片
  77. deletePic(event) {
  78. this.fileList.splice(event.index, 1);
  79. let newList = this.fileList.map(i => {
  80. return i.url
  81. })
  82. this.$emit('input', newList)
  83. },
  84. // 新增图片
  85. async afterRead(event) {
  86. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  87. let lists = [].concat(event.file)
  88. let fileListLen = this.fileList.length
  89. lists.map((item) => {
  90. this.fileList.push({
  91. ...item,
  92. status: 'uploading',
  93. message: '上传中'
  94. })
  95. })
  96. await this.oss() //获取oss数据
  97. for (let i = 0; i < lists.length; i++) {
  98. const result = await this.uploadFilePromise(lists[i])
  99. let item = this.fileList[fileListLen]
  100. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  101. status: 'success',
  102. message: '成功',
  103. url: result
  104. }))
  105. fileListLen++
  106. }
  107. let newList = this.fileList.map(i => {
  108. return i.url
  109. })
  110. this.$emit('input', newList)
  111. },
  112. uploadFilePromise(file) {
  113. //组装上传数据
  114. let timestamp = new Date().getTime()
  115. let randomNum = getRandomNum(10000, 99999)
  116. let imgType = file.url.substr(file.url.indexOf('.'))
  117. this.uploadData.key = `${this.uploadData.dir}${timestamp}${randomNum}${imgType}`
  118. let params = {
  119. policy: this.uploadData.policy,
  120. Signature: this.uploadData.Signature,
  121. OSSAccessKeyId: this.uploadData.OSSAccessKeyId,
  122. key: this.uploadData.key
  123. }
  124. return new Promise((resolve, reject) => {
  125. imgUpload({
  126. url: this.uploadData.uploadImgUrl,
  127. header: {
  128. isToken: true
  129. },
  130. formData: params,
  131. filePath: file.url
  132. }).then((res) => {
  133. console.log('res',res)
  134. let url = this.uploadData.uploadImgUrl + '/' + this.uploadData
  135. .key //图片路径需要前端组装
  136. console.log('图片路径', url)
  137. resolve(url)
  138. }).catch(err => {
  139. reject(err)
  140. })
  141. })
  142. },
  143. }
  144. }
  145. </script>
  146. <style lang="scss">
  147. .upload-fix{
  148. width:160rpx;
  149. height:160rpx;
  150. border: 1rpx dashed #dddddd;
  151. border-radius: 12rpx;
  152. }
  153. </style>