images.vue 3.0 KB

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