Selaa lähdekoodia

refactor: 显示设置代码逻辑重构

lanjianrong 5 vuotta sitten
vanhempi
commit
9753afbf6b

+ 10 - 1
app/const/setting.js

@@ -16,9 +16,18 @@ const cTypeStr = [];
 cTypeStr[cType.dropDown] = '下拉菜单';
 // cTypeStr[cType.radio] = '单选框';
 
+// 显示设置-路由列表
+const listPath = [
+    { is_default: 1, label_name: '标段列表', path: '/list' }, // 标段列表
+    { is_default: 0, label_name: '金额概况', path: '/list/info' }, // 金额概况
+    { is_default: 0, label_name: '计量进度', path: '/list/progress' }, // 计量进度
+];
+
+
 module.exports = {
     cType: {
         key: cType,
         text: cTypeStr,
     },
-}
+    listPath,
+};

+ 4 - 2
app/controller/setting_controller.js

@@ -604,7 +604,7 @@ module.exports = app => {
                 if (!projectData) {
                     throw '没有对应的项目数据';
                 }
-                const showList = await ctx.service.settingShow.getList();
+                const showList = await ctx.service.settingShow.getList(projectId);
                 const renderData = { projectData, showList };
                 await this.layout('setting/show.ejs', renderData);
             } catch (error) {
@@ -620,9 +620,11 @@ module.exports = app => {
          */
         async showListUpdate(ctx) {
             try {
+                // 获取项目id
+                const projectId = ctx.session.sessionProject.id;
                 const { data } = ctx.request.body;
                 const { id } = JSON.parse(data);
-                const result = await ctx.service.settingShow.setDefaultLabel(id);
+                const result = await ctx.service.settingShow.setDefaultLabel(id, projectId);
                 const responseData = { err: 0, msg: '', data: result };
                 ctx.body = responseData;
             } catch (error) {

+ 3 - 2
app/middleware/session_auth.js

@@ -42,7 +42,9 @@ module.exports = options => {
                 throw '系统维护中~';
             }
 
-
+            // 对sub_menu项目默认打开页进行配置
+            const path = yield this.service.settingShow.getDefaultPath(this.session.sessionProject.id);
+            path && (this.curListUrl = path);
         } catch (error) {
             console.log(error);
             if (this.helper.isAjax(this.request)) {
@@ -57,7 +59,6 @@ module.exports = options => {
                     return this.redirect('/wap/login?referer=' + this.url);
                 }
                 return this.redirect('/login?referer=' + this.url);
-
             }
             if (this.helper.isWap(this.request)) {
                 this.session.wapTenderID = this.params.id ? this.params.id : null;

+ 39 - 10
app/service/setting_show.js

@@ -9,6 +9,7 @@
  * @version
  */
 
+const { listPath } = require('../const/setting');
 const BaseService = require('../base/base_service');
 module.exports = app => {
 
@@ -26,32 +27,60 @@ module.exports = app => {
         }
 
         /**
-         * 获取设置表所有记录
-         * @return {Promise<void>} 查询结果集
+         * 获取表的全部记录
+         * @param {Number} pid 项目id
+         * @return {Array} 查询结果集
          */
-        async getList() {
-            return await this.db.select(this.tableName);
+        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<void>} 查询结果集
          */
-        async setDefaultLabel(id) {
-            const record = await this.getDataByCondition({ is_default: 1 });
+        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 });
-            return await this.db.select(this.tableName);
+            const records = await this.getAllDataByCondition({ pid });
+            return records.map(item => {
+                const { sid } = item;
+                item.path = listPath[sid].path;
+                return item;
+            });
         }
 
         /**
          * 返回项目默认打开的url
-         * @return {String} path 默认url
+         * @param {Number} pid 项目id
+         * @return {String} path 路由名
          */
-        async getDefaultPath() {
-            return await this.getDataByCondition({ is_default: 1 });
+        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;

+ 4 - 6
sql/update.sql

@@ -86,17 +86,15 @@ ADD COLUMN `calc_img_remark` TEXT NULL DEFAULT NULL COMMENT '草图备注' AFTER
 
 CREATE TABLE `zh_setting_show` (
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
+  `pid` int(11) NOT NULL COMMENT '项目id',
+  `is_default` int(11) NOT NULL DEFAULT '0' COMMENT '是否默认显示',
+  `sid` int(11) NOT NULL COMMENT '对应consts下listPath的下标',
   `label_name` varchar(45) COLLATE utf8_unicode_ci NOT NULL COMMENT '标签名',
-  `is_default` int(11) NOT NULL DEFAULT '0' COMMENT '设置默认值,1为设置',
-  `path` varchar(45) COLLATE utf8_unicode_ci NOT NULL COMMENT '对应标签跳转路径',
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 
-INSERT INTO `calculation`.`zh_setting_show`
-(`label_name`,`is_default`,`path`)
-VALUES
-('标段列表',0,'/list'),('金额概况',0,'/list/info'),('计量进度',1,'/list/progress');
 
 ALTER TABLE `zh_pos`
 ADD COLUMN `real_qty`  decimal(24,8) NULL DEFAULT 0 COMMENT '实际使用数量' AFTER `qtcl_expr`;
 
+) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;