userBindRole.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  2. const assert = require('assert');
  3. const Service = require('egg').Service;
  4. class UserBindRoleService extends Service {
  5. constructor(ctx) {
  6. super(ctx);
  7. this.model = this.ctx.model.UserBindRole;
  8. this.roleModel = this.ctx.model.Role;
  9. }
  10. async bind({ roleCode, userId }) {
  11. assert(roleCode, '缺少角色编码');
  12. assert(userId, '缺少菜单编码');
  13. try {
  14. const bind = await this.model.findOne({ roleCode, userId });
  15. if (bind) return { errcode: -1001, errmsg: '不能重复绑定', data: '' };
  16. const res = await this.model.create({ roleCode, userId });
  17. return { errcode: 0, errmsg: '', data: res };
  18. } catch (error) {
  19. console.log(error);
  20. throw error;
  21. }
  22. }
  23. async unbind({ id }) {
  24. assert(id, '缺少ID');
  25. try {
  26. const bind = await this.model.findOne({ _id: id });
  27. if (!bind) return { errcode: -1001, errmsg: '数据不存在', data: '' };
  28. await this.model.deleteOne({ _id: id });
  29. return { errcode: 0, errmsg: 'ok', data: 'unbind' };
  30. } catch (error) {
  31. throw error;
  32. }
  33. }
  34. async queryBind({ skip, limit, userId, roleCode }) {
  35. const filter = {};
  36. if (userId) filter.userId = userId;
  37. if (roleCode) filter.roleCode = roleCode;
  38. try {
  39. const total = await this.model.find({ ...filter });
  40. let res;
  41. if (skip && limit) {
  42. res = await this.model.find({ ...filter }).skip(skip * limit).limit(limit);
  43. } else {
  44. res = await this.model.find({ ...filter });
  45. }
  46. return { errcode: 0, errmsg: '', data: res, total: total.length };
  47. } catch (error) {
  48. throw error;
  49. }
  50. }
  51. async queryRole({ userId }) {
  52. try {
  53. const total = await this.model.find({ userId });
  54. if (total.length > 0) {
  55. const list = [];
  56. await Promise.all(total.map(async e => {
  57. list.push(await this.roleModel.findOne({ code: e.roleCode }));
  58. }));
  59. return { errcode: 0, errmsg: '', data: list };
  60. }
  61. return { errcode: 0, errmsg: '', data: [] };
  62. } catch (error) {
  63. throw error;
  64. }
  65. }
  66. async batchBind({ userId, ids = [] }) {
  67. try {
  68. const res = await Promise.all(
  69. ids.filter(async e => {
  70. const bind = await this.model.findOne({ roleCode: e, userId });
  71. if (!bind) await this.model.create({ roleCode: e, userId });
  72. })
  73. );
  74. return { errcode: 0, errmsg: '', data: res };
  75. } catch (error) {
  76. throw error;
  77. }
  78. }
  79. async batchUnBind({ userId, ids = [] }) {
  80. try {
  81. const res = await Promise.all(
  82. ids.filter(async e => {
  83. const bind = await this.model.findOne({ roleCode: e, userId });
  84. if (bind) await this.model.deleteOne({ roleCode: e, userId });
  85. })
  86. );
  87. return { errcode: 0, errmsg: '', data: res };
  88. } catch (error) {
  89. throw error;
  90. }
  91. }
  92. }
  93. module.exports = UserBindRoleService;