This commit is contained in:
xingyu 2022-11-22 23:34:31 +08:00
parent 0e6c187008
commit 974361041a
2 changed files with 70 additions and 51 deletions

View File

@ -1,46 +1,44 @@
<template>
<div class="user-info-head" @click="editCropper()">
<img :src="options.options.img" title="点击上传头像" class="img-circle img-lg" alt="" />
<img :src="props.img" title="点击上传头像" class="img-circle img-lg" alt="" />
</div>
<el-dialog
v-model="dialogVisible"
title="编辑头像"
:mask-closable="false"
width="800px"
append-to-body
style="padding: 30px 20px"
@opened="modalOpened"
@opened="cropperVisible = true"
>
<el-row>
<el-col :xs="24" :md="12" style="height: 350px">
<el-col :xs="24" :md="12" :style="{ height: '350px' }">
<VueCropper
ref="cropper"
v-if="cropperVisible"
:img="options.options.img"
:img="options.img"
:info="true"
:autoCrop="options.options.autoCrop"
:autoCropWidth="options.options.autoCropWidth"
:autoCropHeight="options.options.autoCropHeight"
:fixedBox="options.options.fixedBox"
@real-time="realTime"
:infoTrue="options.infoTrue"
:autoCrop="options.autoCrop"
:autoCropWidth="options.autoCropWidth"
:autoCropHeight="options.autoCropHeight"
:fixedNumber="options.fixedNumber"
:fixedBox="options.fixedBox"
:centerBox="options.centerBox"
@realTime="realTime"
/>
</el-col>
<el-col :xs="24" :md="12" style="height: 350px">
<el-col :xs="24" :md="12" :style="{ height: '350px' }">
<div
class="avatar-upload-preview"
:style="{
width: options.previews.w + 'px',
height: options.previews.h + 'px',
width: previews.w + 'px',
height: previews.h + 'px',
overflow: 'hidden',
margin: '5px'
}"
>
<div :style="options.previews.div">
<img
:src="options.previews.url"
:style="options.previews.img"
style="!max-width: 100%"
alt=""
/>
<div :style="previews.div">
<img :src="previews.url" :style="previews.img" style="!max-width: 100%" alt="" />
</div>
</div>
</el-col>
@ -88,9 +86,9 @@
</el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, watch } from 'vue'
import { ref, reactive, watch, Ref, UnwrapNestedRefs } from 'vue'
import 'vue-cropper/dist/index.css'
import { VueCropper } from 'vue-cropper'
import VueCropper from 'vue-cropper/lib/vue-cropper.vue'
import { ElRow, ElCol, ElUpload, ElMessage, ElDialog } from 'element-plus'
import { propTypes } from '@/utils/propTypes'
import { uploadAvatarApi } from '@/api/system/user/profile'
@ -101,30 +99,47 @@ const cropperVisible = ref(false)
const props = defineProps({
img: propTypes.string.def('')
})
const options = reactive({
options: {
img: props.img, //
autoCrop: true, //
autoCropWidth: 200, //
autoCropHeight: 200, //
fixedBox: true //
},
previews: {
img: '',
url: '',
w: 0,
h: 0,
div: ''
}
interface Options {
img: string | ArrayBuffer | null //
info: true //
outputSize: number // [10.1]
outputType: 'jpeg' //
canScale: boolean //
autoCrop: boolean //
autoCropWidth: number //
autoCropHeight: number //
fixedBox: boolean //
fixed: boolean //
fixedNumber: Array<number> // centerBox使
full: boolean //
canMoveBox: boolean //
original: boolean //
centerBox: boolean //
infoTrue: boolean // true false
}
const options: UnwrapNestedRefs<Options> = reactive({
img: '', //
autoCrop: true, //
autoCropWidth: 200, //
autoCropHeight: 200, //
fixedBox: false, //
info: true, //
outputSize: 1, // [10.1]
outputType: 'jpeg', //
canScale: false, //
fixed: true, //
fixedNumber: [1, 1], // centerBox使
full: true, //
canMoveBox: false, //
original: false, //
centerBox: true, //
infoTrue: true // true false
})
const previews: Ref<any> = ref({})
/** 编辑头像 */
const editCropper = () => {
dialogVisible.value = true
}
//
const modalOpened = () => {
cropperVisible.value = true
}
/** 向左旋转 */
const rotateLeft = () => {
cropper.value.rotateLeft()
@ -146,11 +161,14 @@ const beforeUpload = (file: Blob) => {
ElMessage('文件格式错误,请上传图片类型,如:JPG,PNG后缀的文件。')
} else {
const reader = new FileReader()
// base64
reader.readAsDataURL(file)
reader.onload = () => {
if (reader.result) {
options.options.img = reader.result as string
//
options.img = reader.result as string
}
return false
}
}
}
@ -160,7 +178,8 @@ const uploadImg = () => {
let formData = new FormData()
formData.append('avatarFile', data)
uploadAvatarApi(formData).then((res) => {
options.options.img = res
options.img = res
window.location.reload()
})
dialogVisible.value = false
cropperVisible.value = false
@ -168,15 +187,15 @@ const uploadImg = () => {
}
/** 实时预览 */
const realTime = (data: any) => {
options.previews = data
previews.value = data
}
watch(
() => props.img,
() => {
if (props.img) {
options.options.img = props.img
options.previews.img = props.img
options.previews.url = props.img
options.img = props.img
previews.value.img = props.img
previews.value.url = props.img
}
}
)

View File

@ -3,19 +3,19 @@
<el-table-column type="seq" title="序号" width="60" fixed="left" />
<el-table-column label="社交平台" align="left" width="120">
<template #default="{ row }">
<img style="height: 20px; vertical-align: middle" :src="row.img" alt="" />
{{ row.title }}
<img class="h-5 align-middle" :src="row.img" alt="" />
<p class="mr-5">{{ row.title }}</p>
</template>
</el-table-column>
<el-table-column label="操作" align="center">
<template #default="{ row }">
<template v-if="row.openid">
已绑定
<XTextButton type="primary" @click="unbind(row)" title="(解绑)" />
<XTextButton type="primary" class="mr-5" @click="unbind(row)" title="(解绑)" />
</template>
<template v-else>
未绑定
<XTextButton type="primary" @click="bind(row)" title="(绑定)" />
<XTextButton type="primary" class="mr-5" @click="bind(row)" title="(绑定)" />
</template>
</template>
</el-table-column>