HFish/view/setting/view.go

237 lines
5.0 KiB
Go
Raw Normal View History

2019-08-07 13:16:23 +08:00
package setting
import (
"github.com/gin-gonic/gin"
"net/http"
2019-09-02 19:12:46 +08:00
"HFish/core/dbUtil"
2019-08-07 13:16:23 +08:00
"strings"
"time"
2019-09-02 19:12:46 +08:00
"HFish/utils/log"
"HFish/error"
2019-12-03 21:28:24 +08:00
"HFish/utils/cache"
2019-08-07 13:16:23 +08:00
)
func Html(c *gin.Context) {
2019-09-02 19:12:46 +08:00
// 获取配置列表
result, err := dbUtil.DB().Table("hfish_setting").Fields("id", "type", "info", "setting_name", "setting_dis", "update_time", "status").Get()
if err != nil {
log.Pr("HFish", "127.0.0.1", "获取配置列表失败", err)
}
2019-08-07 13:16:23 +08:00
c.HTML(http.StatusOK, "setting.html", gin.H{
2019-09-02 19:12:46 +08:00
"dataList": result,
2019-08-07 13:16:23 +08:00
})
}
2019-09-02 19:12:46 +08:00
// 检查是否配置信息
2019-08-07 13:16:23 +08:00
func checkInfo(id string) bool {
2019-09-02 19:12:46 +08:00
result, err := dbUtil.DB().Table("hfish_setting").Fields("id", "type", "info").Where("id", "=", id).First()
if err != nil {
log.Pr("HFish", "127.0.0.1", "检查是否配置信息失败", err)
}
info := result["info"].(string)
typeStr := result["type"].(string)
2019-08-07 13:16:23 +08:00
infoArr := strings.Split(info, "&&")
num := len(infoArr)
if num == 4 && typeStr == "mail" {
return true
}
if num == 2 && typeStr == "login" {
return true
}
2019-08-09 15:13:16 +08:00
if num >= 4 && typeStr == "alertMail" {
return true
}
2019-08-25 16:13:18 +08:00
if num >= 1 && typeStr == "whiteIp" {
return true
}
if num >= 1 && typeStr == "webHook" {
return true
}
2020-02-24 19:50:43 +08:00
if num >= 1 && typeStr == "passwdTM" {
return true
}
2019-08-07 13:16:23 +08:00
return false
}
2019-08-12 13:07:09 +08:00
2019-09-02 19:12:46 +08:00
// 拼接字符串
2019-08-07 13:16:23 +08:00
func joinInfo(args ...string) string {
and := "&&"
info := ""
for _, value := range args {
if value == "" {
return ""
}
info += value + and
}
info = info[:len(info)-2]
return info
}
2019-09-02 19:12:46 +08:00
// 更新配置信息
func updateInfoBase(info string, id string) {
_, err := dbUtil.DB().
Table("hfish_setting").
Data(map[string]interface{}{"info": info, "update_time": time.Now().Format("2006-01-02 15:04")}).
Where("id", id).
Update()
if err != nil {
log.Pr("HFish", "127.0.0.1", "更新配置信息失败", err)
}
2019-08-25 16:13:18 +08:00
}
2019-08-07 13:16:23 +08:00
2019-12-03 21:28:24 +08:00
// 更新邮件群发配置
2019-08-07 13:16:23 +08:00
func UpdateEmailInfo(c *gin.Context) {
email := c.PostForm("email")
id := c.PostForm("id")
pass := c.PostForm("pass")
host := c.PostForm("host")
port := c.PostForm("port")
2019-09-02 19:12:46 +08:00
// 拼接字符串
2019-08-07 13:16:23 +08:00
info := joinInfo(host, port, email, pass)
2019-09-02 19:12:46 +08:00
// 更新
updateInfoBase(info, id)
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
2019-08-07 13:16:23 +08:00
}
2019-08-12 13:07:09 +08:00
2019-12-03 21:28:24 +08:00
// 更新警告邮件配置
2019-08-09 15:13:16 +08:00
func UpdateAlertMail(c *gin.Context) {
email := c.PostForm("email")
id := c.PostForm("id")
2019-08-12 13:07:09 +08:00
receive := c.PostForm("receive")
2019-08-09 15:13:16 +08:00
pass := c.PostForm("pass")
host := c.PostForm("host")
port := c.PostForm("port")
2019-09-02 19:12:46 +08:00
// 拼接字符串
2019-08-12 13:07:09 +08:00
receiveArr := strings.Split(receive, ",")
receiveInfo := joinInfo(receiveArr...)
info := joinInfo(host, port, email, pass, receiveInfo)
2019-09-02 19:12:46 +08:00
// 更新
2019-12-03 21:28:24 +08:00
cache.Setx("MailConfigInfo", info)
2019-09-02 19:12:46 +08:00
updateInfoBase(info, id)
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
2019-08-25 16:13:18 +08:00
}
2019-09-02 19:12:46 +08:00
// 更新ip白名单
2019-08-25 16:13:18 +08:00
func UpdateWhiteIp(c *gin.Context) {
id := c.PostForm("id")
whiteIpList := c.PostForm("whiteIpList")
2019-09-02 19:12:46 +08:00
// 拼接字符串
2019-08-25 16:13:18 +08:00
Arr := strings.Split(whiteIpList, ",")
info := joinInfo(Arr...)
2019-09-02 19:12:46 +08:00
// 更新
2019-12-03 21:28:24 +08:00
cache.Setx("IpConfigInfo", info)
2019-09-02 19:12:46 +08:00
updateInfoBase(info, id)
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
2019-08-25 16:13:18 +08:00
}
2019-09-02 19:12:46 +08:00
// 更新 webHook
func UpdateWebHook(c *gin.Context) {
2019-08-25 16:13:18 +08:00
id := c.PostForm("id")
2019-09-02 19:12:46 +08:00
webHookUrl := c.PostForm("webHookUrl")
// 更新
2019-12-03 21:28:24 +08:00
cache.Setx("HookConfigInfo", webHookUrl)
2019-09-02 19:12:46 +08:00
updateInfoBase(webHookUrl, id)
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
2019-08-09 15:13:16 +08:00
}
2019-08-07 13:16:23 +08:00
2020-02-24 19:50:43 +08:00
// 更新 密码加密符号
func UpdatePasswdTM(c *gin.Context) {
id := c.PostForm("id")
text := c.PostForm("text")
// 更新
cache.Setx("PasswdConfigInfo", text)
updateInfoBase(text, id)
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
}
2019-09-02 19:12:46 +08:00
// 更新设置状态
2019-08-07 13:16:23 +08:00
func UpdateStatusSetting(c *gin.Context) {
id := c.PostForm("id")
status := c.PostForm("status")
if !checkInfo(id) && status == "1" {
2019-09-02 19:12:46 +08:00
c.JSON(http.StatusOK, gin.H{
"code": error.ErrFailConfigCode,
"msg": error.ErrFailConfigMsg,
})
2019-09-03 10:53:22 +08:00
return
2019-09-02 19:12:46 +08:00
}
_, err := dbUtil.DB().
Table("hfish_setting").
Data(map[string]interface{}{"status": status, "update_time": time.Now().Format("2006-01-02 15:04")}).
Where("id", id).
Update()
2019-12-03 21:28:24 +08:00
if id == "2" {
cache.Setx("MailConfigStatus", status)
} else if id == "3" {
cache.Setx("HookConfigStatus", status)
} else if id == "4" {
cache.Setx("IpConfigStatus", status)
2020-02-24 19:50:43 +08:00
} else if id == "4" {
cache.Setx("PasswdConfigStatus", status)
2019-12-03 21:28:24 +08:00
}
2019-09-02 19:12:46 +08:00
if err != nil {
log.Pr("HFish", "127.0.0.1", "更新设置状态失败", err)
2019-08-07 13:16:23 +08:00
}
2019-09-02 19:12:46 +08:00
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
})
2019-08-07 13:16:23 +08:00
}
2019-09-02 19:12:46 +08:00
// 根据id获取设置详情
2019-08-07 13:16:23 +08:00
func GetSettingInfo(c *gin.Context) {
id, _ := c.GetQuery("id")
2019-09-02 19:12:46 +08:00
result, err := dbUtil.DB().Table("hfish_setting").Fields("id", "type", "info", "status").Where("id", "=", id).First()
if err != nil {
log.Pr("HFish", "127.0.0.1", "获取设置详情失败", err)
}
c.JSON(http.StatusOK, gin.H{
"code": error.ErrSuccessCode,
"msg": error.ErrSuccessMsg,
"data": result,
})
2019-08-07 13:16:23 +08:00
}