setting_show.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. /**
  3. * Created by LanJianRong on 2020/7/6.
  4. * 项目设置->显示设置表数据模型
  5. * @author LanJianRong
  6. * @date 2020/07/06
  7. * @version
  8. */
  9. const { listPath } = require('../const/setting');
  10. const BaseService = require('../base/base_service');
  11. module.exports = app => {
  12. class settingShow extends BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'setting_show';
  22. }
  23. /**
  24. * 获取表的全部记录
  25. * @param {Number} pid 项目id
  26. * @return {Array} 查询结果集
  27. */
  28. async getList(pid) {
  29. const record = await this.getAllDataByCondition({ pid });
  30. return record.map(item => {
  31. // 拼接路由path
  32. const { sid } = item;
  33. item.path = listPath[sid].path;
  34. return item;
  35. });
  36. }
  37. /**
  38. * 设置默认显示字段
  39. * @param {Number} id 标签id
  40. * @param {Number} pid 项目id
  41. * @return {Promise<void>} 查询结果集
  42. */
  43. async setDefaultLabel(id, pid) {
  44. const record = await this.getDataByCondition({ pid, is_default: 1 });
  45. if (!record) throw '该标签不存在';
  46. await this.update({ is_default: 0 }, { id: record.id });
  47. await this.update({ is_default: 1 }, { id });
  48. const records = await this.getAllDataByCondition({ pid });
  49. return records.map(item => {
  50. const { sid } = item;
  51. item.path = listPath[sid].path;
  52. return item;
  53. });
  54. }
  55. /**
  56. * 返回项目默认打开的url
  57. * @param {Number} pid 项目id
  58. * @return {String} path 路由名
  59. */
  60. async getDefaultPath(pid) {
  61. let record = await this.getAllDataByCondition({ pid });
  62. if (record && !record.length) {
  63. console.log('显示列表为空,正在进行初始化数据.......');
  64. // 记录为空,说明表没有进行初始化
  65. listPath.forEach(async (list, idx) => {
  66. await this.db.insert(this.tableName, { pid, is_default: list.is_default, sid: idx, label_name: list.label_name });
  67. });
  68. // 获取当前项目下默认的记录
  69. record = await this.getAllDataByCondition({ pid, is_default: 1 });
  70. }
  71. // const record = await this.getDataByCondition({ is_default: 1, pid });
  72. if (record && record.length) {
  73. const { sid } = record[0];
  74. return listPath[sid].path;
  75. }
  76. }
  77. }
  78. return settingShow;
  79. };