This commit is contained in:
quantulr
2023-07-31 09:12:25 +08:00
parent 9fe9a45d2b
commit f0bf63e43f
2 changed files with 47 additions and 32 deletions

View File

@ -59,9 +59,9 @@ const useUserStore = defineStore("user", {
.then((res) => { .then((res) => {
const user = res.user; const user = res.user;
const avatar = const avatar =
user.avatar == "" || user.avatar == null /* user.avatar == "" || user.avatar == null
? defAva ? defAva
: import.meta.env.VITE_APP_BASE_API + user.avatar; : import.meta.env.VITE_APP_BASE_API + */user.avatar;
if (res.roles && res.roles.length > 0) { if (res.roles && res.roles.length > 0) {
// 验证返回的roles是否是一个非空数组 // 验证返回的roles是否是一个非空数组

View File

@ -1,47 +1,49 @@
<template> <template>
<div class="user-info-head" @click="editCropper()"> <div class="user-info-head" @click="editCropper()">
<img :src="options.img" title="点击上传头像" class="img-circle img-lg" /> <img :src="options.img" class="img-circle img-lg" title="点击上传头像"/>
</div> </div>
<el-dialog <el-dialog
:title="title" v-model="open"
v-model="open" :title="title"
width="800px" append-to-body
append-to-body width="800px"
@opened="modalOpened" @close="closeDialog"
@close="closeDialog" @opened="modalOpened"
> >
<el-row> <el-row>
<el-col :xs="24" :md="12" :style="{ height: '350px' }"> <el-col :md="12" :style="{ height: '350px' }" :xs="24">
<vue-cropper <vue-cropper
ref="cropper" v-if="visible"
:img="options.img" ref="cropper"
:info="true" :autoCrop="options.autoCrop"
:autoCrop="options.autoCrop" :autoCropHeight="options.autoCropHeight"
:autoCropWidth="options.autoCropWidth" :autoCropWidth="options.autoCropWidth"
:autoCropHeight="options.autoCropHeight" :fixedBox="options.fixedBox"
:fixedBox="options.fixedBox" :img="options.img"
@realTime="realTime" :info="true"
v-if="visible" @realTime="realTime"
/> />
</el-col> </el-col>
<el-col :xs="24" :md="12" :style="{ height: '350px' }"> <el-col :md="12" :style="{ height: '350px' }" :xs="24">
<div class="avatar-upload-preview"> <div class="avatar-upload-preview">
<img :src="options.previews.url" :style="options.previews.img" /> <img :src="options.previews.url" :style="options.previews.img"/>
</div> </div>
</el-col> </el-col>
</el-row> </el-row>
<br /> <br/>
<el-row> <el-row>
<el-col :lg="2" :md="2"> <el-col :lg="2" :md="2">
<el-upload <el-upload
action="#" :before-upload="beforeUpload"
:http-request="requestUpload" :http-request="requestUpload"
:show-file-list="false" :show-file-list="false"
:before-upload="beforeUpload" action="#"
> >
<el-button> <el-button>
选择 选择
<el-icon class="el-icon--right"><Upload /></el-icon> <el-icon class="el-icon--right">
<Upload/>
</el-icon>
</el-button> </el-button>
</el-upload> </el-upload>
</el-col> </el-col>
@ -66,16 +68,17 @@
<script setup> <script setup>
import "vue-cropper/dist/index.css"; import "vue-cropper/dist/index.css";
import { VueCropper } from "vue-cropper"; import {VueCropper} from "vue-cropper";
import { uploadAvatar } from "@/api/system/user"; import {uploadAvatar} from "@/api/system/user";
import useUserStore from "@/store/modules/user"; import useUserStore from "@/store/modules/user";
const userStore = useUserStore(); const userStore = useUserStore();
const { proxy } = getCurrentInstance(); const {proxy} = getCurrentInstance();
const open = ref(false); const open = ref(false);
const visible = ref(false); const visible = ref(false);
const title = ref("修改头像"); const title = ref("修改头像");
const emit = defineEmits(["uploaded"])
//图片裁剪数据 //图片裁剪数据
const options = reactive({ const options = reactive({
@ -91,30 +94,37 @@ const options = reactive({
function editCropper() { function editCropper() {
open.value = true; open.value = true;
} }
/** 打开弹出层结束时的回调 */ /** 打开弹出层结束时的回调 */
function modalOpened() { function modalOpened() {
visible.value = true; visible.value = true;
} }
/** 覆盖默认上传行为 */ /** 覆盖默认上传行为 */
function requestUpload() {} function requestUpload() {
}
/** 向左旋转 */ /** 向左旋转 */
function rotateLeft() { function rotateLeft() {
proxy.$refs.cropper.rotateLeft(); proxy.$refs.cropper.rotateLeft();
} }
/** 向右旋转 */ /** 向右旋转 */
function rotateRight() { function rotateRight() {
proxy.$refs.cropper.rotateRight(); proxy.$refs.cropper.rotateRight();
} }
/** 图片缩放 */ /** 图片缩放 */
function changeScale(num) { function changeScale(num) {
num = num || 1; num = num || 1;
proxy.$refs.cropper.changeScale(num); proxy.$refs.cropper.changeScale(num);
} }
/** 上传预处理 */ /** 上传预处理 */
function beforeUpload(file) { function beforeUpload(file) {
if (file.type.indexOf("image/") == -1) { if (file.type.indexOf("image/") == -1) {
proxy.$modal.msgError( proxy.$modal.msgError(
"文件格式错误,请上传图片类型,如JPGPNG后缀的文件。" "文件格式错误,请上传图片类型,如JPGPNG后缀的文件。"
); );
} else { } else {
const reader = new FileReader(); const reader = new FileReader();
@ -124,24 +134,29 @@ function beforeUpload(file) {
}; };
} }
} }
/** 上传图片 */ /** 上传图片 */
function uploadImg() { function uploadImg() {
proxy.$refs.cropper.getCropBlob((data) => { proxy.$refs.cropper.getCropBlob((data) => {
let formData = new FormData(); let formData = new FormData();
formData.append("avatarfile", data); formData.append("avatarfile", data);
uploadAvatar(formData).then((response) => { uploadAvatar(formData).then((response) => {
console.log(response)
open.value = false; open.value = false;
options.img = import.meta.env.VITE_APP_BASE_API + response.imgUrl; options.img = import.meta.env.VITE_APP_BASE_API + response.imgUrl;
userStore.avatar = options.img; userStore.avatar = options.img;
proxy.$modal.msgSuccess("修改成功"); proxy.$modal.msgSuccess("修改成功");
visible.value = false; visible.value = false;
emit("uploaded")
}); });
}); });
} }
/** 实时预览 */ /** 实时预览 */
function realTime(data) { function realTime(data) {
options.previews = data; options.previews = data;
} }
/** 关闭窗口 */ /** 关闭窗口 */
function closeDialog() { function closeDialog() {
options.img = userStore.avatar; options.img = userStore.avatar;