scanimg.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view>
  3. <view class="img_box u-m-t-24">
  4. <view v-for="(imgItem, im) in photoPoints" :key="imgItem.id">
  5. <view class="titl u-flex u-row-left u-m-t-12 u-m-l-8">
  6. <span>{{ im + 1 }}.{{ imgItem.name }}</span>
  7. <sup class="sup" v-if="imgItem.isRequired">*</sup>
  8. </view>
  9. <u-upload
  10. :source-type="['camera']"
  11. @on-list-change="change(imgItem.id, im)"
  12. upload-text="拍照"
  13. width="139"
  14. height="139"
  15. :max-size="10 * 1024 * 1024"
  16. max-count="6"
  17. ref="uUpload"
  18. :action="action"
  19. :auto-upload="false"
  20. ></u-upload>
  21. </view>
  22. </view>
  23. <u-toast ref="sacast" />
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. props: {
  29. //照片点数组
  30. photoPoints: {
  31. type: [Object, Array]
  32. },
  33. //拍照选项项目巡检id
  34. scan_id: {
  35. type: Number
  36. }
  37. },
  38. data() {
  39. return {
  40. uploadlist: [], //已上传图片数组
  41. images: [], //本地图片数组
  42. value: [], //提交图片数组
  43. action: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload', //没用到的
  44. load: false
  45. }
  46. },
  47. methods: {
  48. // 选择图片-暂存本地图片
  49. change(id, index) {
  50. // 重置待上传图片数组
  51. this.images[index] = {}
  52. this.images[index].id = id
  53. this.images[index].imgArr = []
  54. this.$nextTick(() => {
  55. let lists = this.$refs.uUpload[index].lists
  56. lists.forEach((val) => {
  57. this.images[index].imgArr.push(val.url)
  58. })
  59. })
  60. },
  61. //提交
  62. async submit() {
  63. for (var index = 0; index < this.images.length; index++) {
  64. const item = this.images[index]
  65. this.uploadlist[index] = {}
  66. this.uploadlist[index].imgArr = []
  67. this.uploadlist[index].id = item.id
  68. await Promise.all(item.imgArr.map((url) => this.upload(url, item.id, index)))
  69. console.log('全部图片上传完成', JSON.stringify(this.uploadlist))
  70. }
  71. console.log('执行了后续操作')
  72. //生成图片字符串
  73. let uploadArr = JSON.parse(JSON.stringify(this.uploadlist))
  74. let imgStr = ''
  75. for (let i = 0; i < uploadArr.length; i++) {
  76. let len = uploadArr[i].imgArr.length
  77. if (len > 1) {
  78. imgStr = uploadArr[i].imgArr[0]
  79. for (let j = 1; j < len; j++) {
  80. imgStr = imgStr + ',' + uploadArr[i].imgArr[j]
  81. }
  82. } else if (len == 1) {
  83. imgStr = uploadArr[i].imgArr[0]
  84. } else {
  85. return (imgStr = '')
  86. }
  87. let obj = {}
  88. obj.id = uploadArr[i].id
  89. obj.value = imgStr
  90. this.value[i] = obj
  91. }
  92. //巡检图片数据提交
  93. let newarr = JSON.parse(JSON.stringify(this.value))
  94. let item = {
  95. id: this.scan_id,
  96. value: JSON.stringify(newarr)
  97. } //提交数据
  98. this.$store.state.user.items.push(item)
  99. },
  100. //上传图片API
  101. upload(item, id, index) {
  102. return new Promise((resolve, reject) => {
  103. uni.uploadFile({
  104. url: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload',
  105. filePath: item,
  106. name: 'file',
  107. success: (uploadFileRes) => {
  108. let data = JSON.parse(uploadFileRes.data)
  109. let showUrl = data.data
  110. this.uploadlist[index].imgArr.push(showUrl)
  111. resolve()
  112. },
  113. fail: (err) => {
  114. this.$refs.sacast.show({
  115. title: '图片不得超过10M',
  116. type: 'warning'
  117. })
  118. reject()
  119. }
  120. })
  121. })
  122. },
  123. //重置表单数据
  124. reset() {
  125. this.$nextTick(() => {
  126. this.$refs.uUpload.forEach((i) => {
  127. i.lists.length = 0
  128. })
  129. })
  130. }
  131. }
  132. }
  133. </script>
  134. <style></style>