段少珍 3 years ago
parent
commit
5b7364398b
  1. 58
      controllers/BaseController.go
  2. 45
      controllers/UserController.go
  3. 2
      models/UserModel.go
  4. 5
      routers/router.go
  5. 37
      views/user/add.html
  6. 43
      views/user/edit.html
  7. 4
      views/user/footerjs.html
  8. 17
      views/user/footerjs_add.html
  9. 26
      views/user/footerjs_edit.html

58
controllers/BaseController.go

@ -2,14 +2,14 @@ package controllers
import (
"fmt"
"strconv"
// "strconv"
"strings"
"github.com/astaxie/beego"
cache "github.com/patrickmn/go-cache"
// cache "github.com/patrickmn/go-cache"
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
"github.com/ziyoubiancheng/xcms/utils"
// "github.com/ziyoubiancheng/xcms/utils"
)
type BaseController struct {
@ -76,53 +76,17 @@ func (c *BaseController) listJsonResult(code consts.JsonResultCode, msg string,
}
func (c *BaseController) auth() {
arr := strings.Split(c.Ctx.GetCookie("auth"), "|")
c.userId = 0
if len(arr) == 2 {
idstr, password := arr[0], arr[1]
userId, _ := strconv.Atoi(idstr)
if userId > 0 {
var err error
cheUser, found := utils.Cache.Get("uid" + strconv.Itoa(userId))
user := &models.AdminModel{}
if found && cheUser != nil { //从缓存取用户
user = cheUser.(*models.AdminModel)
} else {
user, err = user.GetById(userId)
utils.Cache.Set("uid"+strconv.Itoa(userId), user, cache.DefaultExpiration)
}
if err == nil && password == utils.Md5([]byte(user.Password+user.Salt)) {
c.userId = user.Id
c.loginName = user.LoginName
c.userName = user.RealName
c.user = user
//c.AdminAuth()
}
isHasAuth := strings.Contains(c.allowUrl, c.controllerName+"/"+c.actionName)
//不需要权限检查
noAuth := "ajaxsave/ajaxdel/table/loginin/loginout/getnodes/start/show/ajaxapisave/index/group/public/env/code/apidetail"
isNoAuth := strings.Contains(noAuth, c.actionName)
if isHasAuth == false && isNoAuth == false {
c.Ctx.WriteString("没有权限")
c.jsonResult(consts.JRCodeFailed, "", 0)
return
}
}
}
if c.userId == 0 && (c.controllerName != "login" && c.actionName != "loginin") {
c.redirect(beego.URLFor("LoginController.LoginIn"))
}
// if c.userId == 0 && (c.controllerName != "login" && c.actionName != "loginin") {
// c.redirect(beego.URLFor("LoginController.LoginIn"))
// }
}
// 重定向
func (c *BaseController) redirect(url string) {
c.Redirect(url, 302)
c.StopRun()
}
//// 重定向
//func (c *BaseController) redirect(url string) {
// c.Redirect(url, 302)
// c.StopRun()
//}
// 重定向 去错误页
func (c *BaseController) pageError(msg string) {

45
controllers/UserController.go

@ -1,6 +1,9 @@
package controllers
import (
"fmt"
"github.com/astaxie/beego/orm"
"github.com/ziyoubiancheng/xcms/consts"
"github.com/ziyoubiancheng/xcms/models"
)
@ -29,3 +32,45 @@ func (c *UserController) List() {
result, count := m.List(size, page)
c.listJsonResult(consts.JRCodeSucc, "ok", count, result)
}
func (c *UserController) Add() {
c.LayoutSections = make(map[string]string)
c.LayoutSections["footerjs"] = "user/footerjs_add.html"
c.setTpl("user/add.html", "common/layout_edit.html")
}
func (c UserController) AddDo() {
var m models.UserModel
if err := c.ParseForm(&m); err == nil {
id, _ := orm.NewOrm().Insert(&m)
fmt.Println(m)
c.jsonResult(consts.JRCodeSucc, "ok", id)
} else {
c.jsonResult(consts.JRCodeFailed, "", 0)
}
}
func (c UserController) Edit() {
c.LayoutSections = make(map[string]string)
c.LayoutSections["footerjs"] = "user/footerjs_edit.html"
c.setTpl("user/edit.html", "common/layout_edit.html")
}
func (c UserController) EditDo() {
var m models.UserModel
if err := c.ParseForm(&m); err == nil {
id, _ := orm.NewOrm().Update(&m)
fmt.Println(m)
c.jsonResult(consts.JRCodeSucc, "ok", id)
} else {
c.jsonResult(consts.JRCodeFailed, "", 0)
}
}
func (c *UserController) DeleteDo() {
if uid, err := c.GetInt("uid"); err == nil {
num, _ := orm.NewOrm().Delete(&models.UserModel{UserId: uid})
c.jsonResult(consts.JRCodeSucc, "1", num)
} else {
fmt.Println(err, uid)
c.jsonResult(consts.JRCodeFailed, "", 0)
}
}

2
models/UserModel.go

@ -5,7 +5,7 @@ import (
)
type UserModel struct {
UserId int32 `orm:"pk;auto"`
UserId int `orm:"pk;auto"`
UserName string `orm:"size(64)"`
AuthStr string `orm:"size(512)"`
Password string `orm:"size(128)"`

5
routers/router.go

@ -18,5 +18,10 @@ func init() {
//user
beego.Router("/user", &controllers.UserController{}, "Get:Index")
beego.Router("/user/add", &controllers.UserController{}, "Get:Add")
beego.Router("/user/adddo", &controllers.UserController{}, "*:AddDo")
beego.Router("/user/edit", &controllers.UserController{}, "Get:Edit")
beego.Router("/user/editdo", &controllers.UserController{}, "*:EditDo")
beego.Router("/user/deletedo", &controllers.UserController{}, "Get:DeleteDo")
beego.Router("/user/list", &controllers.UserController{}, "*:List")
}

37
views/user/add.html

@ -0,0 +1,37 @@
<form class="layui-form" action="/menu/adddo" lay-filter="example">
<div class="layui-form-item">
<label class="layui-form-label">菜单名</label>
<div class="layui-input-block">
<input type="text" name="Name" lay-verify="title" autocomplete="off" placeholder="请输入菜单名" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">父菜单</label>
<div class="layui-input-block">
<input type="text" name="Parent" lay-verify="title" autocomplete="off" placeholder="请选择" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序(降序)</label>
<div class="layui-input-block">
<input type="text" name="Seq" lay-verify="title" autocomplete="off" placeholder="请输入序号" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容</label>
<div class="layui-input-block">
<input type="text" name="Fid" lay-verify="title" autocomplete="off" placeholder="选择" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>

43
views/user/edit.html

@ -0,0 +1,43 @@
<form class="layui-form" action="/menu/editdo" lay-filter="example">
<div class="layui-form-item layui-hide">
<label class="layui-form-label">ID</label>
<div class="layui-input-block">
<input type="text" name="Mid" lay-verify="title" autocomplete="off" placeholder="" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">菜单名</label>
<div class="layui-input-block">
<input type="text" name="Name" lay-verify="title" autocomplete="off" placeholder="请输入菜单名" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">父菜单</label>
<div class="layui-input-block">
<input type="text" name="Parent" lay-verify="title" autocomplete="off" placeholder="请选择" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">排序(降序)</label>
<div class="layui-input-block">
<input type="text" name="Seq" lay-verify="title" autocomplete="off" placeholder="请输入序号" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">内容</label>
<div class="layui-input-block">
<input type="text" name="Fid" lay-verify="title" autocomplete="off" placeholder="选择" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>

4
views/user/footerjs.html

@ -9,7 +9,9 @@ layui.use('table', function(){
,cellMinWidth: 60 //全局定义常规单元格的最小宽度,layui 2.2.1 新增
,cols: [[
{field:'UserId', title: 'ID', sort: true}
,{field:'UserName', width:300, title: '菜单名'}
,{field:'UserName', width:300, title: '用户名'}
,{field:'UserName', width:200, title: '权限'}
,{field:'UserName', width:100, title: '用户类型'}
,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]]
,page:true

17
views/user/footerjs_add.html

@ -0,0 +1,17 @@
<script>
layui.use(['form', 'layedit'], function(){
var form = layui.form
,layer = layui.layer
,layedit = layui.layedit
//监听提交
form.on('submit(demo1)', function(data){
parent.location.reload();
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
});
});
</script>

26
views/user/footerjs_edit.html

@ -0,0 +1,26 @@
<script>
layui.use(['form', 'layedit'], function(){
var form = layui.form
,layer = layui.layer
,layedit = layui.layedit
//表单初始赋值
form.val('example', {
"Mid":{{.Mid}}
,"Name": {{.Name}}
,"Parent": {{.Parent}}
,"Seq":{{.Seq}}
,"Fid": {{.Fid}}
})
//监听提交
form.on('submit(demo1)', function(data){
parent.location.reload();
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
parent.layer.close(index); //再执行关闭
});
});
</script>
Loading…
Cancel
Save