'use strict'; /** * Created by LanJianRong on 2020/7/6. * 项目设置->显示设置表数据模型 * @author LanJianRong * @date 2020/07/06 * @version */ const { listPath } = require('../const/setting'); const BaseService = require('../base/base_service'); module.exports = app => { class settingShow extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'setting_show'; } /** * 获取表的全部记录 * @param {Number} pid 项目id * @return {Array} 查询结果集 */ async getList(pid) { const record = await this.getAllDataByCondition({ pid }); return record.map(item => { // 拼接路由path const { sid } = item; item.path = listPath[sid].path; return item; }); } /** * 设置默认显示字段 * @param {Number} id 标签id * @param {Number} pid 项目id * @return {Promise} 查询结果集 */ async setDefaultLabel(id, pid) { const record = await this.getDataByCondition({ pid, is_default: 1 }); if (!record) throw '该标签不存在'; await this.update({ is_default: 0 }, { id: record.id }); await this.update({ is_default: 1 }, { id }); const records = await this.getAllDataByCondition({ pid }); return records.map(item => { const { sid } = item; item.path = listPath[sid].path; return item; }); } /** * 返回项目默认打开的url * @param {Number} pid 项目id * @return {String} path 路由名 */ async getDefaultPath(pid) { let record = await this.getAllDataByCondition({ pid }); if (record && !record.length) { console.log('显示列表为空,正在进行初始化数据.......'); // 记录为空,说明表没有进行初始化 listPath.forEach(async (list, idx) => { await this.db.insert(this.tableName, { pid, is_default: list.is_default, sid: idx, label_name: list.label_name }); }); // 获取当前项目下默认的记录 record = await this.getAllDataByCondition({ pid, is_default: 1 }); } // const record = await this.getDataByCondition({ is_default: 1, pid }); if (record && record.length) { const { sid } = record[0]; return listPath[sid].path; } } } return settingShow; };