mirror of https://github.com/certd/certd
perf: 用户管理优化头像上传
parent
d10d42e206
commit
661293c189
|
@ -1,4 +1,4 @@
|
|||
import { Provide } from '@midwayjs/core';
|
||||
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import dayjs from 'dayjs';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
@ -14,6 +14,7 @@ export const uploadTmpFileCacheKey = 'tmpfile_key_';
|
|||
/**
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class FileService {
|
||||
async saveFile(userId: number, tmpCacheKey: any, permission: 'public' | 'private') {
|
||||
if (tmpCacheKey.startsWith(`/${permission}`)) {
|
||||
|
|
|
@ -185,7 +185,8 @@ function install(app: App, options: any = {}) {
|
|||
defaultType: "form",
|
||||
form: {
|
||||
keepName: true,
|
||||
action: "http://www.docmirror.cn:7070/api/upload/form/upload",
|
||||
type: "form",
|
||||
action: "/basic/file/upload",
|
||||
name: "file",
|
||||
withCredentials: false,
|
||||
uploadRequest: async ({ action, file, onProgress }: any) => {
|
||||
|
@ -205,12 +206,8 @@ function install(app: App, options: any = {}) {
|
|||
}
|
||||
});
|
||||
},
|
||||
successHandle(ret: any) {
|
||||
// 上传完成后的结果处理, 此处应返回格式为{url:xxx}
|
||||
return {
|
||||
url: "http://www.docmirror.cn:7070" + ret,
|
||||
key: ret.replace("/api/upload/form/download?key=", "")
|
||||
};
|
||||
successHandle(res: any) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as api from "./api";
|
||||
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 {
|
||||
const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
|
||||
|
@ -17,6 +18,8 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
|
|||
return await api.AddObj(form);
|
||||
};
|
||||
|
||||
const userStore = useUserStore();
|
||||
|
||||
return {
|
||||
crudOptions: {
|
||||
request: {
|
||||
|
@ -114,6 +117,35 @@ export default function ({ crudExpose }: CreateCrudOptionsProps): CreateCrudOpti
|
|||
style: {
|
||||
height: "30px",
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import { Repository } from 'typeorm';
|
|||
import { UserEntity } from '../entity/user.js';
|
||||
import * as _ from 'lodash-es';
|
||||
import md5 from 'md5';
|
||||
import { CommonException } from '@certd/lib-server';
|
||||
import { CommonException, FileService } from '@certd/lib-server';
|
||||
import { BaseService } from '@certd/lib-server';
|
||||
import { RoleService } from './role-service.js';
|
||||
import { PermissionService } from './permission-service.js';
|
||||
|
@ -34,6 +34,9 @@ export class UserService extends BaseService<UserEntity> {
|
|||
@Inject()
|
||||
sysSettingsService: SysSettingsService;
|
||||
|
||||
@Inject()
|
||||
fileService: FileService;
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
|
@ -68,6 +71,11 @@ export class UserService extends BaseService<UserEntity> {
|
|||
const plainPassword = param.password ?? RandomUtil.randomStr(6);
|
||||
param.passwordVersion = 2;
|
||||
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);
|
||||
//添加角色
|
||||
if (param.roles && param.roles.length > 0) {
|
||||
|
@ -98,6 +106,10 @@ export class UserService extends BaseService<UserEntity> {
|
|||
} else {
|
||||
delete param.password;
|
||||
}
|
||||
|
||||
if (param.avatar) {
|
||||
param.avatar = await this.fileService.saveFile(userInfo.id, param.avatar, 'public');
|
||||
}
|
||||
await super.update(param);
|
||||
await this.roleService.updateRoles(param.id, param.roles);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue