diff --git a/config/config.go b/config/config.go index 5c6921d..33c7d17 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ const AccountConf = Dir + "account.json" const MysqlConf = Dir + "mysql.json" const MailConf = Dir + "mail.json" const LangConf=Dir+"language.json" +const MainConf = Dir + "config.json" type Mysql struct{ Server string Port string @@ -27,14 +28,23 @@ type MailServer struct { Server, Email, Password string } type Config struct { - Mysql *Mysql + Upload string } func CreateConfig()*Config{ - mysql :=CreateMysql() + var configObj Config c:=&Config{ - Mysql: mysql, + Upload: "static/upload/", } - return c + isExist, _ := tools.IsFileExist(MainConf) + if !isExist { + return c + } + info, err := ioutil.ReadFile(MainConf) + if err != nil { + return c + } + err = json.Unmarshal(info, &configObj) + return &configObj } func CreateMailServer() *MailServer { var imap MailServer diff --git a/controller/message.go b/controller/message.go index 012fbba..bc6ea5e 100644 --- a/controller/message.go +++ b/controller/message.go @@ -2,9 +2,15 @@ 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" + "os" + "path" + "strings" "time" ) // @Summary 发送消息接口 @@ -106,3 +112,39 @@ func SendMessage(c *gin.Context) { "msg": "ok", }) } +func UploadImg(c *gin.Context){ + config:=config.CreateConfig() + f, err := c.FormFile("imgfile") + if err != nil { + c.JSON(200, gin.H{ + "code": 400, + "msg": "上传失败!", + }) + return + } else { + + fileExt:=strings.ToLower(path.Ext(f.Filename)) + if fileExt!=".png"&&fileExt!=".jpg"&&fileExt!=".gif"&&fileExt!=".jpeg"{ + c.JSON(200, gin.H{ + "code": 400, + "msg": "上传失败!只允许png,jpg,gif,jpeg文件", + }) + return + } + fileName:=tools.Md5(fmt.Sprintf("%s%s",f.Filename,time.Now().String())) + fildDir:=fmt.Sprintf("%s%d%s/",config.Upload,time.Now().Year(),time.Now().Month().String()) + isExist,_:=tools.IsFileExist(fildDir) + if !isExist{ + os.Mkdir(fildDir,os.ModePerm) + } + filepath:=fmt.Sprintf("%s%s%s",fildDir,fileName,fileExt) + c.SaveUploadedFile(f, filepath) + c.JSON(200, gin.H{ + "code": 200, + "msg": "上传成功!", + "result":gin.H{ + "path":filepath, + }, + }) + } +} diff --git a/main.go b/main.go index e8ab988..d8dc88c 100644 --- a/main.go +++ b/main.go @@ -53,6 +53,7 @@ func main() { engine := gin.Default() engine.LoadHTMLGlob("static/html/*") engine.Static("/static", "./static") + //首页 engine.GET("/", controller.Index) engine.GET("/index", tmpl.PageIndex) @@ -75,6 +76,8 @@ func main() { engine.GET("/messages", controller.GetVisitorMessage) //发送单条消息 engine.POST("/message",controller.SendMessage) + //上传文件 + engine.POST("/uploadimg",controller.UploadImg) //获取未读消息数 engine.GET("/message_status",controller.GetVisitorMessage) //设置消息已读 diff --git a/static/css/common.css b/static/css/common.css index 3640481..dd49ca7 100644 --- a/static/css/common.css +++ b/static/css/common.css @@ -4,13 +4,18 @@ .faceBtn, .faceBtn:after, .faceBtn { border: 1px solid; } +.iconBtns{ + border-top:1px solid #e4e4e4; + border-bottom:1px solid #e4e4e4; + padding: 2px 0; +} .visitorFaceBtn{ - float: right; - margin: 16px 4px 0 0; + float: left; + margin-left: 5px; } .visitorFaceBox{ position: absolute; - bottom: 70px; + bottom: 86px; } .kefuFaceBox{ position: absolute; @@ -69,6 +74,52 @@ top: 10%; width: 15px; } +.imageBtn { + width: 32px; + height: 23px; + overflow: hidden; + display: inline-block; + vertical-align: middle; + position: relative; + font-style: normal; + color: #9da0a0; + text-align: left; + text-indent: -9999px; + direction: ltr; + border: 1px solid; +} +.imageBtn:before { + content: ''; + position: absolute; + width: 17px; + height: 16px; + left: -2px; + top: 10px; + -webkit-transform: rotate(45deg); + -moz-transform: rotate(45deg); + -ms-transform: rotate(45deg); + -o-transform: rotate(45deg); + transform: rotate(45deg); + box-shadow: inset 0 0 0 32px, 10px -6px 0 0; +} +.imageBtn:after { + content: ''; + -webkit-border-radius: 50%; + -moz-border-radius: 50%; + -o-border-radius: 50%; + border-radius: 50%; + position: absolute; + width: 3px; + height: 3px; + box-shadow: inset 0 0 0 32px; + top: 5px; + right: 5px +} +.visitorImageBtn{ + float: left; + margin-left: 20px; + margin-top: 2px; +} .faceBox{ width: 100%; background: #fff; diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 9d17d0b..de8df31 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -132,6 +132,7 @@
+
发送
@@ -575,6 +576,40 @@ $('.faceBox').hide(); this.messageContent+="face"+this.face[index].name; }, + //上传图片 + uploadImg (url){ + let _this=this; + $('#uploadImg').after(''); + $("#uploadImgFile").click(); + $("#uploadImgFile").change(function (e) { + var formData = new FormData(); + var file = $("#uploadImgFile")[0].files[0]; + formData.append("imgfile",file); //传给后台的file的key值是可以自己定义的 + filter(file) && $.ajax({ + url: url || '', + type: "post", + data: formData, + contentType: false, + processData: false, + dataType: 'JSON', + mimeType: "multipart/form-data", + success: function (res) { + if(res.code!=200){ + _this.$message({ + message: res.msg, + type: 'error' + }); + }else{ + _this.messageContent+='img[/' + res.result.path + ']'; + _this.chatToUser(); + } + }, + error: function (data) { + console.log(data); + } + }); + }); + }, }, created: function () { //jquery diff --git a/static/html/chat_page.html b/static/html/chat_page.html index 02e1b42..fb7e85d 100644 --- a/static/html/chat_page.html +++ b/static/html/chat_page.html @@ -6,13 +6,13 @@ GO-FLY咨询页 - + - +