JUpload.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <view>
  3. <view v-if="title" class="title">{{ title }}</view>
  4. <view class="upload-pane">
  5. <view class="left">
  6. <view v-if="!imgUrl" class="upload" @click="chooseImg">+</view>
  7. <image v-else-if="imgUrl" class="iamge-background" :src="imgUrl" mode="" />
  8. </view>
  9. <tui-icon v-show="imgUrl" name="delete" color="#767676" :size="18" @click="removeBackground"></tui-icon>
  10. </view>
  11. </view>
  12. </template>
  13. <script>
  14. import { IMG_UPLOAD_URL } from '../../config'
  15. export default {
  16. props: {
  17. imgUrl: String,
  18. title: String
  19. },
  20. methods: {
  21. removeBackground() {
  22. const _this = this
  23. uni.showModal({
  24. title: '提示',
  25. content: '确定删除当前图片吗?',
  26. success(res) {
  27. if (res.confirm) {
  28. _this.$emit('delete')
  29. }
  30. }
  31. })
  32. },
  33. chooseImg() {
  34. const _this = this
  35. uni.chooseImage({
  36. success: (chooseImageRes) => {
  37. uni.uploadFile({
  38. url: IMG_UPLOAD_URL,
  39. filePath: chooseImageRes.tempFiles[0].path,
  40. name: 'file',
  41. success: (uploadFileRes) => {
  42. _this.$emit('upload', JSON.parse(uploadFileRes.data).data.url)
  43. }
  44. })
  45. }
  46. })
  47. }
  48. }
  49. }
  50. </script>
  51. <style lang="less" scoped>
  52. .title {
  53. color: #3d3d3d;
  54. font-size: 24upx;
  55. margin: 48upx 0 20upx 0;
  56. }
  57. .upload-pane {
  58. border: 1upx solid #d8d8d8;
  59. padding: 32upx 24upx;
  60. box-sizing: border-box;
  61. border-radius: 20upx;
  62. display: flex;
  63. justify-content: space-between;
  64. align-items: flex-end;
  65. .delete-icon {
  66. width: 32upx;
  67. height: 36upx;
  68. }
  69. .left {
  70. display: flex;
  71. align-items: center;
  72. }
  73. .upload {
  74. margin: 0;
  75. width: 160upx;
  76. height: 160upx;
  77. background-color: #ececec;
  78. border-radius: 20upx;
  79. color: #767676;
  80. text-align: center;
  81. line-height: 160upx;
  82. font-size: 60upx;
  83. }
  84. .iamge-background {
  85. width: 160upx;
  86. height: 160upx;
  87. object-fit: cover;
  88. }
  89. }
  90. </style>