imgUpload.vue 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <view class="imgUpload">
  3. <uni-file-picker
  4. v-model="fileList"
  5. :limit='limit'
  6. :countLength='1'
  7. :auto-upload="false"
  8. fileMediatype="image"
  9. @select="select"
  10. @delete="deleteFn"
  11. mode="grid"
  12. />
  13. </view>
  14. </template>
  15. <script>
  16. /*
  17. * imgUpload 上传
  18. */
  19. import { uploadImage } from '../api/upload.js'
  20. import { getToken } from '@/utils/auth'
  21. // import store from '@/store/index.js'
  22. // let { state:{ login } } = store
  23. let intervalTime = null
  24. const app = getApp()
  25. export default {
  26. name:"imgUpload",
  27. data(){
  28. return {
  29. fileList:[]
  30. }
  31. },
  32. props:{
  33. value:{
  34. type:Array,
  35. default:() => []
  36. },
  37. disabled:{
  38. type:Boolean,
  39. default:() => false
  40. },
  41. limit:{
  42. type:Number,
  43. default:1
  44. }
  45. },
  46. watch:{
  47. value:{
  48. immediate:true,
  49. handler(val){
  50. if(val.length){
  51. this.initData()
  52. }
  53. }
  54. }
  55. },
  56. // created(){
  57. // // this.initData()
  58. // },
  59. methods:{
  60. // 初始化数据
  61. initData(){
  62. intervalTime = setInterval(() => {
  63. if(this.fileList.length != 0){
  64. clearInterval(intervalTime)
  65. return;
  66. }
  67. let arr = [...this.value]
  68. // TODO
  69. if(arr.length != 0){
  70. let imgTitem = arr.map(img => {
  71. return {
  72. "name":img,
  73. "extname":'image',
  74. "url":this.imgFilter(img),
  75. "currentUrl":img,
  76. }
  77. })
  78. this.fileList = [...this.fileList,...imgTitem]
  79. }
  80. },500)
  81. },
  82. // 过滤图片
  83. imgFilter(url){
  84. // console.log(url,"url")
  85. let str = url.split('/slm')[1]
  86. let currentUrl = app.globalData.requestUrl + str
  87. return currentUrl
  88. },
  89. // 文件上传
  90. select(e){
  91. // loading
  92. uni.showLoading({
  93. title: '上传中'
  94. });
  95. let { tempFilePaths } = e
  96. // console.log(tempFilePaths[0])
  97. uni.uploadFile({
  98. url:getApp().globalData.requestUrl + uploadImage,
  99. filePath: tempFilePaths[0],
  100. header:{
  101. Authorization:getToken() || '',
  102. },
  103. name: 'file',
  104. // TODO
  105. success:(res) => {
  106. let { data } = res
  107. uni.showToast({
  108. title:this.$t('requst.succes'),
  109. icon:'none',
  110. duration:1000
  111. })
  112. let imgpath = JSON.parse(data).data
  113. let obj = {
  114. "name":imgpath,
  115. "extname":'image',
  116. "url":this.imgFilter(imgpath),
  117. "currentUrl":imgpath,
  118. }
  119. this.fileList = [...this.fileList,obj]
  120. // let valueArr = [...this.value,imgpath]
  121. this.$emit('update:value',this.fileList);
  122. uni.hideLoading();
  123. },
  124. fail:(err) => {
  125. uni.showToast({
  126. title:this.$t('requst.fail'),
  127. icon:'none',
  128. duration:1000
  129. })
  130. this.fileList.pop()
  131. this.$emit('update:value',this.fileList)
  132. uni.hideLoading()
  133. }
  134. })
  135. },
  136. // 文件删除
  137. deleteFn(e){
  138. let { tempFilePath,tempFile:{ currentUrl } } = e
  139. let imgArr = [...this.value]
  140. this.fileList = this.fileList.filter(item => {
  141. return item.url != tempFilePath
  142. })
  143. imgArr = imgArr.filter(item => {
  144. return currentUrl.indexOf(item) == -1
  145. })
  146. this.$emit('update:value',imgArr)
  147. }
  148. }
  149. }
  150. </script>
  151. <style scoped lang="scss">
  152. .dataCharts{
  153. width: 100%;
  154. height:100%;
  155. }
  156. </style>