You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/controller/notice.go

109 lines
2.4 KiB

5 years ago
package controller
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
5 years ago
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/models"
"github.com/taoshihan1991/imaptool/tools"
5 years ago
"log"
"net/http"
"time"
5 years ago
)
func GetNotice(c *gin.Context) {
kefuId:=c.Query("kefu_id")
welcomes:=models.FindWelcomesByUserId(kefuId)
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)
4 years ago
}
c.JSON(200, gin.H{
"code": 200,
"msg": "ok",
"result":result,
})
}
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 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":"",
})
}
5 years ago
var upgrader = websocket.Upgrader{}
var oldFolders map[string]int
4 years ago
5 years ago
//推送新邮件到达
4 years ago
func PushMailServer(w http.ResponseWriter, r *http.Request) {
5 years ago
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
mailServer := tools.GetMailServerFromCookie(r)
var msg []byte
if mailServer == nil {
msg, _ = json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
4 years ago
err = c.WriteMessage(mt, msg)
if err != nil {
log.Println("write:", err)
break
}
4 years ago
} else {
folders := tools.GetMailNum(mailServer.Server, mailServer.Email, mailServer.Password)
for name, num := range folders {
if oldFolders[name] != num {
result := make(map[string]interface{})
result["folder_name"] = name
4 years ago
result["new_num"] = num - oldFolders[name]
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
4 years ago
c.WriteMessage(mt, msg)
}
}
4 years ago
oldFolders = folders
5 years ago
}
}
}