scanimg.vue 3.5 KB

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