diff --git a/config/config.go b/config/config.go index 4867d64..98d571c 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,7 @@ var( const Dir = "config/" const AccountConf = Dir + "account.json" const MysqlConf = Dir + "mysql.json" +const MailConf = Dir + "mail.json" type Mysql struct{ Server string Port string @@ -21,6 +22,9 @@ type Mysql struct{ Username string Password string } +type MailServer struct { + Server, Email, Password string +} type Config struct { Mysql *Mysql } @@ -31,6 +35,20 @@ func CreateConfig()*Config{ } return c } +func CreateMailServer() *MailServer { + var imap MailServer + isExist, _ := tools.IsFileExist(MailConf) + if !isExist { + return &imap + } + info, err := ioutil.ReadFile(MailConf) + if err != nil { + return &imap + } + + err = json.Unmarshal(info, &imap) + return &imap +} func CreateMysql() *Mysql { var mysql Mysql isExist, _ := tools.IsFileExist(MysqlConf) diff --git a/controller/folder.go b/controller/folder.go index 39b5837..06253fa 100644 --- a/controller/folder.go +++ b/controller/folder.go @@ -2,6 +2,8 @@ package controller import ( "encoding/json" + "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/config" "github.com/taoshihan1991/imaptool/tmpl" "github.com/taoshihan1991/imaptool/tools" "io/ioutil" @@ -11,7 +13,62 @@ import ( ) const PageSize = 20 +func GetFolders(c *gin.Context) { + fid := c.Query("fid") + currentPage, _ := strconv.Atoi(c.Query("page")) + if fid == "" { + fid = "INBOX" + } + if currentPage == 0 { + currentPage = 1 + } + + mailServer := config.CreateMailServer() + + var wg sync.WaitGroup + wg.Add(2) + result := make(map[string]interface{}) + go func() { + defer wg.Done() + folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid) + result["folders"] = folders + result["total"] = folders[fid] + }() + go func() { + defer wg.Done() + mails := tools.GetFolderMail(mailServer.Server, mailServer.Email, mailServer.Password, fid, currentPage, PageSize) + result["mails"] = mails + }() + wg.Wait() + result["pagesize"] = PageSize + result["fid"] = fid + + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":result, + }) +} +func GetFolderList(c *gin.Context) { + fid := c.Query("fid") + if fid == "" { + fid = "INBOX" + } + + mailServer := config.CreateMailServer() + + result := make(map[string]interface{}) + folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid) + result["folders"] = folders + result["total"] = folders[fid] + result["fid"] = fid + c.JSON(200, gin.H{ + "code": 200, + "msg": "ok", + "result":result, + }) +} //输出列表 func ActionFolder(w http.ResponseWriter, r *http.Request) { fid := tools.GetUrlArg(r, "fid") diff --git a/main.go b/main.go index 5e036b1..d36dca6 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,10 @@ func main() { engine.GET("/setting_mysql", tmpl.PageSettingMysql) //网页部署 engine.GET("/setting_deploy", tmpl.PageSettingDeploy) + //邮箱列表 + engine.GET("/mail_list", tmpl.PageMailList) + //邮件夹列表 + engine.GET("/folders", controller.GetFolders) engine.GET("/mysql",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.MysqlGetConf) engine.POST("/mysql",middleware.JwtApiMiddleware,middleware.CasbinACL, controller.MysqlSetConf) diff --git a/static/html/list.html b/static/html/list.html index 90fe7db..7f33499 100644 --- a/static/html/list.html +++ b/static/html/list.html @@ -1,4 +1,4 @@ -{{.Header}} +{{template "header" }}