mirror of
https://github.com/eiblog/eiblog.git
synced 2026-02-23 06:32:28 +08:00
chore: trash
This commit is contained in:
@@ -3,7 +3,9 @@ package admin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/eiblog/eiblog/pkg/cache"
|
||||
@@ -14,6 +16,13 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// 通知cookie
|
||||
const (
|
||||
NoticeSuccess = "success"
|
||||
NoticeNotice = "notice"
|
||||
NoticeError = "error"
|
||||
)
|
||||
|
||||
// RegisterRoutes register routes
|
||||
func RegisterRoutes(e *gin.Engine) {
|
||||
e.POST("/admin/login", handleAcctLogin)
|
||||
@@ -21,6 +30,9 @@ func RegisterRoutes(e *gin.Engine) {
|
||||
|
||||
// RegisterRoutesAuthz register routes
|
||||
func RegisterRoutesAuthz(group gin.IRoutes) {
|
||||
group.GET("/draft-delete", handleDraftDelete)
|
||||
|
||||
group.POST("/api/account", handleAPIAccount)
|
||||
}
|
||||
|
||||
// handleAcctLogin 登录接口
|
||||
@@ -50,3 +62,150 @@ func handleAcctLogin(c *gin.Context) {
|
||||
})
|
||||
c.Redirect(http.StatusFound, "/admin/profile")
|
||||
}
|
||||
|
||||
// handleDraftDelete 删除草稿
|
||||
func handleDraftDelete(c *gin.Context) {
|
||||
id, err := strconv.Atoi(c.Query("cid"))
|
||||
if err != nil || id < 1 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
||||
return
|
||||
}
|
||||
err = cache.Ei.RemoveArticle(context.Background(), id)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "删除错误"})
|
||||
return
|
||||
}
|
||||
c.Redirect(http.StatusFound, "/admin/write-post")
|
||||
}
|
||||
|
||||
// handleAPIAccount 更新账户信息
|
||||
func handleAPIAccount(c *gin.Context) {
|
||||
e := c.PostForm("email")
|
||||
pn := c.PostForm("phoneNumber")
|
||||
ad := c.PostForm("address")
|
||||
if (e != "" && !tools.ValidateEmail(e)) || (pn != "" &&
|
||||
!tools.ValidatePhoneNo(pn)) {
|
||||
responseNotice(c, NoticeNotice, "参数错误", "")
|
||||
return
|
||||
}
|
||||
|
||||
err := cache.Ei.UpdateAccount(context.Background(), cache.Ei.Account.Username,
|
||||
map[string]interface{}{
|
||||
"email": e,
|
||||
"phone_n": pn,
|
||||
"address": ad,
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Error("handleAPIAccount.UpdateAccount: ", err)
|
||||
responseNotice(c, NoticeNotice, err.Error(), "")
|
||||
return
|
||||
}
|
||||
cache.Ei.Account.Email = e
|
||||
cache.Ei.Account.PhoneN = pn
|
||||
cache.Ei.Account.Address = ad
|
||||
responseNotice(c, NoticeSuccess, "更新成功", "")
|
||||
}
|
||||
|
||||
// handleAPIBlogger 更新博客信息
|
||||
func handleAPIBlogger(c *gin.Context) {
|
||||
bn := c.PostForm("blogName")
|
||||
bt := c.PostForm("bTitle")
|
||||
ba := c.PostForm("beiAn")
|
||||
st := c.PostForm("subTitle")
|
||||
ss := c.PostForm("seriessay")
|
||||
as := c.PostForm("archivessay")
|
||||
if bn == "" || bt == "" {
|
||||
responseNotice(c, NoticeNotice, "参数错误", "")
|
||||
return
|
||||
}
|
||||
|
||||
err := cache.Ei.UpdateBlogger(context.Background(), map[string]interface{}{
|
||||
"blog_name": bn,
|
||||
"b_title": bt,
|
||||
"sub_title": st,
|
||||
"series_say": ss,
|
||||
"archives_say": as,
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Error("handleAPIBlogger.UpdateBlogger: ", err)
|
||||
responseNotice(c, NoticeNotice, err.Error(), "")
|
||||
return
|
||||
}
|
||||
cache.Ei.Blogger.BlogName = bn
|
||||
cache.Ei.Blogger.BTitle = bt
|
||||
cache.Ei.Blogger.BeiAn = ba
|
||||
cache.Ei.Blogger.SubTitle = st
|
||||
cache.Ei.Blogger.SeriesSay = ss
|
||||
cache.Ei.Blogger.ArchivesSay = as
|
||||
cache.PagesCh <- cache.PageSeries
|
||||
cache.PagesCh <- cache.PageArchive
|
||||
responseNotice(c, NoticeSuccess, "更新成功", "")
|
||||
}
|
||||
|
||||
// handleAPIPassword 更新密码
|
||||
func handleAPIPassword(c *gin.Context) {
|
||||
od := c.PostForm("old")
|
||||
nw := c.PostForm("new")
|
||||
cf := c.PostForm("confirm")
|
||||
if nw != cf {
|
||||
responseNotice(c, NoticeNotice, "两次密码输入不一致", "")
|
||||
return
|
||||
}
|
||||
if !tools.ValidatePassword(nw) {
|
||||
responseNotice(c, NoticeNotice, "密码格式错误", "")
|
||||
return
|
||||
}
|
||||
if cache.Ei.Account.Password != tools.EncryptPasswd(cache.Ei.Account.Username, od) {
|
||||
responseNotice(c, NoticeNotice, "原始密码不正确", "")
|
||||
return
|
||||
}
|
||||
newPwd := tools.EncryptPasswd(cache.Ei.Account.Username, nw)
|
||||
|
||||
err := cache.Ei.UpdateAccount(context.Background(), cache.Ei.Account.Username,
|
||||
map[string]interface{}{
|
||||
"password": newPwd,
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Error("handleAPIPassword.UpdateAccount: ", err)
|
||||
responseNotice(c, NoticeNotice, err.Error(), "")
|
||||
return
|
||||
}
|
||||
cache.Ei.Account.Password = newPwd
|
||||
responseNotice(c, NoticeSuccess, "更新成功", "")
|
||||
}
|
||||
|
||||
// handleAPIPostDelete 删除文章,移入回收箱
|
||||
func handleAPIPostDelete(c *gin.Context) {
|
||||
// var ids []int32
|
||||
// for _, v := range c.PostFormArray("cid[]") {
|
||||
// i, err := strconv.Atoi(v)
|
||||
// if err != nil || int32(i) < config.Conf.BlogApp.General.StartID {
|
||||
// responseNotice(c, NoticeNotice, "参数错误", "")
|
||||
// return
|
||||
// }
|
||||
// ids = append(ids, int32(i))
|
||||
// }
|
||||
// err := DelArticles(ids...)
|
||||
// if err != nil {
|
||||
// logd.Error(err)
|
||||
// responseNotice(c, NOTICE_NOTICE, err.Error(), "")
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // elasticsearch
|
||||
// err = ElasticDelIndex(ids)
|
||||
// if err != nil {
|
||||
// logrus.Error("handleAPIPostDelete.")
|
||||
// }
|
||||
// // TODO disqus delete
|
||||
// responseNotice(c, NoticeSuccess, "删除成功", "")
|
||||
}
|
||||
|
||||
func responseNotice(c *gin.Context, typ, content, hl string) {
|
||||
if hl != "" {
|
||||
c.SetCookie("notice_highlight", hl, 86400, "/", "", true, false)
|
||||
}
|
||||
c.SetCookie("notice_type", typ, 86400, "/", "", true, false)
|
||||
c.SetCookie("notice", fmt.Sprintf("[\"%s\"]", content), 86400, "/", "", true, false)
|
||||
c.Redirect(http.StatusFound, c.Request.Referer())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user