| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- package controller
- import (
- "encoding/json"
- "fmt"
- "git.wanbits.cc/sin/flytalk/config"
- "git.wanbits.cc/sin/flytalk/models"
- "git.wanbits.cc/sin/flytalk/tools"
- "git.wanbits.cc/sin/flytalk/tools/store"
- "git.wanbits.cc/sin/flytalk/ws"
- "github.com/gin-gonic/gin"
- "github.com/gorilla/websocket"
- "os"
- "path"
- "strings"
- "time"
- )
- // @Summary 发送消息接口
- // @Produce json
- // @Accept multipart/form-data
- // @Param from_id formData string true "来源uid"
- // @Param to_id formData string true "目标uid"
- // @Param content formData string true "内容"
- // @Param type formData string true "类型|kefu,visitor"
- // @Success 200 {object} controller.Response
- // @Failure 200 {object} controller.Response
- // @Router /message [post]
- func SendMessage(c *gin.Context) {
- fromId := c.PostForm("from_id")
- toId := c.PostForm("to_id")
- content := c.PostForm("content")
- cType := c.PostForm("type")
- if content == "" {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "内容不能为空",
- })
- return
- }
- var kefuInfo models.User
- var vistorInfo models.Visitor
- if cType == "kefu" {
- kefuInfo = models.FindUser(fromId)
- vistorInfo = models.FindVisitorByVistorId(toId)
- } else if cType == "visitor" {
- vistorInfo = models.FindVisitorByVistorId(fromId)
- kefuInfo = models.FindUser(toId)
- }
- if kefuInfo.ID == 0 || vistorInfo.ID == 0 {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "用户不存在",
- })
- return
- }
- models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType)
- if cType == "kefu" {
- guest, ok := clientList[vistorInfo.VisitorId]
- if guest == nil || !ok {
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- return
- }
- conn := guest.conn
- msg := TypeMessage{
- Type: "message",
- Data: ClientMessage{
- Name: kefuInfo.Nickname,
- Avator: kefuInfo.Avator,
- Id: kefuInfo.Name,
- Time: time.Now().Format("2006-01-02 15:04:05"),
- ToId: vistorInfo.VisitorId,
- Content: content,
- },
- }
- str, _ := json.Marshal(msg)
- PushServerTcp(str)
- conn.WriteMessage(websocket.TextMessage, str)
- }
- if cType == "visitor" {
- kefuConns, ok := kefuList[kefuInfo.Name]
- if kefuConns == nil || !ok {
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- "result": content,
- })
- return
- }
- msg := TypeMessage{
- Type: "message",
- Data: ClientMessage{
- Avator: vistorInfo.Avator,
- Id: vistorInfo.VisitorId,
- Name: vistorInfo.Name,
- ToId: kefuInfo.Name,
- Content: content,
- Time: time.Now().Format("2006-01-02 15:04:05"),
- },
- }
- str, _ := json.Marshal(msg)
- PushServerTcp(str)
- for _, kefuConn := range kefuConns {
- kefuConn.WriteMessage(websocket.TextMessage, str)
- }
- }
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- "result": content,
- })
- }
- func SendMessageV2(c *gin.Context) {
- fromId := c.PostForm("from_id")
- toId := c.PostForm("to_id")
- content := c.PostForm("content")
- cType := c.PostForm("type")
- if content == "" {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "内容不能为空",
- })
- return
- }
- var kefuInfo models.User
- var vistorInfo models.Visitor
- if cType == "kefu" {
- kefuInfo = models.FindUser(fromId)
- vistorInfo = models.FindVisitorByVistorId(toId)
- } else if cType == "visitor" {
- vistorInfo = models.FindVisitorByVistorId(fromId)
- kefuInfo = models.FindUser(toId)
- }
- if kefuInfo.ID == 0 || vistorInfo.ID == 0 {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "用户不存在",
- })
- return
- }
- models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType)
- if cType == "kefu" {
- guest, ok := clientList[vistorInfo.VisitorId]
- if guest == nil || !ok {
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- return
- }
- conn := guest.conn
- msg := TypeMessage{
- Type: "message",
- Data: ClientMessage{
- Name: kefuInfo.Nickname,
- Avator: kefuInfo.Avator,
- Id: kefuInfo.Name,
- Time: time.Now().Format("2006-01-02 15:04:05"),
- ToId: vistorInfo.VisitorId,
- Content: content,
- },
- }
- str, _ := json.Marshal(msg)
- conn.WriteMessage(websocket.TextMessage, str)
- }
- if cType == "visitor" {
- kefuConns, ok := ws.KefuList[kefuInfo.Name]
- if kefuConns == nil || !ok {
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- return
- }
- msg := TypeMessage{
- Type: "message",
- Data: ClientMessage{
- Avator: vistorInfo.Avator,
- Id: vistorInfo.VisitorId,
- Name: vistorInfo.Name,
- ToId: kefuInfo.Name,
- Content: content,
- Time: time.Now().Format("2006-01-02 15:04:05"),
- },
- }
- str, _ := json.Marshal(msg)
- for _, kefuConn := range kefuConns {
- kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
- }
- }
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- }
- func SendVisitorNotice(c *gin.Context) {
- notice := c.Query("msg")
- if notice == "" {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "msg不能为空",
- })
- return
- }
- msg := TypeMessage{
- Type: "notice",
- Data: notice,
- }
- str, _ := json.Marshal(msg)
- for _, visitor := range clientList {
- visitor.conn.WriteMessage(websocket.TextMessage, str)
- }
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- }
- func SendCloseMessage(c *gin.Context) {
- visitorId := c.Query("visitor_id")
- if visitorId == "" {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "visitor_id不能为空",
- })
- return
- }
- msg := TypeMessage{
- Type: "close",
- Data: visitorId,
- }
- str, _ := json.Marshal(msg)
- for _, visitor := range clientList {
- if visitorId == visitor.id {
- visitor.conn.WriteMessage(websocket.TextMessage, str)
- }
- }
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- })
- }
- func UploadImg(c *gin.Context) {
- cfg := config.C
- 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/", cfg.Basic.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)
- // 上传到七牛
- qn := store.NewQn(cfg.Qiniu.Access,
- cfg.Qiniu.Secret, cfg.Qiniu.Bucket,
- cfg.Qiniu.Zone)
- key, err := qn.Upload(filepath)
- if err != nil {
- c.JSON(200, gin.H{
- "code": 400,
- "msg": "上传失败",
- })
- return
- }
- os.Remove(filepath)
- dest := cfg.Qiniu.Domain + key
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "上传成功!",
- "result": gin.H{
- "path": dest,
- },
- })
- }
- }
- func GetMessagesV2(c *gin.Context) {
- visitorId := c.Query("visitor_id")
- kefuId := c.Query("kefu_id")
- messages := models.FindMessagesByVisitorAndKefuId(visitorId, kefuId)
- //result := make([]map[string]interface{}, 0)
- chatMessages := make([]ChatMessage, 0)
- for _, message := range messages {
- //item := make(map[string]interface{})
- var visitor models.Visitor
- var kefu models.User
- if visitor.Name == "" || kefu.Name == "" {
- kefu = models.FindUser(message.KefuId)
- visitor = models.FindVisitorByVistorId(message.VisitorId)
- }
- var chatMessage ChatMessage
- chatMessage.Time = message.CreatedAt.Format("2006-01-02 15:04:05")
- chatMessage.Content = message.Content
- chatMessage.MesType = message.MesType
- if message.MesType == "kefu" {
- chatMessage.Name = kefu.Nickname
- chatMessage.Avator = kefu.Avator
- } else {
- chatMessage.Name = visitor.Name
- chatMessage.Avator = visitor.Avator
- }
- chatMessages = append(chatMessages, chatMessage)
- }
- models.ReadMessageByVisitorId(visitorId)
- c.JSON(200, gin.H{
- "code": 200,
- "msg": "ok",
- "result": chatMessages,
- })
- }
|