补充pure用户头像上传功能

This commit is contained in:
tyjctl
2024-09-24 14:44:06 +08:00
parent 38e112fb06
commit 8e66a9880c
5 changed files with 50 additions and 14 deletions

View File

@@ -3,5 +3,6 @@
public class UpdateIconDto
{
public string? Icon { get; set; }
public Guid? UserId { get; set; }
}
}

View File

@@ -379,7 +379,19 @@ namespace Yi.Framework.Rbac.Application.Services
/// <returns></returns>
public async Task<bool> UpdateIconAsync(UpdateIconDto input)
{
var entity = await _userRepository.GetByIdAsync(_currentUser.Id);
Guid userId;
if (input.UserId == null)
{
userId = _currentUser.GetId();
}
else
{
userId = input.UserId.Value;
}
var entity = await _userRepository.GetByIdAsync(userId);
if (entity.Icon == input.Icon)
{
return false;
@@ -390,7 +402,7 @@ namespace Yi.Framework.Rbac.Application.Services
//发布更新头像任务事件
await this.LocalEventBus.PublishAsync(
new AssignmentEventArgs(AssignmentRequirementTypeEnum.UpdateIcon, _currentUser.GetId()), false);
new AssignmentEventArgs(AssignmentRequirementTypeEnum.UpdateIcon, userId), false);
return true;
}
}

View File

@@ -2,11 +2,15 @@
import type { ResultFile } from "@/api/result";
/** 上传文件*/
export const uploadFile = (data?: object) => {
return http.request<ResultFile>("post", "/file", {
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
},
data
});
export const uploadFile = data => {
return http.request<ResultFile>(
"post",
"/file",
{ data },
{
headers: {
"Content-Type": "multipart/form-data"
}
}
);
};

View File

@@ -52,7 +52,7 @@ export const updateUserProfile = data => {
/** 只修改用户头像 */
export const updateUserIcon = data => {
return http.request<Result>("put", `/account/icon`, { data: { icon: data } });
return http.request<Result>("put", `/account/icon`, { data });
};
/** 用户密码重置 */

View File

@@ -11,6 +11,7 @@ import { addDialog } from "@/components/ReDialog";
import type { PaginationProps } from "@pureadmin/table";
import ReCropperPreview from "@/components/ReCropperPreview";
import type { FormItemProps } from "../utils/types";
import { createFormData } from "@pureadmin/utils";
import {
getKeyList,
isAllEmpty,
@@ -24,10 +25,12 @@ import {
resetUserPwd,
changeUserStatus,
updateUser,
getUserList
getUserList,
updateUserIcon
} from "@/api/system/user";
import { getRoleOption } from "@/api/system/role";
import { getDeptList } from "@/api/system/dept";
import { uploadFile } from "@/api/file";
import {
ElForm,
ElInput,
@@ -93,7 +96,7 @@ export function useUser(tableRef: Ref, treeRef: Ref) {
<el-image
fit="cover"
preview-teleported={true}
src={getFileUrl(row.avatar, userAvatar)}
src={getFileUrl(row.icon, userAvatar)}
preview-src-list={Array.of(getFileUrl(row.avatar, userAvatar))}
class="w-[24px] h-[24px] rounded-full align-middle"
/>
@@ -402,8 +405,24 @@ export function useUser(tableRef: Ref, treeRef: Ref) {
beforeSure: done => {
console.log("裁剪后的图片信息:", avatarInfo.value);
// 根据实际业务使用avatarInfo.value和row里的某些字段去调用上传头像接口即可
done(); // 关闭弹框
onSearch(); // 刷新表格数据
const formData = createFormData({
file: avatarInfo.value // file 文件
});
uploadFile(formData)
.then(async response => {
let data = {
userId: row.id,
icon: response.data[0]["id"]
};
updateUserIcon(data).then(_response2 => {
message(`头像更新成功`, { type: "success" });
done(); // 关闭弹框
onSearch(); // 刷新表格数据
});
})
.catch(error => {
message(`头像更新异常 ${error}`, { type: "error" });
});
},
closeCallBack: () => cropRef.value.hidePopover()
});