ImgUpload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="img-container">
  3. <view class="wrapper-box" v-if="pic">
  4. <view class="wrapper" :style="{ width: width + 'vw', height: height + 'vw' }">
  5. <image class="image" mode="aspectFill" :src="pic" @click="preview(pic)" @load="hideLoading" />
  6. <text class="close-icon" @click="deletePic()">&#xe88a;</text>
  7. </view>
  8. </view>
  9. <view class="wrapper-box" v-else>
  10. <view class="add" :style="{ width: width + 'vw', height: height + 'vw' }" @click="addImg">
  11. <IconText :code="`\ue76f`" :color="addColor" size="32"></IconText>
  12. <view class="tip" v-if="tip">{{tip}}</view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script lang="ts">
  18. import {
  19. Component,
  20. Prop,
  21. Vue,
  22. Emit,
  23. Watch
  24. } from 'vue-property-decorator';
  25. @Component({})
  26. export default class MulImgUlpload extends Vue {
  27. @Prop({
  28. default: ""
  29. })
  30. imgs ? : any;
  31. @Prop({
  32. default: ""
  33. })
  34. tip ? : any;
  35. @Prop({
  36. default: 26
  37. })
  38. width ? : number;
  39. @Prop({
  40. default: 26
  41. })
  42. height ? : number;
  43. @Prop({
  44. default: '#9c3d92'
  45. })
  46. addColor ? : string;
  47. @Prop({
  48. default: false
  49. })
  50. disabled ? : boolean;
  51. @Prop({
  52. default: "点击上传图片"
  53. })
  54. label ? : any;
  55. pic: any = "";
  56. @Emit('update')
  57. changeImg(): any {
  58. return this.pic;
  59. }
  60. @Emit('cancelUpdate')
  61. cancelUpdate(): any {}
  62. @Watch('imgs', {
  63. deep: true,
  64. immediate: true,
  65. })
  66. imgsChange(val: any) {
  67. if (val) {
  68. this.pic = val;
  69. } else {
  70. this.pic = '';
  71. }
  72. }
  73. addImg(): void {
  74. // if (this.disabled) return;
  75. // 选择图库图片
  76. const that = this;
  77. uni.chooseImage({
  78. count: 1,
  79. success: (res: any) => {
  80. const tempFilePaths: any = res.tempFilePaths;
  81. uni.showLoading({
  82. title: '上传中...'
  83. });
  84. tempFilePaths.forEach((item: any, index: any) => {
  85. that.$http
  86. .upPic({
  87. filePath: item
  88. })
  89. .then((res: any) => {
  90. uni.hideLoading();
  91. this.pic = res;
  92. that.changeImg();
  93. })
  94. .catch((err: any) => {
  95. uni.hideLoading();
  96. console.log(err);
  97. });
  98. });
  99. },
  100. fail: () => {
  101. this.cancelUpdate();
  102. }
  103. });
  104. }
  105. deletePic(): void {
  106. // console.log(3333, index);
  107. if (this.disabled) return;
  108. this.pic = '';
  109. this.changeImg();
  110. }
  111. preview(url: string): void {
  112. uni.previewImage({
  113. current: url, // 当前显示图片的http链接
  114. urls: [url] // 需要预览的图片http链接列表
  115. });
  116. }
  117. hideLoading(): void {
  118. uni.hideLoading();
  119. }
  120. }
  121. </script>
  122. <style lang="scss" scoped>
  123. .img-container {
  124. width: 100%;
  125. display: flex;
  126. align-content: flex-start;
  127. flex-wrap: wrap;
  128. background: white;
  129. // padding-top: 10px;
  130. .wrapper-box {
  131. width: 1/3 * 100%;
  132. margin-bottom: vw(12);
  133. &:nth-of-type(3n+2) {
  134. @include flex-x(center);
  135. }
  136. &:nth-of-type(3n) {
  137. @include flex-x(flex-end);
  138. }
  139. .tip{
  140. margin-top: vw(10);
  141. }
  142. }
  143. .wrapper {
  144. position: relative;
  145. .image {
  146. width: 100%;
  147. height: 100%;
  148. border-radius: 3px;
  149. }
  150. .close-icon {
  151. position: absolute;
  152. width: vw(40);
  153. height: vw(40);
  154. top: 0;
  155. left: 0;
  156. right: 0;
  157. bottom: 0;
  158. margin: auto;
  159. @include icon(40, rgba(0, 0, 0, 0.5));
  160. }
  161. }
  162. .add {
  163. // width: vw(100);
  164. // height: vw(100);
  165. @include flex-y(center);
  166. color: #999;
  167. background: #F2F2F2;
  168. border: 1px solid #f5f5f5;
  169. border-radius: vw(5);
  170. box-sizing: border-box;
  171. .default-icon {
  172. @include icon(60, $btn-color);
  173. }
  174. .tips {
  175. @include word-vw(12, #cccccc);
  176. }
  177. }
  178. }
  179. </style>