Просмотр исходного кода

refactor: 更改设置-显示数据库存储逻辑

lanjianrong 5 лет назад
Родитель
Сommit
0797955ec6

+ 3 - 3
app/const/setting.js

@@ -18,9 +18,9 @@ cTypeStr[cType.dropDown] = '下拉菜单';
 
 // 显示设置-路由列表
 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' }, // 计量进度
+    { label_name: '计量进度', path: '/list/progress', is_default: false }, // 计量进度
+    { label_name: '标段列表', path: '/list', is_default: false }, // 标段列表
+    { label_name: '金额概况', path: '/list/info', is_default: false }, // 金额概况
 ];
 
 

+ 1 - 1
app/controller/setting_controller.js

@@ -604,7 +604,7 @@ module.exports = app => {
                 if (!projectData) {
                     throw '没有对应的项目数据';
                 }
-                const showList = await ctx.service.settingShow.getList(projectId);
+                const showList = await ctx.service.settingShow.getList(projectData.page_path);
                 const renderData = { projectData, showList };
                 await this.layout('setting/show.ejs', renderData);
             } catch (error) {

+ 1 - 0
app/middleware/session_auth.js

@@ -45,6 +45,7 @@ module.exports = options => {
             // 对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)) {

+ 3 - 3
app/public/js/setting.js

@@ -176,9 +176,9 @@ $(document).ready(() => {
         const data = {id: attid};
         postData('/setting/show/update', data, function (result) {
            let html = ''
-            result.forEach(item => {
+            result.forEach((item, idx) => {
                 html += `<li class="list-group-item">${item.label_name}`
-                html+= item.is_default ? `<span class="pull-right">默认</span></li>`:`<a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="${item.id}">设为默认</a></li>`
+                html+= item.is_default ? `<span class="pull-right">默认</span></li>`:`<a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="${idx}">设为默认</a></li>`
             })
             // <li class="list-group-item">
             // 标段列表<a href="#" class="btn btn-primary btn-sm pull-right">设为默认</a>
@@ -189,7 +189,7 @@ $(document).ready(() => {
             // </li>
             $('.list-group').empty()
             $('.list-group').append(html)
-            const item = result.find(i => i.id === attid)
+            const item = result.find((i, idx) => idx=== attid)
             $('#nav_tender').attr('href', item.path)
         });
     });

+ 17 - 32
app/service/setting_show.js

@@ -23,39 +23,35 @@ module.exports = app => {
          */
         constructor(ctx) {
             super(ctx);
-            this.tableName = 'setting_show';
+            this.tableName = 'project';
         }
 
         /**
          * 获取表的全部记录
-         * @param {Number} pid 项目id
+         * @param {Number | Null} i listPath对应下标
          * @return {Array} 查询结果集
          */
-        async getList(pid) {
-            const record = await this.getAllDataByCondition({ pid });
-            return record.map(item => {
-                // 拼接路由path
-                const { sid } = item;
-                item.path = listPath[sid].path;
+        async getList(i = 0) {
+            return listPath.map((item, idx) => {
+                if (i === idx) {
+                    return { ...item, is_default: true }
+                }
                 return item;
             });
         }
 
         /**
          * 设置默认显示字段
-         * @param {Number} id 标签id
+         * @param {Number} id 标签索引
          * @param {Number} pid 项目id
          * @return {Promise<void>} 查询结果集
          */
         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;
+            await this.update({ page_path: id }, { id: pid });
+            return listPath.map((item, idx) => {
+                if (id === idx) {
+                    return { ...item, is_default: true}
+                }
                 return item;
             });
         }
@@ -66,21 +62,10 @@ module.exports = app => {
          * @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;
-            }
+            const record = await this.getDataByCondition({ id: pid });
+            const { page_path = 0 } = record;
+            const list = listPath.find((item, idx) => idx === page_path);
+            return list.path;
         }
     }
     return settingShow;

+ 1 - 1
app/view/layout/menu.ejs

@@ -12,7 +12,7 @@
             <% for (const index in ctx.menuList) { %>
             <% if (ctx.menuList[index].display === undefined || !ctx.menuList[index].display) { continue } %>
             <li <% if(ctx.controllerName === index || (ctx.controllerName === 'list' && index === 'tender')) { %>class="active"<% } %>>
-                <a href="<%- (index === 'tender' && !ctx.app.config.is_debug ? ctx.curListUrl : ctx.menuList[index].url) %>" id="<%- 'nav_' + index%>" data-toggle="tooltip" data-placement="right" title="" data-original-title="<%- ctx.menuList[index].name %>">
+                <a href="<%- (index === 'tender' ? ctx.curListUrl : ctx.menuList[index].url) %>" id="<%- 'nav_' + index%>" data-toggle="tooltip" data-placement="right" title="" data-original-title="<%- ctx.menuList[index].name %>">
                     <i class="fa <%- ctx.menuList[index].icon %>"></i>
                     <% if (ctx.menuList[index].caption) { %>
                     <span><%- ctx.menuList[index].caption %></span>

+ 2 - 2
app/view/setting/show.ejs

@@ -14,11 +14,11 @@
                             <label>项目列表默认显示</label>
                             <div class="card w-50">
                                 <ul class="list-group list-group-flush">
-                                    <% showList.forEach(function(item) { %>
+                                    <% showList.forEach(function(item, idx) { %>
                                         <li class="list-group-item">
                                             <%= item.label_name %>
                                             <% if(!item.is_default) { %>
-                                                <a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="<%- item.id %>">设为默认</a>
+                                                <a href="javascript:void(0)" id="set-default" class="btn btn-primary btn-sm pull-right" data-attid="<%- idx %>">设为默认</a>
                                             <% } else {%>
                                                 <span class="pull-right">默认</span>
                                             <% } %>

+ 3 - 10
sql/update.sql

@@ -84,16 +84,9 @@ ADD COLUMN `pre_sf_tp`  decimal(24,8) NULL DEFAULT NULL COMMENT '截止上期实
 ALTER TABLE `zh_stage_detail`
 ADD COLUMN `calc_img_remark` TEXT NULL DEFAULT NULL COMMENT '草图备注' AFTER `custom_define`;
 
-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 '标签名',
-  PRIMARY KEY (`id`)
-) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-
-
 ALTER TABLE `zh_pos`
 ADD COLUMN `real_qty`  decimal(24,8) NULL DEFAULT 0 COMMENT '实际使用数量' AFTER `qtcl_expr`;
 
+ALTER TABLE `calculation`.`zh_project`
+ADD COLUMN `page_path` INT NOT NULL DEFAULT 0 AFTER `page_show`;
+