jp-form-upload.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <view class="margin-top grid col-4 grid-square flex-sub">
  3. <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="item.url">
  4. <view class="cu-avatar lg margin-left-sm" v-if="!disabled" @tap.stop="DelImg" :data-index="index" :style="`background-image:url('${item.url}')`" >
  5. <text class='cuIcon-close'></text>
  6. </view>
  7. </view>
  8. <view class="solids" v-if="!disabled" @tap="ChooseImage">
  9. <text class='cuIcon-cameraadd'></text>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import fileService from "@/api/file/fileService"
  15. export default {
  16. data() {
  17. return {
  18. imgList: []
  19. }
  20. },
  21. props: {
  22. value: {
  23. type: Array,
  24. default: function () {
  25. return []
  26. }
  27. },
  28. disabled: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. watch:{
  34. value:{
  35. handler (val) {
  36. this.imgList = val
  37. },
  38. immediate: true,
  39. deep: false
  40. }
  41. },
  42. methods: {
  43. ChooseImage() {
  44. uni.chooseImage({
  45. count: 4, //默认9
  46. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  47. sourceType: ['album'], //从相册选择
  48. success: (res) => {
  49. this.upload(res.tempFilePaths[0])
  50. }
  51. });
  52. },
  53. ViewImage(e) {
  54. let urls = this.imgList.map((img)=>{
  55. return img.url
  56. })
  57. uni.previewImage({
  58. urls: urls,
  59. current: e.currentTarget.dataset.url
  60. });
  61. },
  62. DelImg(e) {
  63. uni.showModal({
  64. title: '提示',
  65. content: '确定要删除图片吗?',
  66. cancelText: '取消',
  67. confirmText: '确定',
  68. success: res => {
  69. if (res.confirm) {
  70. this.imgList.splice(e.currentTarget.dataset.index, 1)
  71. this.$emit('input', this.imgList)
  72. }
  73. }
  74. })
  75. },
  76. upload(img) {
  77. fileService.upload(img).then((res)=>{
  78. this.imgList.push({url:res, keys:''})
  79. this.$emit('input',this.imgList)
  80. })
  81. }
  82. }
  83. }
  84. </script>
  85. <style>
  86. .btn-logout {
  87. margin-top: 100upx;
  88. width: 80%;
  89. border-radius: 50upx;
  90. font-size: 16px;
  91. color: #fff;
  92. background: linear-gradient(to right, #365fff, #36bbff);
  93. }
  94. .btn-logout-hover {
  95. background: linear-gradient(to right, #365fdd, #36bbfa);
  96. }
  97. uni-image>div, uni-image>img {
  98. width: 100upx !important;
  99. height: 100upx !important;
  100. }
  101. </style>