150 lines
4.6 KiB
JavaScript
150 lines
4.6 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* @controller PrincipalUserController 主体用户关联管理
|
||
*/
|
||
class PrincipalUserController extends Controller {
|
||
/**
|
||
* @summary 关联列表
|
||
* @description 分页查询主体与用户的关联关系,可按主体或用户过滤
|
||
* @router get /api/principal-users
|
||
* @apikey
|
||
* @request query integer page 页码,默认 1
|
||
* @request query integer pageSize 每页条数,默认 20
|
||
* @request query integer principal_id 主体ID(必填,需当前账号已绑定该主体)
|
||
* @request query integer user_id 用户ID,不传则不过滤
|
||
* @request query string role 角色标识,精确匹配
|
||
* @request query integer status 状态:1启用 0禁用,不传则不过滤
|
||
* @response 200 principalUserListResponse success
|
||
*/
|
||
async index() {
|
||
const { ctx } = this;
|
||
const page = Number(ctx.query.page || 1);
|
||
const pageSize = Number(ctx.query.pageSize || 20);
|
||
const { user_id: userId, role, status } = ctx.query;
|
||
const principalId = ctx.state.principalId;
|
||
|
||
const where = {};
|
||
where.principal_id = principalId;
|
||
if (userId) where.user_id = Number(userId);
|
||
if (role) where.role = role;
|
||
if (status !== undefined && status !== '') where.status = Number(status);
|
||
|
||
const { count, rows } = await ctx.model.PrincipalUser.findAndCountAll({
|
||
where,
|
||
offset: (page - 1) * pageSize,
|
||
limit: pageSize,
|
||
order: [[ 'id', 'DESC' ]],
|
||
});
|
||
|
||
ctx.body = { code: 0, message: 'success', data: { list: rows, total: count, page, pageSize } };
|
||
}
|
||
|
||
/**
|
||
* @summary 关联详情
|
||
* @description 按主键查询关联记录,并带出用户与主体信息
|
||
* @router get /api/principal-users/{id}
|
||
* @apikey
|
||
* @request path integer *id 关联记录主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async show() {
|
||
const { ctx } = this;
|
||
const relation = await ctx.model.PrincipalUser.findByPk(ctx.params.id, {
|
||
include: [
|
||
{ model: ctx.model.User, as: 'user', attributes: { exclude: [ 'password', 'salt' ] } },
|
||
{ model: ctx.model.Principal, as: 'principal' },
|
||
],
|
||
});
|
||
|
||
if (!relation) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '关联记录不存在' };
|
||
return;
|
||
}
|
||
|
||
ctx.body = { code: 0, message: 'success', data: relation };
|
||
}
|
||
|
||
/**
|
||
* @summary 绑定用户到主体
|
||
* @description 建立主体与用户的关联;同一主体下同一用户仅允许一条记录
|
||
* @router post /api/principal-users
|
||
* @apikey
|
||
* @request body bindPrincipalUserRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async create() {
|
||
const { ctx } = this;
|
||
const { principal_id: principalId, user_id: userId } = ctx.request.body;
|
||
|
||
if (!principalId || !userId) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: 'principal_id 与 user_id 为必填项' };
|
||
return;
|
||
}
|
||
|
||
const exist = await ctx.model.PrincipalUser.findOne({
|
||
where: { principal_id: principalId, user_id: userId },
|
||
});
|
||
if (exist) {
|
||
ctx.status = 400;
|
||
ctx.body = { code: 400, message: '该用户已绑定到该主体' };
|
||
return;
|
||
}
|
||
|
||
const relation = await ctx.model.PrincipalUser.create(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: relation.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 更新关联信息
|
||
* @description 可修改角色、默认主体标识与状态
|
||
* @router put /api/principal-users/{id}
|
||
* @apikey
|
||
* @request path integer *id 关联记录主键ID
|
||
* @request body bindPrincipalUserRequest *body
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async update() {
|
||
const { ctx } = this;
|
||
const relation = await ctx.model.PrincipalUser.findByPk(ctx.params.id);
|
||
|
||
if (!relation) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '关联记录不存在' };
|
||
return;
|
||
}
|
||
|
||
delete ctx.request.body.principal_id;
|
||
await relation.update(ctx.request.body);
|
||
ctx.body = { code: 0, message: 'success', data: { id: relation.id } };
|
||
}
|
||
|
||
/**
|
||
* @summary 解绑用户与主体
|
||
* @description 物理删除关联记录
|
||
* @router delete /api/principal-users/{id}
|
||
* @apikey
|
||
* @request path integer *id 关联记录主键ID
|
||
* @response 200 baseResponse success
|
||
*/
|
||
async destroy() {
|
||
const { ctx } = this;
|
||
const relation = await ctx.model.PrincipalUser.findByPk(ctx.params.id);
|
||
|
||
if (!relation) {
|
||
ctx.status = 404;
|
||
ctx.body = { code: 404, message: '关联记录不存在' };
|
||
return;
|
||
}
|
||
|
||
await relation.destroy();
|
||
ctx.body = { code: 0, message: 'success', data: { id: Number(ctx.params.id) } };
|
||
}
|
||
}
|
||
|
||
module.exports = PrincipalUserController;
|