index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <view>
  3. <view class="img_box">
  4. <u-upload
  5. @on-list-change="change"
  6. upload-text="上传图片"
  7. width="139"
  8. height="139"
  9. :max-size="10 * 1024 * 1024"
  10. max-count="6"
  11. ref="uUpload"
  12. :action="action"
  13. :auto-upload="false"
  14. ></u-upload>
  15. </view>
  16. <u-toast ref="uToast" />
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. data() {
  22. return {
  23. uploadlist: [], //上传图片数组
  24. value: '', //提交图片数组
  25. action: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload'
  26. }
  27. },
  28. created() {},
  29. methods: {
  30. change() {
  31. this.uploadlist = []
  32. this.value = ''
  33. this.$nextTick(() => {
  34. let lists = this.$refs.uUpload.lists
  35. lists.forEach((val) => {
  36. this.upload(val.url)
  37. })
  38. })
  39. },
  40. //上传图片
  41. upload(item) {
  42. uni.uploadFile({
  43. url: 'https://xj.chuanghai-tech.com/patrol-app/v1/file/upload', //仅为示例,非真实的接口地址
  44. filePath: item,
  45. name: 'file',
  46. success: (uploadFileRes) => {
  47. let data = JSON.parse(uploadFileRes.data)
  48. let showUrl = data.data
  49. this.uploadlist.push(showUrl)
  50. this.$nextTick(() => {
  51. let value = ''
  52. let len = this.uploadlist.length
  53. if (len > 0) {
  54. if (len > 1) {
  55. value = this.uploadlist[0]
  56. for (let i = 1; i < len; i++) {
  57. value = value + ',' + this.uploadlist[i]
  58. }
  59. } else {
  60. value = this.uploadlist[0]
  61. }
  62. this.value = value
  63. this.$nextTick(() => {
  64. this.$emit('changeImg', this.value)
  65. })
  66. }
  67. })
  68. },
  69. fail(data) {
  70. this.$refs.uToast.show({
  71. title: data.message,
  72. type: 'warning'
  73. })
  74. }
  75. })
  76. },
  77. //提交
  78. submit() {
  79. let newarr = JSON.parse(JSON.stringify(this.value))
  80. let item = {
  81. id: this.scan_id,
  82. value: JSON.stringify(newarr)
  83. } //提交数据
  84. this.$store.state.user.items.push(item)
  85. },
  86. reset() {
  87. this.$nextTick(() => {
  88. let len = this.$refs.uUpload.lists.length
  89. this.$refs.uUpload.lists.splice(0, len)
  90. })
  91. }
  92. }
  93. }
  94. </script>
  95. <style></style>