diff --git a/config/go-fly.sql b/config/go-fly.sql index 0b1592b..1078243 100644 --- a/config/go-fly.sql +++ b/config/go-fly.sql @@ -139,11 +139,12 @@ CREATE TABLE `reply_group` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8| DROP TABLE IF EXISTS `reply_item`| CREATE TABLE `reply_item` ( - `id` int(11) NOT NULL, + `id` int(11) NOT NULL AUTO_INCREMENT, `content` varchar(1024) NOT NULL DEFAULT '', `group_id` int(11) NOT NULL DEFAULT '0', - `user_id` int(11) NOT NULL DEFAULT '0', + `user_id` varchar(50) NOT NULL DEFAULT '', `item_name` varchar(50) NOT NULL DEFAULT '', + PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `group_id` (`group_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8| \ No newline at end of file diff --git a/controller/reply.go b/controller/reply.go index ce7a55d..fe7625e 100644 --- a/controller/reply.go +++ b/controller/reply.go @@ -9,6 +9,10 @@ import ( type ReplyForm struct { GroupName string `form:"group_name" binding:"required"` } +type ReplyContentForm struct { + GroupId string `form:"group_id" binding:"required"` + Content string `form:"content" binding:"required"` +} func GetReplys(c *gin.Context) { kefuId, _ := c.Get("kefu_name") @@ -22,11 +26,53 @@ func GetReplys(c *gin.Context) { } func PostReply(c *gin.Context) { var replyForm ReplyForm + kefuId, _ := c.Get("kefu_name") err := c.Bind(&replyForm) if err != nil { c.JSON(200, gin.H{ + "code": 400, + "msg": "error:" + err.Error(), + }) + return + } + models.CreateReplyGroup(replyForm.GroupName, kefuId.(string)) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} +func PostReplyContent(c *gin.Context) { + var replyContentForm ReplyContentForm + kefuId, _ := c.Get("kefu_name") + err := c.Bind(&replyContentForm) + if err != nil { + c.JSON(400, gin.H{ "code": 200, "msg": "error:" + err.Error(), }) + return } + models.CreateReplyContent(replyContentForm.GroupId, kefuId.(string), replyContentForm.Content) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} +func DelReplyContent(c *gin.Context) { + kefuId, _ := c.Get("kefu_name") + id := c.Query("id") + models.DeleteReplyContent(id, kefuId.(string)) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) +} +func DelReplyGroup(c *gin.Context) { + kefuId, _ := c.Get("kefu_name") + id := c.Query("id") + models.DeleteReplyGroup(id, kefuId.(string)) + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + }) } diff --git a/models/replys.go b/models/replys.go index d7e2585..9dd98bc 100644 --- a/models/replys.go +++ b/models/replys.go @@ -5,18 +5,30 @@ type ReplyItem struct { Content string `json:"item_content"` GroupId string `json:"group_id"` ItemName string `json:"item_name"` + UserId string `json:"user_id"` } type ReplyGroup struct { - Id string `json:"group_id"` - GroupName string `json:"group_name"` - UserId string `json:"user_id"` - Items []ReplyItem `json:"items"` + Id string `json:"group_id"` + GroupName string `json:"group_name"` + UserId string `json:"user_id"` + Items []*ReplyItem `json:"items";"` } -func FindReplyByUserId(userId interface{}) ReplyGroup { - var replyGroup ReplyGroup - DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroup) - return replyGroup +func FindReplyByUserId(userId interface{}) []*ReplyGroup { + var replyGroups []*ReplyGroup + //DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroups) + var replyItems []*ReplyItem + DB.Where("user_id = ?", userId).Find(&replyGroups) + DB.Where("user_id = ?", userId).Find(&replyItems) + temp := make(map[string]*ReplyGroup) + for _, replyGroup := range replyGroups { + replyGroup.Items = make([]*ReplyItem, 0) + temp[replyGroup.Id] = replyGroup + } + for _, replyItem := range replyItems { + temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem) + } + return replyGroups } func CreateReplyGroup(groupName string, userId string) { g := &ReplyGroup{ @@ -25,3 +37,19 @@ func CreateReplyGroup(groupName string, userId string) { } DB.Create(g) } +func CreateReplyContent(groupId string, userId string, content string) { + g := &ReplyItem{ + GroupId: groupId, + UserId: userId, + Content: content, + ItemName: "", + } + DB.Create(g) +} +func DeleteReplyContent(id string, userId string) { + DB.Where("user_id = ? and id = ?", userId, id).Delete(ReplyItem{}) +} +func DeleteReplyGroup(id string, userId string) { + DB.Where("user_id = ? and id = ?", userId, id).Delete(ReplyGroup{}) + DB.Where("user_id = ? and group_id = ?", userId, id).Delete(ReplyItem{}) +} diff --git a/router/api.go b/router/api.go index 32cddbf..24fbad2 100644 --- a/router/api.go +++ b/router/api.go @@ -83,6 +83,9 @@ func InitApiRouter(engine *gin.Engine) { engine.GET("/config", controller.GetConfig) engine.GET("/replys", middleware.JwtApiMiddleware, controller.GetReplys) engine.POST("/reply", middleware.JwtApiMiddleware, controller.PostReply) + engine.POST("/reply_content", middleware.JwtApiMiddleware, controller.PostReplyContent) + engine.DELETE("/reply_content", middleware.JwtApiMiddleware, controller.DelReplyContent) + engine.DELETE("/reply", middleware.JwtApiMiddleware, controller.DelReplyGroup) //微信接口 engine.GET("/micro_program", middleware.JwtApiMiddleware, controller.GetCheckWeixinSign) } diff --git a/static/css/common.css b/static/css/common.css index bbdedc6..b7d489a 100644 --- a/static/css/common.css +++ b/static/css/common.css @@ -341,6 +341,7 @@ background: #fff; border: solid 1px #e6e6e6; margin-top: 5px; + margin-bottom: 50px; } .replyItem:hover{ background-color: #f0f9eb; diff --git a/static/html/chat_main.html b/static/html/chat_main.html index 4d8b07a..31ea960 100644 --- a/static/html/chat_main.html +++ b/static/html/chat_main.html @@ -4,7 +4,7 @@