From 6c0cef9c04ef998f5f66a139358502d3f1b3c205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=B6=E5=A3=AB=E6=B6=B5?= <630892807@qq.com> Date: Sat, 5 Sep 2020 18:27:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/notice.go | 50 +++++++++++++++++++++----------- main.go | 2 ++ models/welcomes.go | 15 ++++++++++ static/html/chat_page.html | 21 ++++++-------- static/html/setting_bottom.html | 26 +++++++++++++++++ static/html/setting_welcome.html | 1 - static/js/functions.js | 6 +++- 7 files changed, 90 insertions(+), 31 deletions(-) diff --git a/controller/notice.go b/controller/notice.go index 9efba91..0ad8044 100644 --- a/controller/notice.go +++ b/controller/notice.go @@ -2,9 +2,9 @@ package controller import ( "encoding/json" + "fmt" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" - "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/models" "github.com/taoshihan1991/imaptool/tools" "log" @@ -13,27 +13,23 @@ import ( ) func GetNotice(c *gin.Context) { kefuId:=c.Query("kefu_id") - lang,_:=c.Get("lang") - language:=config.CreateLanguage(lang.(string)) - welcome:=models.FindWelcomeByUserId(kefuId) + welcomes:=models.FindWelcomesByUserId(kefuId) user:=models.FindUser(kefuId) - var content string - log.Println(welcome) - if welcome.Content!=""{ - content=welcome.Content - }else { - content=language.Notice + result:=make([]gin.H,0) + for _,welcome:=range welcomes{ + h:=gin.H{ + "name":user.Nickname, + "avator":user.Avator, + "is_kefu":false, + "content":welcome.Content, + "time":time.Now().Format("2006-01-02 15:04:05"), + } + result=append(result,h) } c.JSON(200, gin.H{ "code": 200, "msg": "ok", - "result":gin.H{ - "nickname":user.Nickname, - "avator":user.Avator, - "name":user.Name, - "content":content, - "time":time.Now().Format("2006-01-02 15:04:05"), - }, + "result":result, }) } func GetNotices(c *gin.Context) { @@ -45,6 +41,26 @@ func GetNotices(c *gin.Context) { "result":welcomes, }) } +func PostNotice(c *gin.Context) { + kefuId,_:=c.Get("kefu_name") + content:=c.PostForm("content") + models.CreateWelcome(fmt.Sprintf("%s",kefuId),content) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":"", + }) +} +func DelNotice(c *gin.Context) { + kefuId,_:=c.Get("kefu_name") + id:=c.Query("id") + models.DeleteWelcome(kefuId,id) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":"", + }) +} var upgrader = websocket.Upgrader{} var oldFolders map[string]int diff --git a/main.go b/main.go index 92ca09a..710570e 100644 --- a/main.go +++ b/main.go @@ -118,6 +118,8 @@ func main() { engine.GET("/statistics",middleware.JwtApiMiddleware, controller.GetStatistics) //前台接口 engine.GET("/notice",middleware.SetLanguage, controller.GetNotice) + engine.POST("/notice",middleware.JwtApiMiddleware, controller.PostNotice) + engine.DELETE("/notice",middleware.JwtApiMiddleware, controller.DelNotice) engine.GET("/notices",middleware.JwtApiMiddleware, controller.GetNotices) //前台引入js接口 engine.GET("/webjs", tmpl.PageWebJs) diff --git a/models/welcomes.go b/models/welcomes.go index cef4fd6..8a26b6f 100644 --- a/models/welcomes.go +++ b/models/welcomes.go @@ -9,6 +9,18 @@ type Welcome struct { IsDefault uint `json:"is_default"` Ctime time.Time `json:"ctime"` } +func CreateWelcome(userId string,content string)uint{ + if userId==""||content==""{ + return 0 + } + w:=&Welcome{ + UserId: userId, + Content: content, + Ctime: time.Now(), + } + DB.Create(w) + return w.ID +} func FindWelcomeByUserId(userId interface{})Welcome{ var w Welcome DB.Where("user_id = ? and is_default=?", userId,1).First(&w) @@ -18,4 +30,7 @@ func FindWelcomesByUserId(userId interface{})[]Welcome{ var w []Welcome DB.Where("user_id = ?", userId).Find(&w) return w +} +func DeleteWelcome(userId interface{},id string){ + DB.Where("user_id = ? and id = ?", userId,id).Delete(Welcome{}) } \ No newline at end of file diff --git a/static/html/chat_page.html b/static/html/chat_page.html index fbb5550..3d0a3ff 100644 --- a/static/html/chat_page.html +++ b/static/html/chat_page.html @@ -366,20 +366,17 @@ getNotice : function (){ let _this=this; $.get("/notice?kefu_id="+guest.to_id+"&lang={{.Lang}}",function(res) { - console.log(res); + //debugger; if (res.result != null) { let msg = res.result; - let content = {} - content.avator = msg.avator; - content.name = msg.nickname; - content.content = replaceContent(msg.content); - content.is_kefu = false; - content.time = msg.time; - setTimeout(function () { - _this.msgList.push(content); - _this.scrollBottom(); - }, 4000); - ; + for(let i=0;i { + if (valid) { + this.sendAjax("/notice","POST",_this.welcomeForm,function(result){ + _this.welcomeDialog=false; + _this.sendAjax("/notices","get",{},function(result){ + _this.noticeList=result; + }); + }); + } else { + return false; + } + }); + }, //编辑客服表单 editKefuForm(formName){ let _this=this; @@ -278,6 +294,16 @@ }); }); }, + //删除欢迎 + deleteWelcome(id){ + let _this=this; + this.sendAjax("/notice?id="+id,"DELETE",{id:id},function(result){ + _this.kefuDialog=false; + _this.sendAjax("/notices","get",{},function(result){ + _this.noticeList=result; + }); + }); + }, //配置角色权限 showAuthDialog(id,name,method,path){ this.roleForm.id=id diff --git a/static/html/setting_welcome.html b/static/html/setting_welcome.html index 8eb0d79..0187b02 100644 --- a/static/html/setting_welcome.html +++ b/static/html/setting_welcome.html @@ -22,7 +22,6 @@ prop="id" label="操作"> diff --git a/static/js/functions.js b/static/js/functions.js index 88d9d10..4b8ae50 100644 --- a/static/js/functions.js +++ b/static/js/functions.js @@ -93,4 +93,8 @@ function filter (obj){ } } return false; -} \ No newline at end of file +} +function sleep(time) { + var startTime = new Date().getTime() + parseInt(time, 10); + while(new Date().getTime() < startTime) {} +}; \ No newline at end of file