perf: 用户管理优化头像上传

pull/229/head
xiaojunnuo 2024-10-27 00:52:26 +08:00
parent d10d42e206
commit 661293c189
4 changed files with 51 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { Provide } from '@midwayjs/core'; import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import path from 'path'; import path from 'path';
import fs from 'fs'; import fs from 'fs';
@ -14,6 +14,7 @@ export const uploadTmpFileCacheKey = 'tmpfile_key_';
/** /**
*/ */
@Provide() @Provide()
@Scope(ScopeEnum.Singleton)
export class FileService { export class FileService {
async saveFile(userId: number, tmpCacheKey: any, permission: 'public' | 'private') { async saveFile(userId: number, tmpCacheKey: any, permission: 'public' | 'private') {
if (tmpCacheKey.startsWith(`/${permission}`)) { if (tmpCacheKey.startsWith(`/${permission}`)) {

View File

@ -185,7 +185,8 @@ function install(app: App, options: any = {}) {
defaultType: "form", defaultType: "form",
form: { form: {
keepName: true, keepName: true,
action: "http://www.docmirror.cn:7070/api/upload/form/upload", type: "form",
action: "/basic/file/upload",
name: "file", name: "file",
withCredentials: false, withCredentials: false,
uploadRequest: async ({ action, file, onProgress }: any) => { uploadRequest: async ({ action, file, onProgress }: any) => {
@ -205,12 +206,8 @@ function install(app: App, options: any = {}) {
} }
}); });
}, },
successHandle(ret: any) { successHandle(res: any) {
// 上传完成后的结果处理, 此处应返回格式为{url:xxx} return res;
return {
url: "http://www.docmirror.cn:7070" + ret,
key: ret.replace("/api/upload/form/download?key=", "")
};
} }
} }
}); });

View File

@ -1,5 +1,6 @@
import * as api from "./api"; import * as api from "./api";
import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, UserPageQuery, UserPageRes } from "@fast-crud/fast-crud"; import { AddReq, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, dict, EditReq, UserPageQuery, UserPageRes } from "@fast-crud/fast-crud";
import { useUserStore } from "/@/store/modules/user";
export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet { export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOptionsRet {
const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => { const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
@ -17,6 +18,8 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
return await api.AddObj(form); return await api.AddObj(form);
}; };
const userStore = useUserStore();
return { return {
crudOptions: { crudOptions: {
request: { request: {
@ -114,6 +117,35 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
style: { style: {
height: "30px", height: "30px",
width: "auto" width: "auto"
},
buildUrl(key: string) {
return `/api/basic/file/download?&key=` + key;
}
}
},
form: {
component: {
vModel: "modelValue",
valueType: "key",
cropper: {
aspectRatio: 1,
autoCropArea: 1,
viewMode: 0
},
onReady: null,
uploader: {
type: "form",
action: "/basic/file/upload",
name: "file",
headers: {
Authorization: "Bearer " + userStore.getToken
},
successHandle(res: any) {
return res;
}
},
buildUrl(key: string) {
return `/api/basic/file/download?&key=` + key;
} }
} }
} }

View File

@ -4,7 +4,7 @@ import { Repository } from 'typeorm';
import { UserEntity } from '../entity/user.js'; import { UserEntity } from '../entity/user.js';
import * as _ from 'lodash-es'; import * as _ from 'lodash-es';
import md5 from 'md5'; import md5 from 'md5';
import { CommonException } from '@certd/lib-server'; import { CommonException, FileService } from '@certd/lib-server';
import { BaseService } from '@certd/lib-server'; import { BaseService } from '@certd/lib-server';
import { RoleService } from './role-service.js'; import { RoleService } from './role-service.js';
import { PermissionService } from './permission-service.js'; import { PermissionService } from './permission-service.js';
@ -34,6 +34,9 @@ export class UserService extends BaseService<UserEntity> {
@Inject() @Inject()
sysSettingsService: SysSettingsService; sysSettingsService: SysSettingsService;
@Inject()
fileService: FileService;
//@ts-ignore //@ts-ignore
getRepository() { getRepository() {
return this.repository; return this.repository;
@ -68,6 +71,11 @@ export class UserService extends BaseService<UserEntity> {
const plainPassword = param.password ?? RandomUtil.randomStr(6); const plainPassword = param.password ?? RandomUtil.randomStr(6);
param.passwordVersion = 2; param.passwordVersion = 2;
param.password = await this.genPassword(plainPassword, param.passwordVersion); // 默认密码 建议未改密码不能登陆 param.password = await this.genPassword(plainPassword, param.passwordVersion); // 默认密码 建议未改密码不能登陆
if (param.avatar) {
param.avatar = await this.fileService.saveFile(0, param.avatar, 'public');
}
await super.add(param); await super.add(param);
//添加角色 //添加角色
if (param.roles && param.roles.length > 0) { if (param.roles && param.roles.length > 0) {
@ -98,6 +106,10 @@ export class UserService extends BaseService<UserEntity> {
} else { } else {
delete param.password; delete param.password;
} }
if (param.avatar) {
param.avatar = await this.fileService.saveFile(userInfo.id, param.avatar, 'public');
}
await super.update(param); await super.update(param);
await this.roleService.updateRoles(param.id, param.roles); await this.roleService.updateRoles(param.id, param.roles);
} }