新增实时新邮件提醒

pull/30/head
陶士涵 5 years ago
parent ce37a9f432
commit 4547cbd766

@ -89,6 +89,8 @@ func FoldersList(w http.ResponseWriter, r *http.Request) {
w.Write(msg)
return
}
var wg sync.WaitGroup
wg.Add(2)
result := make(map[string]interface{})

@ -1,10 +1,14 @@
package controller
import(
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/taoshihan1991/imaptool/tools"
"log"
"net/http"
)
var upgrader = websocket.Upgrader{}
var oldFolders map[string]int
//推送新邮件到达
func PushMailServer(w http.ResponseWriter, r *http.Request){
c, err := upgrader.Upgrade(w, r, nil)
@ -20,10 +24,26 @@ func PushMailServer(w http.ResponseWriter, r *http.Request){
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, []byte("{\"title\":\"测试\"}"))
if err != nil {
log.Println("write:", err)
break
mailServer := tools.GetMailServerFromCookie(r)
var msg []byte
if mailServer == nil {
msg, _ = json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
err = c.WriteMessage(mt,msg)
if err != nil {
log.Println("write:", err)
break
}
}else{
folders:=tools.GetMailNum(mailServer.Server, mailServer.Email, mailServer.Password)
log.Println(folders)
log.Println(oldFolders)
for name,num:=range folders{
if oldFolders[name]!=num{
msg, _ = json.Marshal(tools.JsonResult{Code: 200, Msg: fmt.Sprintf("%s:%d",name,num)})
c.WriteMessage(mt,msg)
}
}
oldFolders=folders
}
}
}

@ -114,6 +114,13 @@
},
websocketonmessage(e){ //数据接收
const redata = JSON.parse(e.data);
if (redata.code==200){
this.$notify({
title: '新邮件',
message: redata.msg,
type: 'success'
});
}
},
websocketsend(Data){//数据发送
this.websock.send(Data);

@ -63,7 +63,33 @@ func connect(server string, email string, password string) *client.Client {
}
return c
}
//获取邮件总数
func GetMailNum(server string, email string, password string) map[string]int {
var c *client.Client
//defer c.Logout()
c = connect(server, email, password)
if c == nil {
return nil
}
// 列邮箱
mailboxes := make(chan *imap.MailboxInfo, 10)
done := make(chan error, 1)
go func() {
done <- c.List("", "*", mailboxes)
}()
//// 存储邮件夹
var folders = make(map[string]int)
for m := range mailboxes {
folders[m.Name] = 0
}
for m, _ := range folders {
mbox, _ := c.Select(m, true)
if mbox != nil {
folders[m] = int(mbox.Messages)
}
}
return folders
}
//获取邮件夹
func GetFolders(server string, email string, password string, folder string) map[string]int {
var c *client.Client

Loading…
Cancel
Save