結組作業,第二次衝刺(6)

saccharin發表於2024-06-14

今天是第二次衝刺的第六天,昨天完成了廣告功能,今天我們準備增加聊天功能,能夠實現在軟體中直接點選對應的兼職資訊,就能和其使用者聊天,管理員能夠對聊天內容進行稽核。以下為今日部分程式碼:

package com.work.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.work.common.utils.BaseUtil;
import com.work.common.utils.JsonUtil;
import com.work.pojo.Chat;
import com.work.service.ChatService;

@Controller
@RequestMapping(value="/server/chat")
public class ChatController extends BaseUtil{

    @Autowired
    private ChatService chatService;
    
    //去聊天列表
    @RequestMapping("/goChatList")
    public ModelAndView goChatList(ModelAndView mv){
        mv.setViewName("server/chatManage/chatList");
        return mv;
    }
    
    //獲取聊天列表
    @RequestMapping("/getChatList")
    public void getChatList(HttpServletResponse response,Chat chat,Integer page,Integer limit){
        if(page == null){
            page = 1;
        }
        if(limit == null){
            limit = 10;
        }
        int totalCount = chatService.getChatListCount(chat);
        List<Chat> list = chatService.getChatList(chat,(page-1) * limit, limit);
        output(response,JsonUtil.buildJsonByTotalCount(list, totalCount));
    }
    
    //去編輯聊天
    @RequestMapping("/goUpdateChat")
    public ModelAndView goUpdateChat(ModelAndView mv,Integer id){
        Chat chat = chatService.getChatById(id);
        mv.addObject("chat", chat);
        mv.setViewName("server/chatManage/updateChat");
        return mv;
    }
    
    //編輯聊天
    @RequestMapping("/updateChat")
    public void updateChat(HttpServletResponse response,Chat chat){
        chatService.updateChat(chat);
        output(response,JsonUtil.buildFalseJson(0, "編輯成功!"));
    }

    //刪除聊天
    @RequestMapping("/deleteChat")
    public void deleteChat(HttpServletResponse response,Integer id){
        chatService.deleteChatByChatId(id);
        output(response,JsonUtil.buildFalseJson(0, "刪除成功!"));
    }

}

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>聊天列表</title>
<link rel="stylesheet" href="${ctx}/server/layui/css/layui.css"
    media="all">
</head>
<body>
    <form class="layui-form layui-form-pane" action="">
        <blockquote class="layui-elem-quote news_search">
            <div class="layui-form-item">
                <div class="layui-inline pane-select">
                    <label class="layui-form-label">聊天編號</label>
                    <div class="layui-input-block ">
                        <input type="text" id="chatId" name="chatId"
                            lay-verify="required|number" placeholder="請輸入聊天編號"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline pane-select">
                    <label class="layui-form-label">傳送方</label>
                    <div class="layui-input-block ">
                        <input type="text" id="userIdFa" name="userIdFa"
                            lay-verify="required|number" placeholder="請輸入傳送方編號"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline pane-select">
                    <label class="layui-form-label">接收方</label>
                    <div class="layui-input-block ">
                        <input type="text" id="userIdJie" name="userIdJie"
                            lay-verify="required|number" placeholder="請輸入接收方編號"
                            autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div style="float: right;">
                    <a class="layui-btn search_btn" onclick="doSearch()"> <i
                        class="layui-icon">&#xe615;</i>查詢
                    </a>
                </div>
            </div>
        </blockquote>
    </form>
    
    <table class="layui-table" id="forum" lay-filter="forum"
        lay-data="{id: 'forum'}"></table>
    <script type="text/html" id="toolBar">
        <a class="layui-btn layui-btn-mini" lay-event="updateChat" >編輯</a>
        <a class="layui-btn layui-btn-mini layui-btn-danger" lay-event="deleteChat">刪除</a>
    </script>
    <script type="text/html" id="isLookTpl">
       {{# var isLook=d.isLook}}
          {{# if (isLook==1){}}
            <span style="color:green">是</span>
       {{# } else if(isLook==0){ }}
               <span style="color:red">否</span>
        {{# }  }}
    </script>
    <script type="text/javascript" src="${ctx}/server/js/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="${ctx}/server/layui/layui.all.js"></script>
    <script>
        layui.use([ 'layer', 'table', 'laydate' ], function() {
            table = layui.table;
            layer = layui.layer;
            var laydate = layui.laydate;
            //--------------方法渲染TABLE----------------
            //日期範圍
            laydate.render({
                elem : '#startAndendTime',
                calendar : true,
                range : '~'
            });
            getlist(null, null, null);
        });
        function doSearch() {
            var chatId = $("#chatId").val();
            var userIdFa = $("#userIdFa").val();
            var userIdJie = $("#userIdJie").val();
            getlist(chatId, userIdFa, userIdJie);
        }

        function getlist(chatId, userIdFa, userIdJie) {
            tableIns = table.render({
                elem : '#forum',
                id : 'forum',
                method : 'post',
                where : {
                    'id' : chatId,
                    'userIdFa' : userIdFa,
                    'userIdJie' : userIdJie
                },
                url : '${ctx}/server/chat/getChatList.action',
                cols : [ [ {
                    field : 'id',
                    title : '編號',
                    width : 80,
                    align : 'center',
                    fixed : true,
                    sort : true
                }, {
                    field : 'userIdFa',
                    title : '傳送方',
                    width : 140,
                    align : 'center'
                }, {
                    field : 'userIdJie',
                    title : '接收方',
                    width : 140,
                    align : 'center'
                }, {
                    field : 'chatSignal',
                    title : '聊天標誌',
                    width : 140,
                    align : 'center'
                }, {
                    field : 'isLook',
                    title : '是否檢視',
                    width : 140,
                    align : 'center',
                    templet : "#isLookTpl"
                }, {
                    field : 'createTime',
                    title : '釋出時間',
                    width : 180,
                    align : 'center',
                    sort : true,
                }, {
                    title : '操作',
                    width : 240,
                    align : 'center',
                    fixed : 'right',
                    toolbar : '#toolBar'
                } ] ],
                page : true,
                limits : [ 10, 20, 30, 50, 100 ],
                limit : 10, //預設採用20
                width : '100%',
                height : 'auto',
                loading : true,
                even : true,
                response : {
                    statusName : 'code' //資料狀態的欄位名稱,預設:code
                    ,
                    statusCode : 0 //成功的狀態碼,預設:0
                    ,
                    countName : 'count' //資料總數的欄位名稱,預設:count
                    ,
                    dataName : 'data' //資料列表的欄位名稱,預設:data
                },
                done : function(res, curr, count) {
                }
            });
        }
        table.on('tool(forum)', function(obj) {
            var data = obj.data;
            if (obj.event === 'updateChat') {
                var index = layui.layer.open({
                    title : "編輯聊天",
                    type : 2,
                    content : "${ctx}/server/chat/goUpdateChat.action?id="
                            + data.id,
                    success : function(layero, index) {
                        setTimeout(function() {
                            layui.layer.tips('點選此處返回',
                                    '.layui-layer-setwin .layui-layer-close', {
                                        tips : 3
                                    });
                        }, 500)
                    }
                })
                layui.layer.full(index);
            }else if (obj.event === 'deleteChat') {
                layer.confirm('確定刪除該條聊天記錄嗎?', function(index){
                    //向服務端傳送刪除指令
                    $.ajax({
                        type : "post",
                        url : "${ctx}/server/chat/deleteChat.action",
                        dataType : "json",
                        data : {
                            "id" : data.id,
                        },
                        success : function(data) {
                            if(data.code==0){
                                layer.msg("刪除成功!");
                                setTimeout(function () {
                                       window.location.reload();
                                 }, 1000);
                            }else{
                                layer.msg("刪除失敗!");
                            }
                        }
                    });
                 });
            }
        });
        
    </script>
</body>
</html>

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta charset="utf-8">
<title>檢視聊天記錄</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">

<link rel="stylesheet" href="${ctx}/server/layui/css/layui.css"
    media="all" />
<link rel="stylesheet" href="${ctx}/server/font-awesome/css/font-awesome.min.css">
<style type="text/css">
#Images li{
    float:left;
    margin-left:10px;
    height:300px;
    margin-bottom:20px;
}
#Images img{
    height:300px;
}
.img_del{
    cursor: pointer;
}
</style>
</head>
<body>
    <div style="margin: 15px;">
        <div class="layui-form">
            
            <div class="layui-form-item">
                <label class="layui-form-label">內容:</label>
                <div class="layui-input-block">
                     <textarea placeholder="請輸入內容" class="layui-textarea" name="content">${chat.content}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">傳送方:</label>
                <div class="layui-input-block">
                    <input type="text" name="userIdFa" value="${chat.userIdFa}" autocomplete="off" class="layui-input" lay-verify="userIdFa" placeholder="請輸入傳送方編號" style="width:390px">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">接收方:</label>
                <div class="layui-input-block">
                    <input type="text" name="userIdJie" value="${chat.userIdJie}" autocomplete="off" class="layui-input" lay-verify="userIdJie" placeholder="請輸入接收方編號" style="width:390px">
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">聊天標誌:</label>
                <div class="layui-input-block">
                    <input type="text" name="title" value="${chat.chatSignal}"  autocomplete="off" class="layui-input" lay-verify="chatSignal" placeholder="請輸入聊天標誌" style="width:390px">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否檢視:</label>
                <div class="layui-input-inline" style="width:390px">
                    <select name="isLook" lay-verify="collType">
                        <option value="1" <c:if test="${chat.isLook == 1}">selected="selected"</c:if>>是</option>
                        <option value="0" <c:if test="${chat.isLook == 0}">selected="selected"</c:if>>否</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">傳送方刪除:</label>
                <div class="layui-input-inline" style="width:390px">
                    <select name="isRemoveFa" lay-verify="collType">
                        <option value="1" <c:if test="${chat.isRemoveFa == 1}">selected="selected"</c:if>>是</option>
                        <option value="0" <c:if test="${chat.isRemoveFa == 0}">selected="selected"</c:if>>否</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">接收方刪除:</label>
                <div class="layui-input-inline" style="width:390px">
                    <select name="isRemoveJie" lay-verify="collType">
                        <option value="1" <c:if test="${chat.isRemoveJie == 1}">selected="selected"</c:if>>是</option>
                        <option value="0" <c:if test="${chat.isRemoveJie == 0}">selected="selected"</c:if>>否</option>
                    </select>
                </div>
            </div>
            
            <div class="layui-form-item">
                <label class="layui-form-label">圖片:</label>
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" id="uploadNewImg">圖片上傳</button> 
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <ul class="layer-photos-demo" id="Images" lay-verify="uploadNewImg"></ul>
            </div>            
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">確認</button>
                    <button onclick="goBack();" class="layui-btn layui-btn-primary">返回</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="${ctx}/server/js/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="${ctx}/server/layui/layui.js"></script>

<script type="text/javascript">
var image = '${chat.image}';
if(image != ''){
    $('#Images').html('<li><img src="'+ image +'" class="layui-upload-img"><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>');
}

//圖片集合刪除
function delImages(){
    var i_s=$("#Images .img_del");
    for(var i=0;i<i_s.length;i++){
        $("#Images .img_del").get(i).index=i;
        $("#Images .img_del:eq("+i+")").click(function(){
            var i=this.index;
            $("#Images li:eq("+i+")").remove();
        })
    }
}
delImages();
layui.use('upload', function() {
    var $ = layui.jquery, upload = layui.upload;
    upload.render({
        url: '${ctx}/imageUpload/chatImage.action'
        ,elem: '#uploadNewImg' //指定原始元素,預設直接查詢class="layui-upload-file"
        ,method: 'post', //上傳介面的http型別
        dataType:"json"
        ,multiple: true
        ,before: function(obj){
          //預讀本地檔案示例,不支援ie8
          obj.preview(function(index, file, result){
          });
        }
        ,done: function(res){
            //$('#Images').append('<li><img layer-src="'+ res.msg +'" src="'+ res.msg +'" alt="'+ res.msg +'" class="layui-upload-img"><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>');
            $('#Images').html('<li><img src="'+ res.msg +'" class="layui-upload-img"><div class="operate"><i class="layui-icon img_del">&#xe640;</i></div></li>');
            image = res.msg;
            delImages();
        }
    });
});



layui.use([ 'form', 'layer'],
        function() {
         form = layui.form, layer = layui.layer;
         //自定義驗證規則
        form.verify({    
            
        });
         
        //監聽提交
        form.on('submit(demo1)', function(data) {
            $.ajax({
                type : "post",
                url : "${ctx}/server/chat/updateChat.action",
                dataType:"json",
                data : {
                    "id" : '${chat.id}',
                    "chatSignal":data.field.chatSignal,
                    "content":data.field.content,
                    "userIdFa":data.field.userIdFa,
                    "userIdJie":data.field.userIdJie,
                    "isLook":data.field.isLook,
                    "isRemoveFa":data.field.isRemoveFa,
                    "isRemoveJie":data.field.isRemoveJie,
                    "image":image
                },
                success : function(result) {
                    if(result.code==0){
                        layer.msg("編輯成功");
                        setTimeout(function () {
                             layer.closeAll("iframe");
                              //重新整理父頁面
                               parent.location.reload();
                          }, 1000);
                    } else{
                        layer.msg(result.msg,{time:2000});
                    }
                },error:function(){
                    layer.msg("編輯失敗");
                }
            });
            return false;
        });
    });

function goBack(){
    window.history.go(-1);
}
</script>
</html>

相關文章