image.vue
5.35 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<template>
<div class="image_upload_page">
<!-- 图片列表 开始 -->
<div class="img_cell" v-for="(item,index) in imgList" :key="index">
<div class="el_img_box">
<i class="el-icon-error el_icon" @click="removeImage(item)" v-if="readStatus != 1"></i>
<el-image class="el_img" :src="item.path" :preview-src-list="getPreViewList(item.path,imgList)"></el-image>
</div>
</div>
<!-- 图片列表 end -->
<!-- 上传 开始 -->
<el-upload class="el_upload" v-if="showUpload()" :action="uploadUrl" list-type="picture-card" :accept="acceptFile" :show-file-list="false" :on-change="onChange" :http-request="uploadRequest">
<i class="el-icon-plus"></i>
</el-upload>
<!-- 上传 end -->
</div>
</template>
<script>
import axios from "axios";
import sign from "@/utils/sign";
import qs from "qs";
import { DELETE_ORDER_IMAGE } from "@/services/order";
import { showToast } from "@/utils/util";
export default {
name: "ImageUpload",
props: {
value: {
type: Array,
default: function () {
return [];
},
},
status: {
type: [String, Number],
default: "",
},
limit: {
type: [String, Number],
default: 20,
},
},
data() {
return {
imgList: [],
uploadUrl: "",
readStatus: "",
acceptFile: ".jpg,.png",
previewList: [],
};
},
watch: {
value: {
handler: function (n, o) {
this.imgList = n;
},
immediate: true,
},
status: {
handler: function (n, o) {
this.readStatus = n;
},
immediate: true,
},
},
mounted() {},
methods: {
// 监听选择图片
onChange(file, fileList) {
console.log(file);
},
// 图片转换成base64
toBase64(file) {
let _this = this;
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const imgFile = event.target.result;
const arrBase64 = imgFile.split(",");
let arr = file.name.split(".");
let _type = arr[arr.length - 1];
let fileInfo = _type + "@" + arrBase64[1];
let oParams = {
fileName: file.name,
file: fileInfo,
};
_this.uploadImage(oParams);
};
},
// 自定义上传
uploadRequest(e) {
let file = e.file;
if (file.size > 10 * 1024 * 1024) {
showToast({
message: "图片最大上传10M",
});
return false;
}
this.toBase64(file);
},
// 上传图片到服务器
uploadImage(params = {}, noSignAttr = ["file"]) {
let _this = this;
const fileUploadURL = "https://didicb.glsx.com.cn" || localStorage.getItem("FILE_UPLOAD_URL");
//const fileUploadURL = 'http://qcdidicb.dev.glsx.com.cn:7060' // 给java后台测试用的地址
params.source = "glsx-oss-carowner";
params.app_key = "48e5e13229b82c1b4e6e8c96151f0637";
params.timestamp = new Date().getTime();
params.v = "1.0.0";
params.format = "json";
params.method = "glsx.accounts.biz.common.pictureUpload";
const data = sign(params, noSignAttr);
axios
.post(
`${fileUploadURL}/router?method=${params.method}`,
qs.stringify(data)
)
.then((res) => {
console.log(res);
let response = res.data;
if (response.returnCode == 0) {
_this.imgList.push({ path: response.data });
_this.previewList.push(res.data);
console.log(_this.imgList);
//
//this.$emit("change", response.data);
_this.$emit("input", _this.imgList);
}
});
},
// 删除图片
removeImage(row) {
//数组中删除对应img
this.imgList = this.imgList.filter((k) => k.path != row.path);
console.log(this.imgList);
this.$emit("input", this.imgList);
// 删除存量图片
// if (row.id) {
// DELETE_ORDER_IMAGE({ id: row.id });
// }
},
// 是否可以上传
showUpload() {
if (this.readStatus == 1) {
return false;
}
if (this.imgList && this.imgList.length >= this.limit) {
return false;
}
return true;
},
// 获取图片预览列表
getPreViewList(url) {
let _this = this;
let list = this.imgList || [];
let arr = [];
list.map((k) => {
if (k.pah == url) {
arr.unshift(k.path);
} else {
arr.push(k.path);
}
});
return arr;
},
},
};
</script>
<style lang="scss">
.el_upload {
.el-upload--picture-card {
width: 120px !important;
height: 120px;
line-height: 118px;
}
}
</style>
<style lang="scss" scoped>
.image_upload_page {
display: flex;
flex-wrap: wrap;
}
.img_cell {
display: flex;
flex-wrap: wrap;
.el_img_box {
position: relative;
width: 120px;
height: 120px;
border-radius: 4px;
border: 1px dashed #d8d8d8;
padding: 4px;
margin: 0 10px 10px 0;
box-sizing: border-box;
.el_icon {
position: absolute;
top: 0;
right: 0;
z-index: 2;
color: #f00;
font-size: 20px;
display: none;
cursor: pointer;
}
.el_img {
width: 100%;
height: 100%;
border-radius: 4px;
}
}
.el_img_box:hover {
.el_icon {
display: block;
}
}
}
</style>