project.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. /**
  3. * 项目数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Project extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'project';
  20. // 状态相关
  21. this.status = {
  22. TRY: 1,
  23. NORMAL: 2,
  24. DISABLE: 3,
  25. };
  26. }
  27. /**
  28. * 数据规则
  29. *
  30. * @param {String} scene - 场景
  31. * @return {Object} - 返回数据规则
  32. */
  33. rule(scene) {
  34. let rule = {};
  35. switch (scene) {
  36. case 'saveInfo':
  37. rule = {
  38. name: { type: 'string', required: true, min: 2 },
  39. };
  40. break;
  41. default:
  42. break;
  43. }
  44. return rule;
  45. }
  46. /**
  47. * 根据项目code获取项目数据
  48. *
  49. * @param {String} code - 项目code
  50. * @return {Object} - 返回项目数据
  51. */
  52. async getProjectByCode(code) {
  53. // 获取项目状态为非禁止的项目
  54. this.initSqlBuilder();
  55. this.sqlBuilder.setAndWhere('code', {
  56. value: this.db.escape(code),
  57. operate: '=',
  58. });
  59. this.sqlBuilder.setAndWhere('status', {
  60. value: this.status.DISABLE,
  61. operate: '<',
  62. });
  63. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  64. const projectData = await this.db.queryOne(sql, sqlParam);
  65. return projectData;
  66. }
  67. /**
  68. * 根据项目code获取项目数据
  69. *
  70. * @param {int} prjId - id
  71. * @return {Object} - 返回项目数据
  72. */
  73. async getProjectById(prjId) {
  74. // 获取项目状态为非禁止的项目
  75. this.initSqlBuilder();
  76. this.sqlBuilder.setAndWhere('id', {
  77. value: this.db.escape(prjId),
  78. operate: '=',
  79. });
  80. this.sqlBuilder.setAndWhere('status', {
  81. value: this.status.DISABLE,
  82. operate: '<',
  83. });
  84. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  85. const projectData = await this.db.queryOne(sql, sqlParam);
  86. return projectData;
  87. }
  88. /**
  89. * 切换项目
  90. *
  91. * @param {Number} projectId - 项目id
  92. * @return {Boolean} - 返回切换结果
  93. */
  94. async switchProject(projectId) {
  95. // 获取该用户拥有的项目数据
  96. const sessionUser = this.ctx.session.sessionUser;
  97. const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  98. let result = false;
  99. // 判断切换的项目是否属于对应用户
  100. if (projectInfo.length < 0) {
  101. return result;
  102. }
  103. let targetProject = {};
  104. for (const tmp of projectInfo) {
  105. if (tmp.id === projectId) {
  106. result = true;
  107. targetProject = tmp;
  108. }
  109. }
  110. // 成功后更改session
  111. if (result) {
  112. this.ctx.session.sessionProject = {
  113. id: targetProject.id,
  114. name: targetProject.name,
  115. userAccount: targetProject.user_account,
  116. };
  117. }
  118. return result;
  119. }
  120. async getPageshow(id) {
  121. const projectData = await this.db.get(this.tableName, { id });
  122. return projectData.page_show ? JSON.parse(projectData.page_show) : null;
  123. }
  124. }
  125. return Project;
  126. };