|
|
@@ -4,19 +4,29 @@
|
|
|
:action="state.action"
|
|
|
:headers="state.headers"
|
|
|
:show-file-list="false"
|
|
|
+ :multiple="multiple"
|
|
|
:on-success="handleAvatarSuccess"
|
|
|
:on-error="handleAvatarError"
|
|
|
:before-upload="beforeAvatarUpload"
|
|
|
>
|
|
|
- <img v-if="imageUrl" :src="imageUrl" class="avatar"/>
|
|
|
- <el-icon v-else class="avatar-uploader-icon">
|
|
|
- <Plus/>
|
|
|
- </el-icon>
|
|
|
+ <template v-if="multiple">
|
|
|
+ <img v-for="(img,idx) in showImageList" :key="idx" :src="img" class="avatar"/>
|
|
|
+ <el-icon v-if="showImageList.length<max" class="avatar-uploader-icon">
|
|
|
+ <Plus/>
|
|
|
+ </el-icon>
|
|
|
+ </template>
|
|
|
+ <template v-else>
|
|
|
+ <img v-if="showImageList[0]" :src="showImageList[0]" class="avatar"/>
|
|
|
+ <el-icon v-else class="avatar-uploader-icon">
|
|
|
+ <Plus/>
|
|
|
+ </el-icon>
|
|
|
+ </template>
|
|
|
+
|
|
|
</el-upload>
|
|
|
</template>
|
|
|
|
|
|
<script lang="ts" setup>
|
|
|
-import {ref, reactive, onMounted, watch} from 'vue'
|
|
|
+import {ref, reactive, onMounted, watch, computed} from 'vue'
|
|
|
import {ElMessage} from 'element-plus'
|
|
|
import {Plus} from '@element-plus/icons-vue'
|
|
|
|
|
|
@@ -30,20 +40,59 @@ const emit = defineEmits(['update:modelValue', 'on-change']);
|
|
|
const props = defineProps({
|
|
|
modelValue: {
|
|
|
type: String
|
|
|
+ },
|
|
|
+ multiple: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
+ },
|
|
|
+ max: {
|
|
|
+ type: Number,
|
|
|
+ default: 9
|
|
|
+ },
|
|
|
+ splitor: {
|
|
|
+ type: String,
|
|
|
+ default: ";"
|
|
|
}
|
|
|
})
|
|
|
|
|
|
+
|
|
|
+const typeOf = (obj: any) => {
|
|
|
+ return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
const state = reactive({
|
|
|
action: '',
|
|
|
- headers: {}
|
|
|
-})
|
|
|
-
|
|
|
-watch(() => props.modelValue, (o, v) => {
|
|
|
- imageUrl.value = u.fmt.fmtUrl(props.modelValue);
|
|
|
+ headers: {},
|
|
|
+ imageList: [] as Array<string>
|
|
|
})
|
|
|
|
|
|
const imageUrl = ref('')
|
|
|
|
|
|
+watch(() => props.modelValue, (nv, v) => {
|
|
|
+ if (nv) {
|
|
|
+ if (props.multiple) {
|
|
|
+ if (Array.isArray(nv)) {
|
|
|
+ state.imageList = nv;
|
|
|
+ } else {
|
|
|
+ state.imageList = nv.split(props.splitor);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ state.imageList = [nv]
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ state.imageList = []
|
|
|
+ }
|
|
|
+ console.log(state.imageList)
|
|
|
+ // imageUrl.value = u.fmt.fmtUrl(o);
|
|
|
+}, {immediate: true})
|
|
|
+
|
|
|
+
|
|
|
+const showImageList = computed(() => {
|
|
|
+ return state.imageList.map((k: string) => u.fmt.fmtUrl(k))
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
onMounted(() => {
|
|
|
let url = import.meta.env.VITE_API_URL;
|
|
|
if (!url) {
|
|
|
@@ -53,7 +102,7 @@ onMounted(() => {
|
|
|
state.headers = {"satoken": Session.get("token")}
|
|
|
})
|
|
|
|
|
|
-const handleAvatarError = ()=>{
|
|
|
+const handleAvatarError = () => {
|
|
|
Msg.hideLoading();
|
|
|
}
|
|
|
|
|
|
@@ -66,13 +115,31 @@ const handleAvatarSuccess: UploadProps['onSuccess'] = (
|
|
|
let {url, uuid} = response.data;
|
|
|
imageUrl.value = url;
|
|
|
// imageUrl.value = URL.createObjectURL(uploadFile.raw!)
|
|
|
+ if (props.multiple) {
|
|
|
+ state.imageList.unshift(uuid)
|
|
|
+ } else {
|
|
|
+ state.imageList = [uuid]
|
|
|
+ }
|
|
|
+ emitData()
|
|
|
+}
|
|
|
|
|
|
- emit("update:modelValue", uuid)
|
|
|
- emit("on-change", uuid)
|
|
|
+const emitData = () => {
|
|
|
+ if (props.multiple) {
|
|
|
+ emit("update:modelValue", state.imageList.join(props.splitor))
|
|
|
+ emit("on-change", state.imageList.join(props.splitor))
|
|
|
+ } else {
|
|
|
+ if (state.imageList.length > 0) {
|
|
|
+ emit("update:modelValue", state.imageList[0])
|
|
|
+ emit("on-change", state.imageList[0])
|
|
|
+ } else {
|
|
|
+ emit("update:modelValue", null)
|
|
|
+ emit("on-change", null)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
|
|
- if (rawFile.type !== 'image/jpeg'&&rawFile.type !== 'image/png') {
|
|
|
+ if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
|
|
|
ElMessage.error('Avatar picture must be JPG format!')
|
|
|
return false
|
|
|
} else if (rawFile.size / 1024 / 1024 > 2) {
|