weapp_file_controller.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. module.exports = app => {
  3. class WeappFileController extends app.BaseController {
  4. async getSubProjectPermission(ctx, spid) {
  5. const sessionUser = ctx.session.sessionUser;
  6. // 如果是管理员,返回全部权限
  7. if (sessionUser.is_admin) {
  8. return ctx.service.subProjPermission.adminPermission;
  9. }
  10. // 否则获取用户在该子项目的权限
  11. const permission = await ctx.service.subProjPermission.getSubProjectUserPermission(
  12. spid,
  13. sessionUser.accountId
  14. );
  15. if (!permission) {
  16. throw '您无权查看该项目';
  17. }
  18. return permission;
  19. }
  20. /**
  21. * 获取文件列表页面数据
  22. *
  23. * @param {Object} ctx - 上下文对象
  24. */
  25. async file(ctx) {
  26. try {
  27. const { id } = ctx.query;
  28. if (!id) {
  29. ctx.body = { code: -1, msg: '缺少子项目ID', data: null };
  30. return;
  31. }
  32. // 获取子项目信息
  33. const subProject = await ctx.service.subProject.getDataById(id);
  34. if (!subProject) {
  35. ctx.body = { code: -1, msg: '子项目不存在', data: null };
  36. return;
  37. }
  38. // 获取用户在该子项目的权限信息
  39. const permission = await this.getSubProjectPermission(ctx, id);
  40. // 获取有效的归档分类
  41. const filing = await ctx.service.filing.getValidFiling(id, permission.filing_type);
  42. // const filing = await ctx.service.filing.getValidFiling(id, subProject.permission.filing_type);
  43. // 获取所有分类数据
  44. const categoryData = await ctx.service.category.getAllCategory(subProject);
  45. // 检查权限
  46. const canFiling = permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.filing.value) >= 0;
  47. const canUpload = permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.upload.value) >= 0;
  48. const canEdit = permission.file_permission.indexOf(ctx.service.subProjPermission.PermissionConst.file.editfile.value) >= 0;
  49. // 获取文件引用列表
  50. const fileReferenceList = await ctx.service.subProject.getFileReference(subProject, ctx.service.subProject.FileReferenceType.file);
  51. ctx.body = {
  52. code: 0,
  53. msg: '获取成功',
  54. data: {
  55. filing,
  56. categoryData,
  57. canFiling,
  58. canUpload,
  59. canEdit,
  60. fileReferenceList,
  61. },
  62. };
  63. } catch (err) {
  64. this.log(err);
  65. ctx.body = { code: -1, msg: err.toString(), data: null };
  66. }
  67. }
  68. /**
  69. * 获取第一层级归档分类
  70. *
  71. * @param {Object} ctx - 上下文对象
  72. */
  73. async getFirstLevelFiling(ctx) {
  74. try {
  75. const { id } = ctx.query;
  76. if (!id) {
  77. ctx.body = { code: -1, msg: '缺少子项目ID', data: null };
  78. return;
  79. }
  80. // 获取子项目信息
  81. const subProject = await ctx.service.subProject.getDataById(id);
  82. if (!subProject) {
  83. ctx.body = { code: -1, msg: '子项目不存在', data: null };
  84. return;
  85. }
  86. // 获取有效的归档分类
  87. const allFiling = await ctx.service.filing.getValidFiling(id, subProject.permission.filing_type);
  88. // 过滤出第一层级数据
  89. const firstLevelFiling = allFiling.filter(item => item.tree_level === 1);
  90. ctx.body = {
  91. code: 0,
  92. msg: '获取成功',
  93. data: {
  94. filing: firstLevelFiling,
  95. },
  96. };
  97. } catch (err) {
  98. this.log(err);
  99. ctx.body = { code: -1, msg: err.toString(), data: null };
  100. }
  101. }
  102. }
  103. return WeappFileController;
  104. };