写入配置文件

pull/30/head
陶士涵 4 years ago
parent 2c1fae79b7
commit 5b5ff2c779

@ -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,
})

@ -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
}

@ -11,7 +11,6 @@ import (
"sync"
)
func main() {
log.Println("listen on 8080...\r\ngohttp://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,
})

@ -6,8 +6,12 @@
<el-aside>
<el-menu
:default-active="fid">
<el-menu-item v-on:click="openUrl('/write')">
<i class="el-icon-edit"></i>
<span slot="title">写信</span>
</el-menu-item>
<el-menu-item :index="v" v-for="(f,v) in folders" v-bind:key="v" v-on:click="getFolders(1,v,true)">
<i class="el-icon-menu"></i>
<i class="el-icon-message"></i>
<span slot="title"><{v}></span>
</el-menu-item>
</el-menu>

@ -18,7 +18,11 @@
methods: {
openIframeUrl(url){
this.iframeUrl=url;
}
},
//跳转
openUrl(url){
window.location.href=url;
},
},
created: function () {
}

@ -1,10 +1,9 @@
<el-menu
default-active="4"
mode="horizontal">
<el-menu-item index="1" class="mainLogo" v-on:click="openIframeUrl('/list')">GO-IMAP</el-menu-item>
<el-menu-item index="2" v-on:click="openIframeUrl('/list')">收信<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
<el-menu-item class="mainLogo" v-on:click="openUrl('/login')">GO-IMAP</el-menu-item>
<el-menu-item index="2" v-on:click="openIframeUrl('/list')">邮箱<el-badge class="mark" :value="mailTotal" style="margin-bottom: 20px;"/>
</el-menu-item>
<el-menu-item index="3" v-on:click="openIframeUrl('/write')">写信</el-menu-item>
<el-menu-item index="4" v-on:click="openIframeUrl('/setting')">设置</el-menu-item>
<el-menu-item index="10" v-on:click="openIframeUrl('/logout')">退出</el-menu-item>
</el-menu>

@ -4,22 +4,22 @@
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">
<el-aside>
<el-menu default-active="1" @open="1">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-location"></i>
<span>账户中心</span>
</template>
<el-menu-item-group>
<el-menu-item index="1-1">设置密码</el-menu-item>
<el-menu-item index="1-2">新增用户</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
{{.Left}}
</el-aside>
<el-main class="mainMain">
aaa
<el-form :model="account" :rules="rules" ref="account" label-width="120px">
<el-form-item label="用户名" prop="username">
<el-input v-model="account.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input v-model="account.password"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="setAccount('account')">立即创建</el-button>
<el-button @click="resetForm('account')">取消</el-button>
</el-form-item>
</el-form>
</el-main>
</el-container>
@ -28,19 +28,7 @@
</div>
</body>
<script>
new Vue({
el: '#app',
delimiters:["<{","}>"],
data: {
fullscreenLoading:true,
},
methods: {
},
created: function () {
this.fullscreenLoading=false;
}
})
var Password="{{.Password}}";
var Username="{{.Username}}";
</script>
</html>
{{.Bottom}}

@ -0,0 +1,72 @@
<script>
var app=new Vue({
el: '#app',
delimiters:["<{","}>"],
data: {
fullscreenLoading:true,
openIndex:[1],
account: {
username: Username,
password: Password,
},
rules: {
username: [
{ required: true, message: '请输入用户名', trigger: 'blur' },
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' },
],
},
},
methods: {
//提交表单
setAccount(formName){
let _this=this;
this.$refs[formName].validate((valid) => {
if (valid) {
$.post("/setting_account",_this.account,function(data){
if(data.code==200){
_this.$message({
message: data.msg,
type: 'success'
});
}else{
_this.$message({
message: data.msg,
type: 'error'
});
}
});
} else {
return false;
}
});
},
//重置表单
resetForm(formName) {
this.loading=false;
this.$refs[formName].resetFields();
},
//跳转
openUrl(url){
window.location.href=url;
},
//展示提示
showNotice(){
this.fullscreenLoading=false;
this.$message({
message: '配置信息写入同级config目录目录不存在会自动创建',
type: 'warning',
duration:'8000',
showClose:true,
});
}
},
created: function () {
this.showNotice();
}
})
</script>
</html>

@ -0,0 +1,14 @@
<el-menu default-active="1-3" :default-openeds="openIndex">
<el-submenu index="1">
<template slot="title">
<i class="el-icon-s-custom"></i>
<span>账户中心</span>
</template>
<el-menu-item-group>
<el-menu-item index="1-1">设置smtp</el-menu-item>
<el-menu-item index="1-2">设置imap</el-menu-item>
<el-menu-item index="1-3">设置登陆账号</el-menu-item>
<el-menu-item index="1-4" v-on:click="openUrl('/setting_mysql')">设置mysql</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>

@ -9,6 +9,8 @@ import (
type CommonHtml struct{
Header template.HTML
Nav template.HTML
Left template.HTML
Bottom template.HTML
Rw http.ResponseWriter
}
func NewRender(rw http.ResponseWriter)*CommonHtml{
@ -20,6 +22,14 @@ func NewRender(rw http.ResponseWriter)*CommonHtml{
obj.Nav=template.HTML(nav)
return obj
}
func (obj *CommonHtml)SetLeft(file string){
leftStr := tools.FileGetContent("html/"+file+".html")
obj.Left=template.HTML(leftStr)
}
func (obj *CommonHtml)SetBottom(file string){
str := tools.FileGetContent("html/"+file+".html")
obj.Bottom=template.HTML(str)
}
func (obj *CommonHtml)Display(file string,data interface{}){
main := tools.FileGetContent("html/"+file+".html")
t, _ := template.New(file).Parse(main)

@ -0,0 +1,14 @@
package tmpl
import "net/http"
type SettingHtml struct {
*CommonHtml
Username,Password string
}
func NewSettingHtml(w http.ResponseWriter)*SettingHtml{
obj:=new(SettingHtml)
parent:=NewRender(w)
obj.CommonHtml=parent
return obj
}

@ -0,0 +1,20 @@
package tools
import "os"
//判断文件文件夹是否存在
func IsFileExist(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
//我这里判断了如果是0也算不存在
if fileInfo.Size() == 0 {
return false, nil
}
if err == nil {
return true, nil
}
return false, err
}

@ -40,7 +40,7 @@ type JsonResult struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
type JsonFolders struct {
type JsonListResult struct {
JsonResult
Result interface{} `json:"result"`
}

Loading…
Cancel
Save