images.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="imgs">
  3. <view class="photo" v-for="(item, index) in list" :key="index" @click="preview(item)">
  4. <image :src="ip + item" mode="aspectFill"></image>
  5. <text class="del" @click.stop="del(item)" v-if="!read">删除</text>
  6. </view>
  7. <view class="uploads" v-if="list.length < 3 && !read" @click="chooseImage()">
  8. <view class="bw">
  9. <view class="icon">&#xe65c;</view>
  10. <view class="text" style="color: rgb(145 154 179)">选择图片</view>
  11. </view>
  12. </view>
  13. <view class="clear"></view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'images',
  19. props: {
  20. value: {
  21. type: Array
  22. },
  23. read: {
  24. type: Boolean,
  25. default: false
  26. },
  27. limit: {
  28. type: Number,
  29. default: 3
  30. }
  31. },
  32. data() {
  33. return {
  34. ip: this.http.ip,
  35. list: []
  36. };
  37. },
  38. watch: {
  39. value: {
  40. handler(newValue, oldValue) {
  41. this.list = newValue;
  42. },
  43. immediate: true
  44. }
  45. },
  46. methods: {
  47. chooseImage() {
  48. if (this.list.length < this.limit) {
  49. //照片选择
  50. uni.chooseImage({
  51. count: this.limit, //默认9
  52. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  53. success: (res) => {
  54. res.tempFilePaths.forEach((path) => {
  55. uni.showLoading({ title: '正在上传图片', mask: true });
  56. uni.uploadFile({
  57. url: this.ip + '/app/common/upload',
  58. filePath: path,
  59. name: 'file',
  60. header: { Authorization: this.getUser().token },
  61. success: (res) => {
  62. let data = JSON.parse(res.data);
  63. if (data.code == 200) {
  64. this.list.push(data.fileName);
  65. this.$emit('input', this.list);
  66. }
  67. uni.hideLoading();
  68. },
  69. fail: (res) => {
  70. uni.hideLoading();
  71. uni.showModal({ content: '图片上传失败', showCancel: false });
  72. }
  73. });
  74. });
  75. }
  76. });
  77. } else {
  78. uni.showModal({ content: '只能上传' + this.limit + '张图片', showCancel: false });
  79. }
  80. },
  81. preview(item) {
  82. let urls = [];
  83. this.value.forEach((item) => {
  84. urls.push(this.ip + item);
  85. });
  86. // 预览图片
  87. uni.previewImage({
  88. urls: urls,
  89. current: this.ip + item,
  90. success: (res) => {}
  91. });
  92. },
  93. del(item) {
  94. this.value.splice(this.value.indexOf(item), 1);
  95. this.$emit('input', this.value);
  96. this.$forceUpdate();
  97. }
  98. }
  99. };
  100. </script>
  101. <style lang="scss" scoped>
  102. .imgs {
  103. margin-top: 10px;
  104. overflow: hidden;
  105. .uploads {
  106. float: left;
  107. width: 75px;
  108. height: 75px;
  109. text-align: center;
  110. border-radius: 5px;
  111. margin: 5px 5px 0px 0px;
  112. overflow: hidden;
  113. background-color: white;
  114. border: 1px solid #e2e2e2;
  115. .bw {
  116. padding-top: 10px;
  117. .icon {
  118. font-size: 30px;
  119. display: block;
  120. float: none;
  121. }
  122. .text {
  123. font-size: 14px;
  124. color: #6c6c6c;
  125. }
  126. }
  127. }
  128. .photo {
  129. float: left;
  130. margin: 5px 10px 0px 0px;
  131. position: relative;
  132. overflow: hidden;
  133. border-radius: 5px;
  134. width: 75px;
  135. height: 75px;
  136. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.2);
  137. image {
  138. width: 75px;
  139. height: 75px;
  140. border-radius: 5px;
  141. }
  142. .del {
  143. position: absolute;
  144. top: 0px;
  145. right: 0px;
  146. background-color: #f44336;
  147. color: white;
  148. padding: 2px 6px;
  149. font-size: 12px;
  150. }
  151. }
  152. }
  153. </style>