diff --git a/controller/folder.go b/controller/folder.go index 8e40d7b..7657c73 100644 --- a/controller/folder.go +++ b/controller/folder.go @@ -46,7 +46,7 @@ func FolderDir(w http.ResponseWriter, r *http.Request){ result["folders"] = folders result["total"] = folders[fid] result["fid"] = fid - msg, _ := json.Marshal(tools.JsonFolders{ + msg, _ := json.Marshal(tools.JsonListResult{ JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, Result: result, }) @@ -90,7 +90,7 @@ func FoldersList(w http.ResponseWriter, r *http.Request) { result["pagesize"] = PageSize result["fid"] = fid - msg, _ := json.Marshal(tools.JsonFolders{ + msg, _ := json.Marshal(tools.JsonListResult{ JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, Result: result, }) diff --git a/controller/setting.go b/controller/setting.go index 767286c..2077f1f 100644 --- a/controller/setting.go +++ b/controller/setting.go @@ -1,11 +1,84 @@ package controller import ( + "encoding/json" + "fmt" "github.com/taoshihan1991/imaptool/tmpl" + "github.com/taoshihan1991/imaptool/tools" + "io/ioutil" "net/http" + "os" ) +const configDir = "config/" +const configFile=configDir+"account.json" func ActionSetting(w http.ResponseWriter, r *http.Request){ - render:=tmpl.NewRender(w) + render:=tmpl.NewSettingHtml(w) + render.SetLeft("setting_left") + render.SetBottom("setting_bottom") + account:=getAccount() + render.Username=account["Username"] + render.Password=account["Password"] render.Display("setting",render) } +func SettingAccount(w http.ResponseWriter, r *http.Request){ + w.Header().Set("content-type", "text/json;charset=utf-8;") + mailServer := tools.GetMailServerFromCookie(r) + + if mailServer == nil { + msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) + w.Write(msg) + return + } + + username:=r.PostFormValue("username") + password:=r.PostFormValue("password") + + isExist,_:=tools.IsFileExist(configDir) + if !isExist{ + os.Mkdir(configDir,os.ModePerm) + } + fileConfig:=configFile + file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm) + + format:=`{ + "Username":"%s", + "Password":"%s" +} +` + data := fmt.Sprintf(format,username,password) + file.WriteString(data) + + msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "操作成功!"}) + w.Write(msg) +} +func SettingGetAccount(w http.ResponseWriter, r *http.Request){ + w.Header().Set("content-type", "text/json;charset=utf-8;") + mailServer := tools.GetMailServerFromCookie(r) + + if mailServer == nil { + msg, _ := json.Marshal(tools.JsonResult{Code: 400, Msg: "验证失败"}) + w.Write(msg) + return + } + result:=getAccount() + msg, _ := json.Marshal(tools.JsonListResult{ + JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, + Result: result, + }) + w.Write(msg) +} +func getAccount()map[string]string{ + var account map[string]string + isExist,_:=tools.IsFileExist(configFile) + if !isExist{ + return account + } + info,err:=ioutil.ReadFile(configFile) + if err!=nil{ + return account + } + + err=json.Unmarshal(info,&account) + return account +} \ No newline at end of file diff --git a/server.go b/server.go index e639322..76e291f 100644 --- a/server.go +++ b/server.go @@ -11,7 +11,6 @@ import ( "sync" ) - func main() { log.Println("listen on 8080...\r\ngo:http://127.0.0.1:8080") //根路径 @@ -36,6 +35,8 @@ func main() { http.HandleFunc("/main", controller.ActionMain) //设置界面 http.HandleFunc("/setting", controller.ActionSetting) + //设置账户接口 + http.HandleFunc("/setting_account", controller.SettingAccount) //发送邮件接口 http.HandleFunc("/send", controller.FolderSend) //监听端口 @@ -113,7 +114,7 @@ func mail(w http.ResponseWriter, r *http.Request) { wg.Wait() result["fid"] = fid - msg, _ := json.Marshal(tools.JsonFolders{ + msg, _ := json.Marshal(tools.JsonListResult{ JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"}, Result: result, }) diff --git a/static/html/list.html b/static/html/list.html index 1ad8e9f..2930fe9 100644 --- a/static/html/list.html +++ b/static/html/list.html @@ -6,8 +6,12 @@ + + + 写信 + - + <{v}> diff --git a/static/html/main.html b/static/html/main.html index 14fbecb..22b8b50 100644 --- a/static/html/main.html +++ b/static/html/main.html @@ -18,7 +18,11 @@ methods: { openIframeUrl(url){ this.iframeUrl=url; - } + }, + //跳转 + openUrl(url){ + window.location.href=url; + }, }, created: function () { } diff --git a/static/html/nav.html b/static/html/nav.html index 8e91516..96cc56c 100644 --- a/static/html/nav.html +++ b/static/html/nav.html @@ -1,10 +1,9 @@ - - 收信 + + 邮箱 - 写信 设置 退出 \ No newline at end of file diff --git a/static/html/setting.html b/static/html/setting.html index 156202a..6ee3108 100644 --- a/static/html/setting.html +++ b/static/html/setting.html @@ -4,22 +4,22 @@