当前位置: 首页 > 后端技术 > Node.js

基于nest.js的用户组织权限管理系统

时间:2023-04-03 16:23:29 Node.js

用户组织权限管理系统技术栈:前端:Vue+ElementUi+TypeScript后端:nest.js+mysql+redis演示地址用户组织管理系统(演示地址)github功能设计数据库设计用户实体@Entity()exportclassUser{@PrimaryGeneratedColumn()id:number;@Column({长度:500})名称:字符串;@Column({nullable:true,type:'text'})desc:string;@Column({nullable:true,length:100,select:false,})password:string;@Column({select:false})电子邮件:字符串;@Column({nullable:true})年龄:字符串;@Column({nullable:true})地址:字符串;@Column({nullable:true})昵称:字符串;@Column({default:0})状态:数字;@ManyToOne(type=>Role,role=>role.users)角色:角色;@ManyToMany(type=>Organization,orientation=>orientation.users)organizations:组织[];@Column({default:0})isDelete:number;@Column({default:'',nullable:true})crateTime:string;@Column({default:'',nullable:true})updateTime:string;@Column({default:'',nullable:true})deleteTime:string;}角色实体@Entity()exportclassRole{@PrimaryGeneratedColumn()id:number;@Column({长度:500})名称:字符串;@Column('text',{nullable:true})desc:string;@Column()代码:字符串;@ManyToMany(type=>Authority,authority=>authority.roles)@JoinTable()authority:Authority[];@OneToMany(type=>User,user=>user.role)users:User[];@Column({default:0})isDelete:number;@Column({default:'',nullable:true})crateTime:string;@Column({default:'',nullable:true})updateTime:string;@Column({default:'',nullable:true})deleteTime:string;}资源实体@Entity()exportclassAuthority{@PrimaryGeneratedColumn()id:number;@Column({长度:500})名称:字符串;@Column('text',{nullable:true})desc:string;@Column()路径:字符串;@Column()值:字符串;@Column()parentId:数字;@Column({default:'',nullable:true})parentName:string;@Column({nullable:true})图标:字符串;@Column({nullable:false})系统:字符串;@Column()代码:字符串;@ManyToMany(type=>Role,role=>role.authority)角色:角色[];@Column({default:0})isDelete:number;@Column({default:'',nullable:true})crateTime:string;@Column({default:'',nullable:true})updateTime:string;@Column({default:'',nullable:true})deleteTime:string;}API数据仓库配置(main.module.ts)TypeOrmModule.forRoot({type:'mysql',host:mysqlConfig.host,port:3306,用户名:mysqlConfig.userName,密码:mysqlConfig.password,数据库:'b_simple_user_center',实体:[join(__dirname,'**/**.entity{.ts,.js}')],同步:true,},),全局存储CacheModule.register({store:redisStore,host:redisCacheConfig.host,port:redisCacheConfig.port,ttl:redisCacheConfig.ttl,//secondsmax:redisCacheConfig.max,//seconds}),业务层实现系统使用typeorm操作数据库。常见的typeorm操作方式有EntityManager和QueryBuilder,结合系统的多条件查询场景,所以采用QueryBuilder方式,控制层和服务层一一对应,参考代码中对src/controller、src/service挖掘点嵌套多条件查询...constqueryConditionList=['c.isDelete=:isDelete'];if(query.name){queryConditionList.push('c.nameLIKE:name');}constqueryCondition=queryConditionList.join('AND');constres=awaitthis.productBrandRepository.createQueryBuilder('c').where(queryCondition,{name:`%${query.name}%`,isDelete:0,}).orderBy('c.name','ASC').skip((query.page-1)*query.pageSize).take(query.pageSize).getManyAndCount();..typeorm中任意实体之间的实体联合查询(实体关系之间没有表关联),getManyAndCount无法查询相关字段,必须使用getRawAndEntities,typeorm文档写错了。...constres=等待这个。产品属性Re存储库.createQueryBuilder('c').leftJoinAndSelect(ProductAttributeCategoryEntity,'a','a.id=c.product_attribute_category_id').where(queryCondition,{name:`%${query.name}%`,isDelete:0,productAttributeCategoryId:Number(query.cateAttrId),类型:Number(query.type),}).orderBy('c.name','ASC').skip((query.page-1)*query.pageSize).take(query.pageSize).getRawAndEntities();...JWT用户认证拆分成单独的服务用户系统和身份认证拆分成单独的服务(网关服务)