随笔 - 34  文章 - 0  评论 - 1  阅读 - 29646 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<template>
  <!--
    上传控件
    用法:
     <upload-widget v-model="imgUrl"></upload-widget>
  -->
  <div class="clearfix">
    <a-upload
      :action="manageApi + '/thumbController/upload.do'"
      :data="fileUrl"
      list-type="picture-card"
      :file-list="fileInfos"
      :headers="headers"
      @preview="handlePreview"
      @change="handleChange"
      :remove="handelRemove"
      accept="image/*"
      :showUploadList="{ showPreviewIcon: this.showPreviewIcon, showRemoveIcon: this.showRemoveIcon }"(显示眼睛和隐藏判断)
    >
      <a-icon type="plus" />
      <div class="ant-upload-text">Upload</div>
    </a-upload>
    <!-- 图片预览 -->
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%; height: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>
 
<script>
import axios from 'axios';
 
/**把图片转成BASE64 */
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}
export default {
  name: 'UploadAlbumWidget',
  model: {
    prop: 'fileList',
    event: 'change',
  },
  props: {
    //最大上传数量
    maxUploadNum: {
      type: Number,
      default: 1,
    },
    /**文件列表 */
    fileList: {
      type: [Array, String],
      default() {
        return '';
      },
    },
    type: {
      type: String,
    },
    showPreviewIcon: {
      type: Boolean,
      default() {
        return true;
      },
    },
    showRemoveIcon: {
      type: Boolean,
      default() {
        return true;
      },
    },
  },
  data() {
    return {
      headers: {}, //头
      previewVisible: false,
      previewImage: '',
      fileInfos: [], //上传文件
    };
  },
  created() {
    // this.initVModelData();
    /**默认添加验证token */
    this.headers = {
      token: this.store.user.token,
      adminToken: this.store.admin.token,
    };
  },
  methods: {
    fileUrl() {
      return {
        type: this.type,
      };
    },
    /**处理初始v-model数据 */
    initVModelData() {
      let that = this;
      console.log('initVModelData', this.fileList);
      //判断文件上传是否多个
      if (this.fileList) {
        //多文件
        that.fileList.forEach((fl, index) => {
          that.fileInfos.push({
            uid: '-' + index,
            name: '图集',
            status: 'done',
            url: process.env.VUE_APP_IMAGE_SERVER + fl.imgPath,(process.env.VUE_APP_IMAGE_SERVER =》》》》  )
            thumbUrl: process.env.VUE_APP_IMAGE_SERVER + fl.thumbPath,
            response: {
              data: [{ id: fl.id }],
            },
          });
        });
        // }
      }
    },
    handleCancel() {
      this.previewVisible = false;
    },
    /**预览图 */
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      console.log('handlePreview', file);
      this.previewImage = file.thumbUrl || file.preview;
      this.previewVisible = true;
    },
 
    handleChange({ fileList }) {
      this.fileInfos = fileList;
    },
    handelRemove(file) {
      console.log('handelRemove ');
      let result = axios({
        method: 'post',
        url: `thumbController/del.do`,
        data: {
          id: file.response.data[0].id,
        },
      });
      if (result.data.code == 200) {
        this.$message.success('删除成功');
      } else {
        this.$message.error(result.data.message);
        return false;
      }
    },
  },
  watch: {
    /**检测v-model数据是否发生改变 */
    fileList(val) {
      this.initVModelData();
    },
  },
};
</script>
 
<style scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}
 
.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>

 

 

 

 

 

posted on   前端学习/vue  阅读(345)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
点击右上角即可分享
微信分享提示