123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- 'use strict';
- /**
- * 项目数据模型
- *
- * @author CaiAoLin
- * @date 2017/11/16
- * @version
- */
- module.exports = app => {
- class Project extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'project';
- // 状态相关
- this.status = {
- TRY: 1,
- NORMAL: 2,
- DISABLE: 3,
- };
- }
- /**
- * 数据规则
- *
- * @param {String} scene - 场景
- * @return {Object} - 返回数据规则
- */
- rule(scene) {
- let rule = {};
- switch (scene) {
- case 'saveInfo':
- rule = {
- name: { type: 'string', required: true, min: 2 },
- };
- break;
- default:
- break;
- }
- return rule;
- }
- /**
- * 根据项目code获取项目数据
- *
- * @param {String} code - 项目code
- * @return {Object} - 返回项目数据
- */
- async getProjectByCode(code) {
- // 获取项目状态为非禁止的项目
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('code', {
- value: this.db.escape(code),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('status', {
- value: this.status.DISABLE,
- operate: '<',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const projectData = await this.db.queryOne(sql, sqlParam);
- return projectData;
- }
- /**
- * 根据项目code获取项目数据
- *
- * @param {int} prjId - id
- * @return {Object} - 返回项目数据
- */
- async getProjectById(prjId) {
- // 获取项目状态为非禁止的项目
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('id', {
- value: this.db.escape(prjId),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('status', {
- value: this.status.DISABLE,
- operate: '<',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const projectData = await this.db.queryOne(sql, sqlParam);
- return projectData;
- }
- /**
- * 切换项目
- *
- * @param {Number} projectId - 项目id
- * @return {Boolean} - 返回切换结果
- */
- async switchProject(projectId) {
- // 获取该用户拥有的项目数据
- const sessionUser = this.ctx.session.sessionUser;
- const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
- let result = false;
- // 判断切换的项目是否属于对应用户
- if (projectInfo.length < 0) {
- return result;
- }
- let targetProject = {};
- for (const tmp of projectInfo) {
- if (tmp.id === projectId) {
- result = true;
- targetProject = tmp;
- }
- }
- // 成功后更改session
- if (result) {
- this.ctx.session.sessionProject = {
- id: targetProject.id,
- name: targetProject.name,
- userAccount: targetProject.user_account,
- };
- }
- return result;
- }
- async getPageshow(id) {
- const projectData = await this.db.get(this.tableName, { id });
- return projectData.page_show ? JSON.parse(projectData.page_show) : null;
- }
- }
- return Project;
- };
|