message.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/gorilla/websocket"
  7. "github.com/wenstudio/gofly/config"
  8. "github.com/wenstudio/gofly/models"
  9. "github.com/wenstudio/gofly/tools"
  10. "github.com/wenstudio/gofly/tools/store"
  11. "github.com/wenstudio/gofly/ws"
  12. "os"
  13. "path"
  14. "strings"
  15. "time"
  16. )
  17. // @Summary 发送消息接口
  18. // @Produce json
  19. // @Accept multipart/form-data
  20. // @Param from_id formData string true "来源uid"
  21. // @Param to_id formData string true "目标uid"
  22. // @Param content formData string true "内容"
  23. // @Param type formData string true "类型|kefu,visitor"
  24. // @Success 200 {object} controller.Response
  25. // @Failure 200 {object} controller.Response
  26. // @Router /message [post]
  27. func SendMessage(c *gin.Context) {
  28. fromId := c.PostForm("from_id")
  29. toId := c.PostForm("to_id")
  30. content := c.PostForm("content")
  31. cType := c.PostForm("type")
  32. if content == "" {
  33. c.JSON(200, gin.H{
  34. "code": 400,
  35. "msg": "内容不能为空",
  36. })
  37. return
  38. }
  39. var kefuInfo models.User
  40. var vistorInfo models.Visitor
  41. if cType == "kefu" {
  42. kefuInfo = models.FindUser(fromId)
  43. vistorInfo = models.FindVisitorByVistorId(toId)
  44. } else if cType == "visitor" {
  45. vistorInfo = models.FindVisitorByVistorId(fromId)
  46. kefuInfo = models.FindUser(toId)
  47. }
  48. if kefuInfo.ID == 0 || vistorInfo.ID == 0 {
  49. c.JSON(200, gin.H{
  50. "code": 400,
  51. "msg": "用户不存在",
  52. })
  53. return
  54. }
  55. models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType)
  56. if cType == "kefu" {
  57. guest, ok := clientList[vistorInfo.VisitorId]
  58. if guest == nil || !ok {
  59. c.JSON(200, gin.H{
  60. "code": 200,
  61. "msg": "ok",
  62. })
  63. return
  64. }
  65. conn := guest.conn
  66. msg := TypeMessage{
  67. Type: "message",
  68. Data: ClientMessage{
  69. Name: kefuInfo.Nickname,
  70. Avator: kefuInfo.Avator,
  71. Id: kefuInfo.Name,
  72. Time: time.Now().Format("2006-01-02 15:04:05"),
  73. ToId: vistorInfo.VisitorId,
  74. Content: content,
  75. },
  76. }
  77. str, _ := json.Marshal(msg)
  78. PushServerTcp(str)
  79. conn.WriteMessage(websocket.TextMessage, str)
  80. }
  81. if cType == "visitor" {
  82. kefuConns, ok := kefuList[kefuInfo.Name]
  83. if kefuConns == nil || !ok {
  84. c.JSON(200, gin.H{
  85. "code": 200,
  86. "msg": "ok",
  87. "result": content,
  88. })
  89. return
  90. }
  91. msg := TypeMessage{
  92. Type: "message",
  93. Data: ClientMessage{
  94. Avator: vistorInfo.Avator,
  95. Id: vistorInfo.VisitorId,
  96. Name: vistorInfo.Name,
  97. ToId: kefuInfo.Name,
  98. Content: content,
  99. Time: time.Now().Format("2006-01-02 15:04:05"),
  100. },
  101. }
  102. str, _ := json.Marshal(msg)
  103. PushServerTcp(str)
  104. for _, kefuConn := range kefuConns {
  105. kefuConn.WriteMessage(websocket.TextMessage, str)
  106. }
  107. }
  108. c.JSON(200, gin.H{
  109. "code": 200,
  110. "msg": "ok",
  111. "result": content,
  112. })
  113. }
  114. func SendMessageV2(c *gin.Context) {
  115. fromId := c.PostForm("from_id")
  116. toId := c.PostForm("to_id")
  117. content := c.PostForm("content")
  118. cType := c.PostForm("type")
  119. if content == "" {
  120. c.JSON(200, gin.H{
  121. "code": 400,
  122. "msg": "内容不能为空",
  123. })
  124. return
  125. }
  126. var kefuInfo models.User
  127. var vistorInfo models.Visitor
  128. if cType == "kefu" {
  129. kefuInfo = models.FindUser(fromId)
  130. vistorInfo = models.FindVisitorByVistorId(toId)
  131. } else if cType == "visitor" {
  132. vistorInfo = models.FindVisitorByVistorId(fromId)
  133. kefuInfo = models.FindUser(toId)
  134. }
  135. if kefuInfo.ID == 0 || vistorInfo.ID == 0 {
  136. c.JSON(200, gin.H{
  137. "code": 400,
  138. "msg": "用户不存在",
  139. })
  140. return
  141. }
  142. models.CreateMessage(kefuInfo.Name, vistorInfo.VisitorId, content, cType)
  143. if cType == "kefu" {
  144. guest, ok := clientList[vistorInfo.VisitorId]
  145. if guest == nil || !ok {
  146. c.JSON(200, gin.H{
  147. "code": 200,
  148. "msg": "ok",
  149. })
  150. return
  151. }
  152. conn := guest.conn
  153. msg := TypeMessage{
  154. Type: "message",
  155. Data: ClientMessage{
  156. Name: kefuInfo.Nickname,
  157. Avator: kefuInfo.Avator,
  158. Id: kefuInfo.Name,
  159. Time: time.Now().Format("2006-01-02 15:04:05"),
  160. ToId: vistorInfo.VisitorId,
  161. Content: content,
  162. },
  163. }
  164. str, _ := json.Marshal(msg)
  165. conn.WriteMessage(websocket.TextMessage, str)
  166. }
  167. if cType == "visitor" {
  168. kefuConns, ok := ws.KefuList[kefuInfo.Name]
  169. if kefuConns == nil || !ok {
  170. c.JSON(200, gin.H{
  171. "code": 200,
  172. "msg": "ok",
  173. })
  174. return
  175. }
  176. msg := TypeMessage{
  177. Type: "message",
  178. Data: ClientMessage{
  179. Avator: vistorInfo.Avator,
  180. Id: vistorInfo.VisitorId,
  181. Name: vistorInfo.Name,
  182. ToId: kefuInfo.Name,
  183. Content: content,
  184. Time: time.Now().Format("2006-01-02 15:04:05"),
  185. },
  186. }
  187. str, _ := json.Marshal(msg)
  188. for _, kefuConn := range kefuConns {
  189. kefuConn.Conn.WriteMessage(websocket.TextMessage, str)
  190. }
  191. }
  192. c.JSON(200, gin.H{
  193. "code": 200,
  194. "msg": "ok",
  195. })
  196. }
  197. func SendVisitorNotice(c *gin.Context) {
  198. notice := c.Query("msg")
  199. if notice == "" {
  200. c.JSON(200, gin.H{
  201. "code": 400,
  202. "msg": "msg不能为空",
  203. })
  204. return
  205. }
  206. msg := TypeMessage{
  207. Type: "notice",
  208. Data: notice,
  209. }
  210. str, _ := json.Marshal(msg)
  211. for _, visitor := range clientList {
  212. visitor.conn.WriteMessage(websocket.TextMessage, str)
  213. }
  214. c.JSON(200, gin.H{
  215. "code": 200,
  216. "msg": "ok",
  217. })
  218. }
  219. func SendCloseMessage(c *gin.Context) {
  220. visitorId := c.Query("visitor_id")
  221. if visitorId == "" {
  222. c.JSON(200, gin.H{
  223. "code": 400,
  224. "msg": "visitor_id不能为空",
  225. })
  226. return
  227. }
  228. msg := TypeMessage{
  229. Type: "close",
  230. Data: visitorId,
  231. }
  232. str, _ := json.Marshal(msg)
  233. for _, visitor := range clientList {
  234. if visitorId == visitor.id {
  235. visitor.conn.WriteMessage(websocket.TextMessage, str)
  236. }
  237. }
  238. c.JSON(200, gin.H{
  239. "code": 200,
  240. "msg": "ok",
  241. })
  242. }
  243. func UploadImg(c *gin.Context) {
  244. cfg := config.CreateConfig()
  245. f, err := c.FormFile("imgfile")
  246. if err != nil {
  247. c.JSON(200, gin.H{
  248. "code": 400,
  249. "msg": "上传失败!",
  250. })
  251. return
  252. } else {
  253. fileExt := strings.ToLower(path.Ext(f.Filename))
  254. if fileExt != ".png" && fileExt != ".jpg" && fileExt != ".gif" && fileExt != ".jpeg" {
  255. c.JSON(200, gin.H{
  256. "code": 400,
  257. "msg": "上传失败!只允许png,jpg,gif,jpeg文件",
  258. })
  259. return
  260. }
  261. fileName := tools.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
  262. fildDir := fmt.Sprintf("%s%d%s/", cfg.Upload, time.Now().Year(), time.Now().Month().String())
  263. isExist, _ := tools.IsFileExist(fildDir)
  264. if !isExist {
  265. os.Mkdir(fildDir, os.ModePerm)
  266. }
  267. filepath := fmt.Sprintf("%s%s%s", fildDir, fileName, fileExt)
  268. c.SaveUploadedFile(f, filepath)
  269. // 上传到七牛
  270. qn := store.NewQn(config.QiniuConfig.Access,
  271. config.QiniuConfig.Secret, config.QiniuConfig.Bucket,
  272. config.QiniuConfig.Zone)
  273. key, err := qn.Upload(filepath)
  274. if err != nil {
  275. c.JSON(200, gin.H{
  276. "code": 400,
  277. "msg": "上传失败",
  278. })
  279. return
  280. }
  281. os.Remove(filepath)
  282. dest := config.QiniuConfig.Domain + key
  283. c.JSON(200, gin.H{
  284. "code": 200,
  285. "msg": "上传成功!",
  286. "result": gin.H{
  287. "path": dest,
  288. },
  289. })
  290. }
  291. }
  292. func GetMessagesV2(c *gin.Context) {
  293. visitorId := c.Query("visitor_id")
  294. kefuId := c.Query("kefu_id")
  295. messages := models.FindMessagesByVisitorAndKefuId(visitorId, kefuId)
  296. //result := make([]map[string]interface{}, 0)
  297. chatMessages := make([]ChatMessage, 0)
  298. for _, message := range messages {
  299. //item := make(map[string]interface{})
  300. var visitor models.Visitor
  301. var kefu models.User
  302. if visitor.Name == "" || kefu.Name == "" {
  303. kefu = models.FindUser(message.KefuId)
  304. visitor = models.FindVisitorByVistorId(message.VisitorId)
  305. }
  306. var chatMessage ChatMessage
  307. chatMessage.Time = message.CreatedAt.Format("2006-01-02 15:04:05")
  308. chatMessage.Content = message.Content
  309. chatMessage.MesType = message.MesType
  310. if message.MesType == "kefu" {
  311. chatMessage.Name = kefu.Nickname
  312. chatMessage.Avator = kefu.Avator
  313. } else {
  314. chatMessage.Name = visitor.Name
  315. chatMessage.Avator = visitor.Avator
  316. }
  317. chatMessages = append(chatMessages, chatMessage)
  318. }
  319. models.ReadMessageByVisitorId(visitorId)
  320. c.JSON(200, gin.H{
  321. "code": 200,
  322. "msg": "ok",
  323. "result": chatMessages,
  324. })
  325. }