Browse Source

项目通知优化

ellisran 1 year ago
parent
commit
042ab5b725

+ 47 - 28
app/controller/dashboard_controller.js

@@ -130,38 +130,57 @@ module.exports = app => {
          * @param {Object} ctx - egg全局变量
          * @return {void}
          */
-        async msgList(ctx) {
-            const page = ctx.page;
+        async msg(ctx) {
+            try {
+                const page = ctx.page;
 
-            const type = ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
+                const msgId = parseInt(ctx.params.id) || 0;
 
-            const total = type === 1 ?
-                await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, type }) :
-                await ctx.service.message.count({ status: 1, type });
+                let msgInfo = msgId ? await ctx.service.message.getDataById(msgId) : null;
 
-            const limit = 5;
-            const offset = limit * (this.ctx.page - 1);
+                const type = msgInfo ? msgInfo.type : ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
 
-            const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, limit, offset, type);
+                if (msgInfo && msgInfo.project_id !== ctx.session.sessionProject.id) {
+                    throw '非该项目通知无权查看';
+                }
 
-            const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
-            const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
-            const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
+                const total = type === 1 ?
+                    await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, type }) :
+                    await ctx.service.message.count({ status: 1, type });
 
-            // 分页相关
-            const pageInfo = {
-                page,
-                total: Math.ceil(total / limit),
-                queryData: JSON.stringify(ctx.urlInfo.query),
-            };
-            const renderData = {
-                uid: ctx.session.sessionUser.accountId,
-                type,
-                pageInfo,
-                userMsgPermission,
-                msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
-            };
-            await this.layout('dashboard/msg.ejs', renderData);
+                const limit = 5;
+                const offset = limit * (this.ctx.page - 1);
+
+                const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, limit, offset, type);
+
+                const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
+                const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
+                const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
+
+                if (!msgId) {
+                    msgInfo = msgList[0];
+                }
+                // 分页相关
+                const pageInfo = {
+                    page,
+                    total: Math.ceil(total / limit),
+                    queryData: JSON.stringify(ctx.urlInfo.query),
+                };
+                const renderData = {
+                    msgInfo,
+                    uid: ctx.session.sessionUser.accountId,
+                    type,
+                    pageInfo,
+                    userMsgPermission,
+                    msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
+                };
+                await this.layout('dashboard/msg.ejs', renderData);
+            } catch (error) {
+                console.log(error);
+                this.log(error);
+                ctx.session.postError = error.toString();
+                ctx.redirect('/dashboard');
+            }
         }
 
         /**
@@ -237,7 +256,7 @@ module.exports = app => {
                             }
                         }
                     }
-                    ctx.redirect('/dashboard/msg/list');
+                    ctx.redirect('/dashboard/msg');
                 }
             } catch (error) {
                 ctx.redirect(ctx.request.header.referer);
@@ -263,7 +282,7 @@ module.exports = app => {
                 }
                 const result = await ctx.service.message.deleteById(msgInfo.id);
                 if (result) {
-                    ctx.redirect('/dashboard/msg/list');
+                    ctx.redirect('/dashboard/msg');
                 }
             } catch (error) {
                 ctx.redirect(ctx.request.header.referer);

+ 3 - 0
app/controller/wap_controller.js

@@ -805,6 +805,9 @@ module.exports = app => {
                 if (!msgInfo) {
                     throw '项目通知不存在';
                 }
+                if (msgInfo && msgInfo.project_id !== ctx.session.sessionProject.id) {
+                    throw '非该项目通知无权查看';
+                }
                 const renderData = {
                     msgInfo,
                     moment,

+ 3 - 0
app/public/css/main.css

@@ -2156,3 +2156,6 @@ animation:shake 1s .2s ease both;}
     display: flex;
     margin: auto;
 }
+.list-waring{
+    background-color: #ffeeba!important;
+}

+ 2 - 0
app/public/js/dashboard.js

@@ -26,6 +26,8 @@ $(document).ready(() => {
     $('.list-group-item a').on('click', function () {
         const id = $(this).attr('msg-id');
         const msgInfo = _.find(msgList, {id: parseInt(id)});
+        $('.msg-height-list li').removeClass('list-waring');
+        $(this).parents('.list-group-item').addClass('list-waring');
         $('#title').html(msgInfo.title);
         $('#release_time').html(moment(msgInfo.release_time * 1000).format('YYYY-MM-DD'));
         $('#content').html(msgInfo.content);

+ 2 - 1
app/router.js

@@ -76,7 +76,8 @@ module.exports = app => {
 
     // 控制面板相关
     app.get('/dashboard', sessionAuth, 'dashboardController.index');
-    app.get('/dashboard/msg/list', sessionAuth, 'dashboardController.msgList');
+    app.get('/dashboard/msg', sessionAuth, 'dashboardController.msg');
+    app.get('/dashboard/msg/:id', sessionAuth, 'dashboardController.msg');
     app.get('/dashboard/msg/add/:id', sessionAuth, 'dashboardController.msgAdd');
     app.post('/dashboard/msg/set/:id', sessionAuth, datetimeFill, 'dashboardController.msgSet');
     app.get('/dashboard/msg/del/:id', sessionAuth, 'dashboardController.msgDelete');

+ 1 - 1
app/view/dashboard/index.ejs

@@ -308,7 +308,7 @@
                     <div class="card ml-3">
                         <div class="card-header card-white d-flex justify-content-between">
                             <div class="card-big-htext"><span class="card-icon mr-2"></span>项目通知</div>
-                            <div class="mt-1"><a class="text-secondary" href="/dashboard/msg/list">查看全部</a></div>
+                            <div class="mt-1"><a class="text-secondary" href="/dashboard/msg">查看全部</a></div>
                         </div>
                         <div class="card-body p-0">
                             <div class="contant-height-three">

+ 9 - 9
app/view/dashboard/msg.ejs

@@ -10,13 +10,13 @@
                         <div class="card">
                             <div class="card-header">
                                 <div class="btn-group btn-group-sm" role="group" aria-label="Basic example">
-                                    <a href="/dashboard/msg/list" <% if (type === 1) { %>class="btn btn-primary"<% } else { %>class="btn btn-outline-primary"<% } %>>项目通知</a>
-                                    <a href="/dashboard/msg/list?type=2" <% if (type === 2) { %>class="btn btn-primary"<% } else { %>class="btn btn-outline-primary"<% } %>>系统消息</a>
+                                    <a href="/dashboard/msg" <% if (type === 1 || (msgInfo && msgInfo.type === 1)) { %>class="btn btn-primary"<% } else { %>class="btn btn-outline-primary"<% } %>>项目通知</a>
+                                    <a href="/dashboard/msg?type=2" <% if (type === 2 || (msgInfo && msgInfo.type === 2)) { %>class="btn btn-primary"<% } else { %>class="btn btn-outline-primary"<% } %>>系统消息</a>
                                 </div>
                             </div>
                             <ul class="list-group list-group-flush msg-height-list">
                                 <% for (const msg of msgList) { %>
-                                <li class="list-group-item"><a href="javascript:void(0);" msg-id="<%= msg.id %>" <% if (msg.istop !== '0') { %>class="text-danger"><i class="fa fa-exclamation-triangle" ></i><% } else { %>><% } %> <%= msg.title %></a><br><%= moment(msg.release_time*1000).format('YYYY-MM-DD') %></li>
+                                <li class="list-group-item <% if (msgInfo && msg.id === msgInfo.id) { %>list-waring<% } %>"><a href="javascript:void(0);" msg-id="<%= msg.id %>" <% if (msg.istop !== '0') { %>class="text-danger"><i class="fa fa-exclamation-triangle" ></i><% } else { %>><% } %> <%= msg.title %></a><br><%= moment(msg.release_time*1000).format('YYYY-MM-DD') %></li>
                                 <% } %>
                                 <li class="list-group-item pt-2">
                                     <% include ../layout/page.ejs %>
@@ -27,20 +27,20 @@
                     <div class="card col-9">
                         <div class="card-body">
                             <div>
-                                <% if (msgList && msgList[0]) { %>
-                                <h4 class="text-center" id="title"><%= msgList[0].title %></h4>
+                                <% if (msgInfo) { %>
+                                <h4 class="text-center" id="title"><%= msgInfo.title %></h4>
                                 <p class="text-center text-muted">
-                                    <span id="creator"><%- (type === 1 ? msgList[0].creator : '') %></span> 发布于 <span id="release_time"><%= moment(msgList[0].release_time*1000).format('YYYY-MM-DD') %></span>
+                                    <span id="creator"><%- (type === 1 ? msgInfo.creator : '') %></span> 发布于 <span id="release_time"><%= moment(msgInfo.release_time*1000).format('YYYY-MM-DD') %></span>
                                     <span id="user_permission">
-                                        <% if (uid === msgList[0].create_uid) { %>
+                                        <% if (uid === msgInfo.create_uid) { %>
                                             <!--有编辑权用户-->
-                                            <a href="/dashboard/msg/add/<%- msgList[0].id %>" class="btn btn-sm btn-outline-primary">编辑</a>
+                                            <a href="/dashboard/msg/add/<%- msgInfo.id %>" class="btn btn-sm btn-outline-primary">编辑</a>
                                         <% } %>
                                     </span>
                                 </p>
                                 <!--内容开始-->
                                 <div class="msg-content border-top-1 pt-3" id="content">
-                                    <%- msgList[0].content %>
+                                    <%- msgInfo.content %>
                                 </div>
                                 <% } %>
                             </div>