feat: 初始化项目用户初始化修改
This commit is contained in:
		| @@ -176,19 +176,18 @@ export const initializeUser = async () => { | |||||||
|   console.info('[User count]', w.count); |   console.info('[User count]', w.count); | ||||||
|   const password = '2e8a305521bba54f49638ed25e46adf3'; //123456 |   const password = '2e8a305521bba54f49638ed25e46adf3'; //123456 | ||||||
|   const salt = '123'; |   const salt = '123'; | ||||||
|   const users = [{ username: 'admin' }, { username: 'user' }, { username: 'root' }]; |  | ||||||
|   if (w.count < 1) { |   if (w.count < 1) { | ||||||
|     const newUsers = await User.bulkCreate( |     const root = await User.create({ | ||||||
|       users.map((user) => { |       username: 'root', | ||||||
|         return { |       password: password, | ||||||
|           ...user, |       needChangePassword: true, | ||||||
|           password, |       type: 'user', | ||||||
|           needChangePassword: true, |       description: '系统管理员', | ||||||
|           salt, |       salt, | ||||||
|         }; |     }); | ||||||
|       }), |     const org = await User.createOrg('admin', root.id, '管理员'); | ||||||
|     ); |     console.info(' new Users name', root.username, org.username); | ||||||
|     console.info('[create new Users]', newUsers); |     console.info('new Users id', root.id, org.id); | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,7 +12,9 @@ app | |||||||
|   .define(async (ctx) => { |   .define(async (ctx) => { | ||||||
|     const state = ctx.state?.tokenUser || {}; |     const state = ctx.state?.tokenUser || {}; | ||||||
|     const { id } = state; |     const { id } = state; | ||||||
|     const user = await User.findByPk(id); |     const user = await User.findByPk(id, { | ||||||
|  |       logging: false, | ||||||
|  |     }); | ||||||
|     if (!user) { |     if (!user) { | ||||||
|       throw new CustomError(500, 'user not found'); |       throw new CustomError(500, 'user not found'); | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -23,6 +23,7 @@ app | |||||||
|           ], |           ], | ||||||
|         }, |         }, | ||||||
|       }, |       }, | ||||||
|  |       logging: false, | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|     ctx.body = list; |     ctx.body = list; | ||||||
| @@ -30,21 +31,6 @@ app | |||||||
|   }) |   }) | ||||||
|   .addTo(app); |   .addTo(app); | ||||||
|  |  | ||||||
| app |  | ||||||
|   .route({ |  | ||||||
|     path: 'org', |  | ||||||
|     key: 'get', |  | ||||||
|   }) |  | ||||||
|   .define(async (ctx) => { |  | ||||||
|     const id = ctx.query.id; |  | ||||||
|     if (!id) { |  | ||||||
|       throw new CustomError('id is required'); |  | ||||||
|     } |  | ||||||
|     ctx.body = await Org.findByPk(id); |  | ||||||
|     return ctx; |  | ||||||
|   }) |  | ||||||
|   .addTo(app); |  | ||||||
|  |  | ||||||
| app | app | ||||||
|   .route({ |   .route({ | ||||||
|     path: 'org', |     path: 'org', | ||||||
| @@ -53,10 +39,27 @@ app | |||||||
|   }) |   }) | ||||||
|   .define(async (ctx) => { |   .define(async (ctx) => { | ||||||
|     const tokenUser = ctx.state.tokenUser; |     const tokenUser = ctx.state.tokenUser; | ||||||
|     const { username, description } = ctx.query.data; |     const { username, description, id } = ctx.query.data; | ||||||
|     if (!username) { |     if (!username) { | ||||||
|       throw new CustomError('username is required'); |       throw new CustomError('username is required'); | ||||||
|     } |     } | ||||||
|  |     if (id) { | ||||||
|  |       const org = await Org.findByPk(id); | ||||||
|  |       if (!org) { | ||||||
|  |         throw new CustomError('org not found'); | ||||||
|  |       } | ||||||
|  |       org.description = description; | ||||||
|  |       await org.save(); | ||||||
|  |       const user = await User.findOne({ where: { username } }); | ||||||
|  |       user.description = description; | ||||||
|  |       await user.save(); | ||||||
|  |       ctx.body = { | ||||||
|  |         id: user.id, | ||||||
|  |         username: user.username, | ||||||
|  |         description: user.description, | ||||||
|  |       }; | ||||||
|  |       return; | ||||||
|  |     } | ||||||
|     const user = await User.createOrg(username, tokenUser.id, description); |     const user = await User.createOrg(username, tokenUser.id, description); | ||||||
|     ctx.body = { |     ctx.body = { | ||||||
|       id: user.id, |       id: user.id, | ||||||
| @@ -96,3 +99,47 @@ app | |||||||
|     ctx.body = 'success'; |     ctx.body = 'success'; | ||||||
|   }) |   }) | ||||||
|   .addTo(app); |   .addTo(app); | ||||||
|  |  | ||||||
|  | app | ||||||
|  |   .route({ | ||||||
|  |     path: 'org', | ||||||
|  |     key: 'get', | ||||||
|  |     middleware: ['auth'], | ||||||
|  |   }) | ||||||
|  |   .define(async (ctx) => { | ||||||
|  |     const tokenUser = ctx.state.tokenUser; | ||||||
|  |     const id = ctx.query.id; | ||||||
|  |     if (!id) { | ||||||
|  |       throw new CustomError('id is required'); | ||||||
|  |     } | ||||||
|  |     const org = await Org.findByPk(id); | ||||||
|  |     if (!org) { | ||||||
|  |       throw new CustomError('org not found'); | ||||||
|  |     } | ||||||
|  |     const usersIds = org.users; | ||||||
|  |     const me = usersIds.find((u) => u.uid === tokenUser.id); | ||||||
|  |     if (!me) { | ||||||
|  |       throw new CustomError('Permission denied'); | ||||||
|  |     } | ||||||
|  |     const _users = await User.findAll({ | ||||||
|  |       where: { | ||||||
|  |         id: { | ||||||
|  |           [Op.in]: usersIds.map((u) => u.uid), | ||||||
|  |         }, | ||||||
|  |       }, | ||||||
|  |     }); | ||||||
|  |  | ||||||
|  |     const users = _users.map((u) => { | ||||||
|  |       const role = usersIds.find((r) => r.uid === u.id)?.role; | ||||||
|  |       return { | ||||||
|  |         id: u.id, | ||||||
|  |         username: u.username, | ||||||
|  |         role: role, | ||||||
|  |       }; | ||||||
|  |     }); | ||||||
|  |     ctx.body = { | ||||||
|  |       org, | ||||||
|  |       users, | ||||||
|  |     }; | ||||||
|  |   }) | ||||||
|  |   .addTo(app); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user