upload.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <div class="upload">
  3. <el-button type="primary" @click="uploadHandle" v-if="!pathImg">点击上传</el-button>
  4. <div class="msg" v-if="!pathImg">只能上传jpg/png文件,且不超过2MB</div>
  5. <div class="uopload-box" v-if="pathImg">
  6. <div class="upload-img">
  7. <!-- <img :src="pathUrl + pathImg" style="width: 60px;" /> -->
  8. <el-image
  9. style="width: 60px; height: 60px"
  10. :src="pathUrl + pathImg"
  11. :zoom-rate="1.2"
  12. :max-scale="7"
  13. :min-scale="0.2"
  14. :preview-src-list="srcList"
  15. :initial-index="4"
  16. fit="cover"
  17. />
  18. </div>
  19. <div class="del" @click="delPath">删除</div>
  20. </div>
  21. </div>
  22. </template>
  23. <script setup>
  24. import { ref } from 'vue'
  25. const { proxy } = getCurrentInstance();
  26. const value = ref([])
  27. const config = ref({})
  28. const srcList = ref([])
  29. const pathUrl = ref('')
  30. const emit = defineEmits();
  31. const pathImg = ref('')
  32. const props = defineProps({
  33. propConfig:{},
  34. propValue:{},
  35. parentValue:{}
  36. })
  37. const uploadHandle = () => {
  38. const accept = '.jpg,.png'
  39. let input = document.createElement('input')
  40. input.style.display = 'none'
  41. input.type = 'file'
  42. input.accept = accept
  43. document.body.appendChild(input)
  44. input.addEventListener('change', function (e) {
  45. let txt = e.target.files[0];
  46. let s = txt.name.split('.');
  47. let suffix = '.' + s[s.length - 1];
  48. if(accept.indexOf(suffix) == -1){
  49. proxy.$message({message:'不支持该类型文件上传!'})
  50. return
  51. }
  52. let siz = 2 * 1024*1024;
  53. if(siz < txt.size){
  54. proxy.$message({message:'上传文件不能超过2M!'})
  55. return
  56. }
  57. let data = new FormData();
  58. data.append('file' , txt)
  59. proxy.request({
  60. url:'/system/oss/upload',
  61. method:'post',
  62. data:data
  63. }).then(res => {
  64. pathImg.value = res.data.fileName
  65. srcList.value[0] = pathUrl.value + res.data.fileName
  66. onChange()
  67. }).catch(err => {
  68. if(err && err.msg) {
  69. proxy.$message({
  70. showClose: true,
  71. message:err.msg,
  72. type:'error'
  73. })
  74. }
  75. })
  76. })
  77. input.click()
  78. document.body.removeChild(input)
  79. }
  80. const delPath = () => {
  81. proxy.$confirm('您确定删除该图片?', '提示', {
  82. confirmButtonText: '确定',
  83. cancelButtonText: '取消',
  84. type: 'warning'
  85. }).then(() => {
  86. pathImg.value = '';
  87. srcList.value = []
  88. onChange()
  89. }).catch(()=>{})
  90. }
  91. const onChange = () => {
  92. srcList.value[0] = pathUrl.value + pathImg.value
  93. emit('onChange',pathImg.value)
  94. }
  95. const setValue = data => {
  96. pathImg.value = data ? data : '';
  97. srcList.value[0] = pathUrl.value + pathImg.value
  98. }
  99. defineExpose({
  100. setValue
  101. })
  102. pathUrl.value = import.meta.env.VITE_APP_UPLOAD_URL
  103. if(props.propValue){
  104. setValue(props.propValue)
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. .upload{
  109. width: 100%;
  110. .msg{
  111. font-size: 12px;
  112. // padding: 8px 0;
  113. }
  114. .uopload-box{
  115. display: flex;
  116. align-items: end;
  117. .del{
  118. font-size: 12px;
  119. color: red;
  120. margin-left: 8px;
  121. cursor: pointer;
  122. }
  123. }
  124. .upload-img{
  125. height: 60px;
  126. width: 60px;
  127. border: solid 1px #EEE;
  128. overflow: hidden;
  129. overflow: hidden;
  130. display: flex;
  131. align-items: center;
  132. >img{
  133. width: 100%;
  134. }
  135. }
  136. }
  137. </style>