yf-file-upload.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <view>
  3. <view>
  4. <image v-if="fileUrl" :src="fileUrl" mode="widthFix" style="width: 120px;"></image>
  5. </view>
  6. <view v-if="fileUrl">
  7. <button size="mini" type="warn" @click="clearUpload()">清除</button>
  8. </view>
  9. <view v-else>
  10. <button size="mini" type="primary" @click="doUpload()">上传</button>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { uploadFile } from '@/common/upload.js'
  16. export default {
  17. name: 'yf-file-upload',
  18. props: {
  19. value: String,
  20. accept: {
  21. type: String,
  22. default: '*'
  23. },
  24. tips: String,
  25. listType: {
  26. type: String,
  27. default: 'picture'
  28. }
  29. },
  30. data() {
  31. return {
  32. fileUrl: '',
  33. showProgress: false,
  34. percent: 0
  35. }
  36. },
  37. watch: {
  38. // 检测查询变化
  39. value: {
  40. handler() {
  41. this.fillValue()
  42. }
  43. },
  44. // 检测查询变化
  45. fileUrl: {
  46. handler(val) {
  47. this.$emit('input', val)
  48. }
  49. }
  50. },
  51. mounted() {
  52. },
  53. created() {
  54. this.fillValue()
  55. },
  56. methods: {
  57. fillValue() {
  58. this.fileUrl = this.value
  59. },
  60. // 执行上传操作
  61. doUpload() {
  62. let that = this
  63. uni.chooseImage({
  64. count: 1,
  65. sizeType: ['copressed'],
  66. success(res) {
  67. uploadFile(res.tempFiles[0]).then(resUrl=>{
  68. console.log('上传结果', resUrl)
  69. that.$emit('input', resUrl)
  70. that.showProgress = false
  71. })
  72. }
  73. })
  74. },
  75. // 清理上传
  76. clearUpload() {
  77. this.$emit('input', '')
  78. }
  79. }
  80. }
  81. </script>