From 5e84e65fddaea2d08cad9e39fa3d706d2afed948 Mon Sep 17 00:00:00 2001 From: taoshihan <630892807@qq.com> Date: Thu, 10 Nov 2022 12:50:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/install.go | 7 +-- config/go-fly.sql | 1 + controller/message.go | 2 - controller/notice.go | 76 ++++++------------------------ go.mod | 2 +- go.sum | 6 --- router/api.go | 4 -- static/css/common.css | 6 +++ static/js/chat-page.js | 47 +++++++----------- static/templates/chat_page.html | 5 +- static/templates/setting_left.html | 1 - tools/import_sql.go | 56 ++++++++++++++++++++++ tools/import_sql_test.go | 15 ++++++ ws/visitor.go | 8 ++-- 14 files changed, 121 insertions(+), 115 deletions(-) create mode 100644 tools/import_sql.go create mode 100644 tools/import_sql_test.go diff --git a/cmd/install.go b/cmd/install.go index 7fea759..e85366a 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -24,7 +24,7 @@ func install() { log.Println("请先删除./install.lock") os.Exit(1) } - sqlFile := common.Dir + "go-fly.sql" + sqlFile := "import.sql" isExit, _ := tools.IsFileExist(common.MysqlConf) dataExit, _ := tools.IsFileExist(sqlFile) if !isExit || !dataExit { @@ -32,8 +32,9 @@ func install() { os.Exit(1) } sqls, _ := ioutil.ReadFile(sqlFile) - sqlArr := strings.Split(string(sqls), "|") + sqlArr := strings.Split(string(sqls), ";") for _, sql := range sqlArr { + sql = strings.TrimSpace(sql) if sql == "" { continue } @@ -41,7 +42,7 @@ func install() { if err == nil { log.Println(sql, "\t success!") } else { - log.Println(sql, err, "\t failed!") + log.Println(sql, err, "\t failed!", "数据库导入失败") os.Exit(1) } } diff --git a/config/go-fly.sql b/config/go-fly.sql index 1fd9f19..a036e62 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -113,6 +113,7 @@ CREATE TABLE `config` ( INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(SMTP地址)', 'NoticeEmailSmtp', '')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(邮箱)', 'NoticeEmailAddress', '')| INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(密码)', 'NoticeEmailPassword', '')| +INSERT INTO `config` (`id`, `conf_name`, `conf_key`, `conf_value`) VALUES (NULL, '发送通知邮件(密码)', 'NoticeEmailPassword', '')| DROP TABLE IF EXISTS `about`| CREATE TABLE `about` ( `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, diff --git a/controller/message.go b/controller/message.go index f1319db..40e6622 100644 --- a/controller/message.go +++ b/controller/message.go @@ -110,8 +110,6 @@ func SendMessageV2(c *gin.Context) { str, _ := json.Marshal(msg) ws.OneKefuMessage(kefuInfo.Name, str) //ws.KefuMessage(vistorInfo.VisitorId, content, kefuInfo) - go ws.SendServerJiang(vistorInfo.Name+"说", content, c.Request.Host) - go SendAppGetuiPush(kefuInfo.Name, vistorInfo.Name, content) kefu, ok := ws.KefuList[kefuInfo.Name] if !ok || kefu == nil{ go SendNoticeEmail(content+"|"+vistorInfo.Name, content) diff --git a/controller/notice.go b/controller/notice.go index 6248609..f9d151c 100644 --- a/controller/notice.go +++ b/controller/notice.go @@ -1,80 +1,32 @@ package controller import ( - "fmt" "github.com/gin-gonic/gin" "github.com/taoshihan1991/imaptool/models" - "github.com/taoshihan1991/imaptool/ws" - "time" ) func GetNotice(c *gin.Context) { kefuId := c.Query("kefu_id") - welcomes := models.FindWelcomesByKeyword(kefuId, "welcome") user := models.FindUser(kefuId) - 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) - } - status := "online" - if _, ok := ws.KefuList[kefuId]; !ok { - status = "offline" + if user.ID==0{ + c.JSON(200, gin.H{ + "code": 400, + "msg": "user not found", + }) + return } + welcomeMessage:=models.FindConfig("WelcomeMessage") + offlineMessage:=models.FindConfig("OfflineMessage") + allNotice:=models.FindConfig("AllNotice") c.JSON(200, gin.H{ "code": 200, "msg": "ok", "result": gin.H{ - "welcome": result, - "username": user.Nickname, - "avatar": user.Avator, - "status": status, + "welcome":welcomeMessage, + "offline":offlineMessage, + "avatar":user.Avator, + "nickname":user.Nickname, + "allNotice":allNotice, }, }) } -func GetNotices(c *gin.Context) { - kefuId, _ := c.Get("kefu_name") - welcomes := models.FindWelcomesByUserId(kefuId) - c.JSON(200, gin.H{ - "code": 200, - "msg": "ok", - "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 PostNoticeSave(c *gin.Context) { - kefuId, _ := c.Get("kefu_name") - content := c.PostForm("content") - id := c.PostForm("id") - models.UpdateWelcome(fmt.Sprintf("%s", kefuId), id, 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": "", - }) -} diff --git a/go.mod b/go.mod index 058f204..c8c2232 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/gin-contrib/pprof v1.3.0 github.com/gin-contrib/sessions v0.0.3 github.com/gin-gonic/gin v1.7.7 + github.com/go-sql-driver/mysql v1.5.0 github.com/gobuffalo/packr/v2 v2.5.1 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/gorilla/websocket v1.4.2 @@ -19,6 +20,5 @@ require ( github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v0.0.5 - github.com/tidwall/gjson v1.14.1 github.com/zh-five/xdaemon v0.1.1 ) diff --git a/go.sum b/go.sum index 0889b24..1ff9396 100644 --- a/go.sum +++ b/go.sum @@ -146,12 +146,6 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo= -github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= -github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= -github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= -github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= diff --git a/router/api.go b/router/api.go index fdfcd82..4463dd0 100644 --- a/router/api.go +++ b/router/api.go @@ -69,10 +69,6 @@ func InitApiRouter(engine *gin.Engine) { engine.POST("/about", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.PostAbout) engine.GET("/aboutpages", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.GetAbouts) engine.GET("/notice",controller.GetNotice) - engine.POST("/notice", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.PostNotice) - engine.DELETE("/notice", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.DelNotice) - engine.POST("/notice_save", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.PostNoticeSave) - engine.GET("/notices", middleware.JwtApiMiddleware, controller.GetNotices) engine.POST("/ipblack", middleware.JwtApiMiddleware, middleware.Ipblack, controller.PostIpblack) engine.DELETE("/ipblack", middleware.JwtApiMiddleware, middleware.RbacAuth, controller.DelIpblack) engine.GET("/ipblacks_all", middleware.JwtApiMiddleware, controller.GetIpblacks) diff --git a/static/css/common.css b/static/css/common.css index 01c35bd..9b95bea 100644 --- a/static/css/common.css +++ b/static/css/common.css @@ -558,4 +558,10 @@ a{color: #07a9fe;text-decoration: none;} } .chatBoxMe .chatRowAvator { margin-left: 10px; +} +.allNotice{ + font-size: 12px; + margin: 10px 0px; + line-height: 23px; + color: #666; } \ No newline at end of file diff --git a/static/js/chat-page.js b/static/js/chat-page.js index 51088a8..1982475 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -25,6 +25,7 @@ new Vue({ showIconBtns:false, showFaceIcon:false, isIframe:false, + kefuInfo:{}, }, methods: { //初始化websocket @@ -226,6 +227,8 @@ new Vue({ _this.setCache("visitor",res.result); //_this.getMesssagesByVisitorId(); _this.initConn(); + //获取欢迎 + _this.getNotice(); }); // }else{ // this.visitor=obj; @@ -337,35 +340,20 @@ new Vue({ getNotice : function (){ let _this=this; $.get("/notice?kefu_id="+KEFU_ID,function(res) { - //debugger; - _this.noticeName=res.result.username; - _this.noticeAvatar=res.result.avatar; - if (res.result.welcome != null) { - let msg = res.result.welcome; - var len=msg.length; - var i=0; - if(len>0){ - _this.timer=setInterval(function(){ - if(i>=len||typeof msg[i]=="undefined"||msg[i]==null){ - clearInterval(_this.timer); - return; - } - let content = msg[i]; - if(typeof content.content =="undefined"){ - return; - } - content.content = replaceContent(content.content); - _this.msgList.push(content); - _this.scrollBottom(); - if(i==0){ - _this.alertSound(); - } - - i++; - },4000); - } - + var code=res.code; + if(code!=200) return; + _this.kefuInfo=res.result; + var welcome=res.result.welcome; + if(!welcome) return; + var msg={ + content:replaceContent(welcome), + avator:res.result.avatar, + name :res.result.nickname, + time:new Date(), } + _this.msgList.push(msg); + _this.scrollBottom(); + _this.alertSound(); }); }, initCss:function(){ @@ -638,8 +626,7 @@ new Vue({ //this.msgList=this.getHistory(); //滚动底部 //this.scrollBottom(); - //获取欢迎 - this.getNotice(); + this.getAutoReply(); } }) diff --git a/static/templates/chat_page.html b/static/templates/chat_page.html index 3f8f649..d5e453e 100644 --- a/static/templates/chat_page.html +++ b/static/templates/chat_page.html @@ -23,10 +23,10 @@
- +
-
智能在线客服系统
+
<{kefuInfo.nickname}>
@@ -71,6 +71,7 @@

公告通知

+
<{kefuInfo.allNotice}>
diff --git a/static/templates/setting_left.html b/static/templates/setting_left.html index db772cb..8044ee1 100644 --- a/static/templates/setting_left.html +++ b/static/templates/setting_left.html @@ -10,7 +10,6 @@ 统计信息 - 自动欢迎 修改密码 修改头像 diff --git a/tools/import_sql.go b/tools/import_sql.go new file mode 100644 index 0000000..b5f3fcd --- /dev/null +++ b/tools/import_sql.go @@ -0,0 +1,56 @@ +package tools + +import ( + "fmt" + _ "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" + "io/ioutil" + "log" + "os" + "strings" + "time" +) + +type ImportSqlTool struct { + SqlPath string + Username, Password, Server, Port, Database string +} + +func (this *ImportSqlTool) ImportSql() error { + _, err := os.Stat(this.SqlPath) + if os.IsNotExist(err) { + log.Println("数据库SQL文件不存在:", err) + return err + } + + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", this.Username, this.Password, this.Server, this.Port, this.Database) + db, err := gorm.Open("mysql", dsn) + if err != nil { + log.Println("数据库连接失败:", err) + //panic("数据库连接失败!") + return err + } + db.SingularTable(true) + db.LogMode(true) + db.DB().SetMaxIdleConns(10) + db.DB().SetMaxOpenConns(100) + db.DB().SetConnMaxLifetime(59 * time.Second) + + sqls, _ := ioutil.ReadFile(this.SqlPath) + sqlArr := strings.Split(string(sqls), ";") + for _, sql := range sqlArr { + sql = strings.TrimSpace(sql) + if sql == "" { + continue + } + err := db.Exec(sql).Error + if err != nil { + log.Println("数据库导入失败:" + err.Error()) + return err + } else { + log.Println(sql, "\t success!") + } + } + return nil +} + diff --git a/tools/import_sql_test.go b/tools/import_sql_test.go new file mode 100644 index 0000000..8cb9fed --- /dev/null +++ b/tools/import_sql_test.go @@ -0,0 +1,15 @@ +package tools + +import "testing" + +func TestImportSql(t *testing.T) { + tool:=&ImportSqlTool{ + SqlPath: "../import.sql", + Username: "go-fly", + Password: "go-fly", + Server: "127.0.0.1", + Port: "3306", + Database: "go-fly", + } + tool.ImportSql() +} \ No newline at end of file diff --git a/ws/visitor.go b/ws/visitor.go index 4437c7f..efac998 100644 --- a/ws/visitor.go +++ b/ws/visitor.go @@ -171,12 +171,12 @@ func VisitorAutoReply(vistorInfo models.Visitor, kefuInfo models.User, content s } if !ok || kefu == nil { time.Sleep(1 * time.Second) - welcome := models.FindWelcomeByUserIdKey(kefuInfo.Name, "offline") - if welcome.Content == "" || reply.Content != "" { + welcome := models.FindConfig("OfflineMessage") + if welcome == "" || reply.Content != "" { return } - VisitorMessage(vistorInfo.VisitorId, welcome.Content, kefuInfo) - models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, welcome.Content, "kefu") + VisitorMessage(vistorInfo.VisitorId, welcome, kefuInfo) + models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, welcome, "kefu") } } func CleanVisitorExpire() {