65 Star 395 Fork 128

admpub/nging

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
account.go 4.99 KB
一键复制 编辑 原始数据 按行查看 历史
admpub 提交于 2020-02-21 10:57 . 表名称统一增加nging前缀
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package database
import (
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/dbmanager/driver"
"github.com/admpub/nging/application/library/dbmanager/driver/mysql"
"github.com/admpub/nging/application/model"
)
func init() {
handler.RegisterToGroup(`/db`, func(g echo.RouteRegister) {
e := handler.Echo()
g.Route(`GET,POST`, ``, Manager)
g.Route(`GET,POST`, `/account`, e.MetaHandler(echo.H{`name`: `账号列表`}, AccountIndex))
g.Route(`GET,POST`, `/account_add`, e.MetaHandler(echo.H{`name`: `添加账号`}, AccountAdd))
g.Route(`GET,POST`, `/account_edit`, e.MetaHandler(echo.H{`name`: `修改账号`}, AccountEdit))
g.Route(`GET,POST`, `/account_delete`, e.MetaHandler(echo.H{`name`: `删除账号`}, AccountDelete))
})
}
func AccountIndex(ctx echo.Context) error {
user := handler.User(ctx)
m := model.NewDbAccount(ctx)
page, size, totalRows, p := handler.PagingWithPagination(ctx)
cond := db.Compounds{
db.Cond{`uid`: user.Id},
}
q := ctx.Formx(`q`).String()
if len(q) > 0 {
cond.AddKV(`name`, db.Like(`%`+q+`%`))
}
cnt, err := m.List(nil, func(r db.Result) db.Result {
return r.OrderBy(`-id`)
}, page, size, cond.And())
if totalRows <= 0 {
totalRows = int(cnt())
p.SetRows(totalRows)
}
ret := handler.Err(ctx, err)
driverList := map[string]string{}
for driverName, driver := range driver.GetAll() {
driverList[driverName] = driver.Name()
}
ctx.Set(`driverList`, driverList)
ctx.Set(`pagination`, p)
ctx.Set(`listData`, m.Objects())
ctx.Set(`activeURL`, `/db/account`)
return ctx.Render(`db/account`, ret)
}
func setOptions(ctx echo.Context, m *model.DbAccount) error {
return m.SetOptions()
}
func AccountAdd(ctx echo.Context) error {
user := handler.User(ctx)
var err error
if ctx.IsPost() {
m := model.NewDbAccount(ctx)
err = ctx.MustBind(m.NgingDbAccount)
if err == nil {
err = setOptions(ctx, m)
}
if err == nil {
m.Uid = user.Id
_, err = m.Add()
if err == nil {
if ctx.IsAjax() {
data := ctx.Data().SetInfo(ctx.T(`数据库账号成功`)).SetData(m.NgingDbAccount)
return ctx.JSON(data)
}
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/db/account`))
}
}
if err != nil && ctx.IsAjax() {
return ctx.JSON(ctx.Data().SetError(err))
}
}
ret := handler.Err(ctx, err)
driverList := map[string]string{}
for driverName, driver := range driver.GetAll() {
driverList[driverName] = driver.Name()
}
ctx.Set(`driverList`, driverList)
ctx.Set(`activeURL`, `/db/account_add`)
ctx.Set(`charsetList`, mysql.Charsets)
return ctx.Render(`db/account_edit`, ret)
}
func AccountEdit(ctx echo.Context) error {
user := handler.User(ctx)
var err error
id := ctx.Formx(`id`).Uint()
m := model.NewDbAccount(ctx)
cond := db.And(db.Cond{`id`: id}, db.Cond{`uid`: user.Id})
err = m.Get(nil, cond)
if ctx.IsPost() {
err = ctx.MustBind(m.NgingDbAccount, echo.ExcludeFieldName(`created`, `uid`))
if err == nil {
err = setOptions(ctx, m)
}
if err == nil {
m.Id = id
err = m.Edit(id, nil, cond)
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
return ctx.Redirect(handler.URLFor(`/db/account`))
}
}
} else if err == nil {
echo.StructToForm(ctx, m.NgingDbAccount, ``, echo.LowerCaseFirstLetter)
var charset string
if len(m.NgingDbAccount.Options) > 0 {
options := echo.H{}
com.JSONDecode(com.Str2bytes(m.NgingDbAccount.Options), &options)
charset = options.String(`charset`)
}
if len(charset) == 0 {
charset = `utf8mb4`
}
ctx.Request().Form().Set(`charset`, charset)
}
ret := handler.Err(ctx, err)
driverList := map[string]string{}
for driverName, driver := range driver.GetAll() {
driverList[driverName] = driver.Name()
}
ctx.Set(`driverList`, driverList)
ctx.Set(`activeURL`, `/db/account`)
ctx.Set(`charsetList`, mysql.Charsets)
return ctx.Render(`db/account_edit`, ret)
}
func AccountDelete(ctx echo.Context) error {
id := ctx.Formx(`id`).Uint()
m := model.NewDbAccount(ctx)
err := m.Delete(nil, db.Cond{`id`: id})
if err == nil {
handler.SendOk(ctx, ctx.T(`操作成功`))
} else {
handler.SendFail(ctx, err.Error())
}
return ctx.Redirect(handler.URLFor(`/db/account`))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/admpub/nging.git
git@gitee.com:admpub/nging.git
admpub
nging
nging
v2.2.1

搜索帮助