folder.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package controller
  2. import (
  3. "encoding/json"
  4. "git.wanbits.cc/sin/flytalk/config"
  5. "git.wanbits.cc/sin/flytalk/tmpl"
  6. "git.wanbits.cc/sin/flytalk/tools"
  7. "github.com/gin-gonic/gin"
  8. "io/ioutil"
  9. "net/http"
  10. "strconv"
  11. "sync"
  12. )
  13. const PageSize = 20
  14. func GetFolders(c *gin.Context) {
  15. fid := c.Query("fid")
  16. currentPage, _ := strconv.Atoi(c.Query("page"))
  17. if fid == "" {
  18. fid = "INBOX"
  19. }
  20. if currentPage == 0 {
  21. currentPage = 1
  22. }
  23. imap := config.C.Imap
  24. var wg sync.WaitGroup
  25. wg.Add(2)
  26. result := make(map[string]interface{})
  27. go func() {
  28. defer wg.Done()
  29. folders := tools.GetFolders(imap.Server, imap.Email, imap.Password, fid)
  30. result["folders"] = folders
  31. result["total"] = folders[fid]
  32. }()
  33. go func() {
  34. defer wg.Done()
  35. mails := tools.GetFolderMail(imap.Server, imap.Email, imap.Password, fid, currentPage, PageSize)
  36. result["mails"] = mails
  37. }()
  38. wg.Wait()
  39. result["pagesize"] = PageSize
  40. result["fid"] = fid
  41. c.JSON(200, gin.H{
  42. "code": 200,
  43. "msg": "ok",
  44. "result": result,
  45. })
  46. }
  47. func GetFolderList(c *gin.Context) {
  48. fid := c.Query("fid")
  49. if fid == "" {
  50. fid = "INBOX"
  51. }
  52. imap := config.C.Imap
  53. result := make(map[string]interface{})
  54. folders := tools.GetFolders(imap.Server, imap.Email, imap.Password, fid)
  55. result["folders"] = folders
  56. result["total"] = folders[fid]
  57. result["fid"] = fid
  58. c.JSON(200, gin.H{
  59. "code": 200,
  60. "msg": "ok",
  61. "result": result,
  62. })
  63. }
  64. //输出列表
  65. func ActionFolder(w http.ResponseWriter, r *http.Request) {
  66. fid := tools.GetUrlArg(r, "fid")
  67. currentPage, _ := strconv.Atoi(tools.GetUrlArg(r, "page"))
  68. if fid == "" {
  69. fid = "INBOX"
  70. }
  71. if currentPage == 0 {
  72. currentPage = 1
  73. }
  74. render := tmpl.NewFolderHtml(w)
  75. render.CurrentPage = currentPage
  76. render.Fid = fid
  77. render.Display("list", render)
  78. }
  79. //写信界面
  80. func ActionWrite(w http.ResponseWriter, r *http.Request) {
  81. render := tmpl.NewRender(w)
  82. render.SetLeft("mail_left")
  83. render.Display("write", nil)
  84. }
  85. //读信界面
  86. func ActionDetail(w http.ResponseWriter, r *http.Request) {
  87. fid := tools.GetUrlArg(r, "fid")
  88. id, _ := strconv.Atoi(tools.GetUrlArg(r, "id"))
  89. render := tmpl.NewDetailHtml(w)
  90. render.SetLeft("mail_left")
  91. render.Fid = fid
  92. render.Id = uint32(id)
  93. render.Display("mail_detail", render)
  94. }
  95. //获取邮件夹接口
  96. func FolderDir(w http.ResponseWriter, r *http.Request) {
  97. fid := tools.GetUrlArg(r, "fid")
  98. if fid == "" {
  99. fid = "INBOX"
  100. }
  101. mailServer := tools.GetMailServerFromCookie(r)
  102. w.Header().Set("content-type", "text/json;charset=utf-8;")
  103. if mailServer == nil {
  104. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
  105. w.Write(msg)
  106. return
  107. }
  108. result := make(map[string]interface{})
  109. folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid)
  110. result["folders"] = folders
  111. result["total"] = folders[fid]
  112. result["fid"] = fid
  113. msg, _ := json.Marshal(tools.JsonListResult{
  114. JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
  115. Result: result,
  116. })
  117. w.Write(msg)
  118. }
  119. //邮件夹接口
  120. func FoldersList(w http.ResponseWriter, r *http.Request) {
  121. fid := tools.GetUrlArg(r, "fid")
  122. currentPage, _ := strconv.Atoi(tools.GetUrlArg(r, "page"))
  123. if fid == "" {
  124. fid = "INBOX"
  125. }
  126. if currentPage == 0 {
  127. currentPage = 1
  128. }
  129. mailServer := tools.GetMailServerFromCookie(r)
  130. w.Header().Set("content-type", "text/json;charset=utf-8;")
  131. if mailServer == nil {
  132. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
  133. w.Write(msg)
  134. return
  135. }
  136. var wg sync.WaitGroup
  137. wg.Add(2)
  138. result := make(map[string]interface{})
  139. go func() {
  140. defer wg.Done()
  141. folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid)
  142. result["folders"] = folders
  143. result["total"] = folders[fid]
  144. }()
  145. go func() {
  146. defer wg.Done()
  147. mails := tools.GetFolderMail(mailServer.Server, mailServer.Email, mailServer.Password, fid, currentPage, PageSize)
  148. result["mails"] = mails
  149. }()
  150. wg.Wait()
  151. result["pagesize"] = PageSize
  152. result["fid"] = fid
  153. msg, _ := json.Marshal(tools.JsonListResult{
  154. JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
  155. Result: result,
  156. })
  157. w.Write(msg)
  158. }
  159. //邮件接口
  160. func FolderMail(w http.ResponseWriter, r *http.Request) {
  161. fid := tools.GetUrlArg(r, "fid")
  162. id, _ := strconv.Atoi(tools.GetUrlArg(r, "id"))
  163. mailServer := tools.GetMailServerFromCookie(r)
  164. w.Header().Set("content-type", "text/json;charset=utf-8;")
  165. if mailServer == nil {
  166. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
  167. w.Write(msg)
  168. return
  169. }
  170. var wg sync.WaitGroup
  171. result := make(map[string]interface{})
  172. wg.Add(2)
  173. go func() {
  174. defer wg.Done()
  175. folders := tools.GetFolders(mailServer.Server, mailServer.Email, mailServer.Password, fid)
  176. result["folders"] = folders
  177. result["total"] = folders[fid]
  178. }()
  179. go func() {
  180. defer wg.Done()
  181. mail := tools.GetMessage(mailServer.Server, mailServer.Email, mailServer.Password, fid, uint32(id))
  182. result["from"] = mail.From
  183. result["to"] = mail.To
  184. result["subject"] = mail.Subject
  185. result["date"] = mail.Date
  186. result["html"] = mail.Body
  187. }()
  188. wg.Wait()
  189. result["fid"] = fid
  190. msg, _ := json.Marshal(tools.JsonListResult{
  191. JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
  192. Result: result,
  193. })
  194. w.Write(msg)
  195. }
  196. //发送邮件接口
  197. func FolderSend(w http.ResponseWriter, r *http.Request) {
  198. w.Header().Set("content-type", "text/json;charset=utf-8;")
  199. mailServer := tools.GetMailServerFromCookie(r)
  200. if mailServer == nil {
  201. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"})
  202. w.Write(msg)
  203. return
  204. }
  205. bodyBytes, err := ioutil.ReadAll(r.Body)
  206. if err != nil {
  207. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败," + err.Error()})
  208. w.Write(msg)
  209. return
  210. }
  211. var sendData tools.SmtpBody
  212. err = json.Unmarshal(bodyBytes, &sendData)
  213. if err != nil {
  214. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "操作失败," + err.Error()})
  215. w.Write(msg)
  216. return
  217. }
  218. smtpServer := sendData.Smtp
  219. smtpFrom := mailServer.Email
  220. smtpTo := sendData.To
  221. smtpBody := sendData.Body
  222. smtpPass := mailServer.Password
  223. smtpSubject := sendData.Subject
  224. err = tools.Send(smtpServer, smtpFrom, smtpPass, smtpTo, smtpSubject, smtpBody)
  225. if err != nil {
  226. msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: err.Error()})
  227. w.Write(msg)
  228. return
  229. }
  230. msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "发送成功!"})
  231. w.Write(msg)
  232. }