You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/controller/setting.go

62 lines
1.6 KiB

package controller
import (
"encoding/json"
"fmt"
"github.com/taoshihan1991/imaptool/config"
"github.com/taoshihan1991/imaptool/tmpl"
"github.com/taoshihan1991/imaptool/tools"
"net/http"
"os"
)
5 years ago
func ActionSetting(w http.ResponseWriter, r *http.Request) {
render := tmpl.NewSettingHtml(w)
render.SetLeft("setting_left")
render.SetBottom("setting_bottom")
5 years ago
account := config.GetAccount()
render.Username = account["Username"]
render.Password = account["Password"]
render.Display("setting", render)
}
5 years ago
func SettingAccount(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/json;charset=utf-8;")
5 years ago
username := r.PostFormValue("username")
password := r.PostFormValue("password")
5 years ago
isExist, _ := tools.IsFileExist(config.Dir)
if !isExist {
os.Mkdir(config.Dir, os.ModePerm)
}
5 years ago
fileConfig := config.AccountConf
file, _ := os.OpenFile(fileConfig, os.O_RDWR|os.O_CREATE, os.ModePerm)
5 years ago
format := `{
"Username":"%s",
"Password":"%s"
}
`
5 years ago
data := fmt.Sprintf(format, username, password)
file.WriteString(data)
msg, _ := json.Marshal(tools.JsonResult{Code: 200, Msg: "操作成功!"})
w.Write(msg)
}
5 years ago
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
}
5 years ago
result := config.GetAccount()
msg, _ := json.Marshal(tools.JsonListResult{
JsonResult: tools.JsonResult{Code: 200, Msg: "获取成功"},
Result: result,
})
w.Write(msg)
}