singleImg.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div class="singleImg">
  3. <div class="single">
  4. <!--<img :src="defaultImg" v-if="HeadImg===''">-->
  5. <div v-if="HeadImg === ''" class="add-wrap">
  6. <div class="defaultImg">
  7. <p class="add">+</p>
  8. <p class="text">图片</p>
  9. <p class="tips">支持扩展名:.jpg .png</p>
  10. </div>
  11. <input
  12. type="file"
  13. class="file"
  14. @change="addChangeImg"
  15. ref="inputImg"
  16. accept="image/gif, image/jpeg, image/png, image/jpg"
  17. />
  18. </div>
  19. <div
  20. v-else
  21. class="edit-wrap"
  22. :style="{ width: setWidth, height: setHeight }"
  23. >
  24. <img :src="HeadImg" />
  25. <input
  26. type="file"
  27. class="file"
  28. @change="addChangeImg"
  29. ref="inputImg"
  30. accept="image/gif, image/jpeg, image/png, image/jpg"
  31. />
  32. </div>
  33. </div>
  34. <p class="tips" v-if="size">建议尺寸:{{ size }}</p>
  35. </div>
  36. </template>
  37. <script type="text/ecmascript-6">
  38. export default {
  39. name: 'singleImg',
  40. props: {
  41. img: {
  42. type: String,
  43. },
  44. imgType: {
  45. type: String,
  46. default: 'other',
  47. },
  48. Width: {
  49. type: Number,
  50. },
  51. Height: {
  52. type: Number,
  53. },
  54. Size: {
  55. type: String,
  56. },
  57. },
  58. data() {
  59. return {
  60. defaultImg: this.$Public.defaultImg,
  61. HeadImg: this.img,
  62. setWidth: this.Width + 'px',
  63. setHeight: this.Height + 'px',
  64. size: this.Size,
  65. }
  66. },
  67. created() {},
  68. methods: {
  69. addChangeImg(e) {
  70. // 上传主图
  71. this.upPic(e, (res) => {
  72. console.log(111, res)
  73. this.$emit('getPic', res)
  74. this.HeadImg = res;
  75. this.$refs.inputImg.value = ''
  76. })
  77. },
  78. upPic(e, fun) {
  79. // 上传图片
  80. let file = e.target.files[0]
  81. if (!file) {
  82. return
  83. }
  84. this.request.upPic({ prefix: this.imgType, file: file }).then(
  85. (res) => {
  86. fun(res)
  87. },
  88. (res) => {}
  89. )
  90. },
  91. },
  92. watch: {
  93. img(val) {
  94. this.HeadImg = val
  95. },
  96. Width(val) {
  97. this.setWidth = val + 'px'
  98. },
  99. Height(val) {
  100. this.setHeight = val + 'px'
  101. },
  102. Size(val) {
  103. this.size = val
  104. },
  105. },
  106. }
  107. </script>
  108. <style lang="stylus" rel="stylesheet/stylus" scoped>
  109. @import '~assets/main.styl';
  110. .single {
  111. flex-x(flex-start);
  112. .add-wrap {
  113. position: relative;
  114. width: 130px;
  115. height: 130px;
  116. // border: 1px solid border-color;
  117. .defaultImg {
  118. width: 130px;
  119. height: 130px;
  120. border: 2px dashed border-color;
  121. background:#F6F6F6
  122. .add {
  123. font-size: 40px;
  124. color: gray3;
  125. font-weight:100
  126. line-height: 1;
  127. }
  128. .text {
  129. font-size: 14px;
  130. color: gray3;
  131. }
  132. flex-y(center, center);
  133. p {
  134. text-align: center;
  135. }
  136. }
  137. }
  138. .edit-wrap {
  139. position: relative;
  140. width: 100px;
  141. height: 100px;
  142. img {
  143. width: 100%;
  144. height: 100%;
  145. }
  146. }
  147. input {
  148. width: 100%;
  149. height: 100%;
  150. opacity: 0;
  151. position: absolute;
  152. top: 0;
  153. left: 0;
  154. }
  155. }
  156. p {
  157. float: left;
  158. width: 150px;
  159. }
  160. </style>