简化代码和修复编码问题

pull/30/head
陶士涵 5 years ago
parent 37f85ad673
commit 21a17ced91

@ -4,4 +4,7 @@ go 1.14
require github.com/emersion/go-imap v1.0.4
require github.com/emersion/go-message v0.11.2
require (
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394
github.com/emersion/go-message v0.11.2
)

@ -1,3 +1,5 @@
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

@ -16,7 +16,7 @@ type IndexData struct {
Mails interface{}
CurrentPage int
NextPage,PrePage string
NumPages map[string]template.HTML
NumPages template.HTML
}
const PAGE_SIZE=20
@ -84,34 +84,37 @@ func list(w http.ResponseWriter, r *http.Request) {
render.NextPage = fmt.Sprintf("/list?fid=%s&page=%d",fid,currentPage+1)
var wg sync.WaitGroup
wg.Add(1)
wg.Add(2)
go func() {
defer wg.Done()
folders :=tools.GetFolders(authStrings[0], authStrings[1], authStrings[2],fid)
render.Folders = folders
}()
mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], fid,currentPage, PAGE_SIZE)
render.Mails = mails
wg.Wait()
PageCount:=(render.Folders[fid]/PAGE_SIZE)
log.Println(PageCount)
numPages:=""
start:=currentPage-5
if start <=0 {
start=1
}
end:=start+11
PageCount:= render.Folders[fid]/PAGE_SIZE
log.Println(PageCount)
numPages:=""
start:=currentPage-5
if start <=0 {
start=1
}
end:=start+11
for i:=start;i<end;i++{
active:=""
if currentPage==i{
active="active"
for i:=start;i<end;i++{
active:=""
if currentPage==i{
active="active"
}
numPages+=fmt.Sprintf("<li class=\"page-item %s\"><a class=\"page-link\" href=\"/list?fid=%s&page=%d\">%d</a></li>",active,fid,i,i)
}
numPages+=fmt.Sprintf("<li class=\"page-item %s\"><a class=\"page-link\" href=\"/list?fid=%s&page=%d\">%d</a></li>",active,fid,i,i)
}
render.NumPages=map[string]template.HTML{"NumPages": template.HTML(numPages)}
render.NumPages=template.HTML(numPages)
}()
go func() {
defer wg.Done()
mails := tools.GetFolderMail(authStrings[0], authStrings[1], authStrings[2], fid, currentPage, PAGE_SIZE)
render.Mails = mails
}()
t, _ := template.ParseFiles("./tmpl/index.html")
wg.Wait()
t.Execute(w, render)
}

@ -44,7 +44,7 @@
<li class="page-item">
<a class="page-link" href="{{.PrePage}}">Previous</a>
</li>
{{.NumPages.NumPages}}
{{.NumPages}}
<li class="page-item">
<a class="page-link" href="{{.NextPage}}">Next</a>
</li>

@ -1,10 +1,15 @@
package tools
import (
"bytes"
"fmt"
"github.com/axgle/mahonia"
"github.com/emersion/go-imap"
"github.com/emersion/go-imap/client"
"io"
"io/ioutil"
"log"
"mime"
"strconv"
"strings"
)
@ -109,10 +114,58 @@ func GetFolderMail(server string, email string, password string,folder string,cu
done <- c.Fetch(seqset, []imap.FetchItem{imap.FetchEnvelope}, messages)
}()
var res []string
for msg := range messages {
res=append(res,msg.Envelope.Subject)
dec :=new(mime.WordDecoder)
dec.CharsetReader= func(charset string, input io.Reader) (io.Reader, error) {
switch charset {
case "gb2312":
content, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
//ret:=bytes.NewReader(content)
//ret:=transform.NewReader(bytes.NewReader(content), simplifiedchinese.HZGB2312.NewEncoder())
utf8str:=ConvertToStr(string(content),"gbk","utf-8")
t:=bytes.NewReader([]byte(utf8str))
//ret:=utf8.DecodeRune(t)
//log.Println(ret)
return t, nil
case "gb18030":
content, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
//ret:=bytes.NewReader(content)
//ret:=transform.NewReader(bytes.NewReader(content), simplifiedchinese.HZGB2312.NewEncoder())
utf8str:=ConvertToStr(string(content),"gbk","utf-8")
t:=bytes.NewReader([]byte(utf8str))
//ret:=utf8.DecodeRune(t)
//log.Println(ret)
return t, nil
default:
return nil,fmt.Errorf("unhandle charset:%s",charset)
}
}
for msg:=range messages{
ret,err:=dec.Decode(msg.Envelope.Subject)
if err!=nil{
ret,_=dec.DecodeHeader(msg.Envelope.Subject)
}
res=append(res,ret)
}
log.Println(res)
log.Println(res)
return res
}
// 任意编码转特定编码
func ConvertToStr(src string, srcCode string, tagCode string) string {
srcCoder := mahonia.NewDecoder(srcCode)
srcResult := srcCoder.ConvertString(src)
tagCoder := mahonia.NewDecoder(tagCode)
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
result := string(cdata)
return result
}
Loading…
Cancel
Save