实现用户组

dev
Minho 2018-04-11 16:35:45 +08:00
parent d2f882eff6
commit 2bc88e9d1c
6 changed files with 679 additions and 121 deletions

View File

@ -722,9 +722,77 @@ func (c *ManagerController) LabelDelete() {
} }
} }
// 用户组列表
func (c *ManagerController) MemberGroupList() {
c.Prepare()
c.TplName = "manager/member_group_list.gohtml"
pageIndex, _ := c.GetInt("page", 1)
memberGroupList ,totalCount,err := models.NewMemberGroup().FindByPager(pageIndex,conf.PageSize)
if err != nil {
c.ShowErrorPage(50001,"获取用户组失败")
}
if totalCount > 0 {
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
c.Data["PageHtml"] = pager.HtmlPages()
} else {
c.Data["PageHtml"] = ""
}
c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
c.Data["Lists"] = memberGroupList
}
//编辑或添加用户组
func (c *ManagerController) MemberGroupEdit() {
c.Prepare()
c.TplName = "manager/member_group_edit.gohtml"
if c.Ctx.Input.IsPost() {
}
groupId,_ := c.GetInt("group_id",0)
memberGroup := models.NewMemberGroup()
var err error
if groupId > 0 {
memberGroup,err = memberGroup.FindFirst(groupId)
if err != nil {
c.ShowErrorPage(500,"查询用户组失败")
}
}
c.Data["Model"] = memberGroup
}
//用户组成员列表
func (c *ManagerController) MemberGroupMemberList() {
c.Prepare()
c.TplName = "manager/member_group_member_list.gohtml"
pageIndex, _ := c.GetInt("page", 1)
groupId,_ := c.GetInt("group_id",0)
if groupId <= 0 {
c.ShowErrorPage(404,"用户组参数不能为空")
}
memberGroupMemberList ,totalCount,err := models.NewMemberGroupMembers().FindToPager(pageIndex,conf.PageSize,groupId)
if err != nil {
c.ShowErrorPage(50001,"获取用户组失败")
}
if totalCount > 0 {
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
c.Data["PageHtml"] = pager.HtmlPages()
} else {
c.Data["PageHtml"] = ""
}
c.Data["TotalPages"] = int(math.Ceil(float64(totalCount) / float64(conf.PageSize)))
c.Data["Lists"] = memberGroupMemberList
}

View File

@ -0,0 +1,222 @@
package models
import (
"github.com/lifei6671/mindoc/conf"
"time"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
"fmt"
"errors"
)
type MemberGroupMembers struct {
GroupMemberId int `orm:"column(group_member_id);pk;auto;unique;" json:"group_member_id"`
MemberId int `orm:"column(member_id);index" json:"member_id"`
GroupId int `orm:"column(group_id);index" json:"group_id"`
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
CreateAt int `orm:"type(int);column(create_at)" json:"create_at"`
}
type MemberGroupMemberResult struct {
GroupMemberId int
MemberId int
Account string
RealName string
Avatar string
GroupId int
CreateTime time.Time
CreateAt int
}
// TableName 获取对应数据库表名.
func (m *MemberGroupMembers) TableName() string {
return "member_group_members"
}
// TableEngine 获取数据使用的引擎.
func (m *MemberGroupMembers) TableEngine() string {
return "INNODB"
}
func (m *MemberGroupMembers) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
// 多字段唯一键
func (m *MemberGroupMembers) TableUnique() [][]string {
return [][]string{
{"member_id", "group_id"},
}
}
func NewMemberGroupMembers() *MemberGroupMembers {
return &MemberGroupMembers{}
}
// 查询用户组成员
func (m *MemberGroupMembers) FindByGroupId(groupId int) ([]*MemberGroupMemberResult,error) {
o := orm.NewOrm()
var groupMembers []*MemberGroupMemberResult
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).All(&groupMembers);
if err != nil {
beego.Error("获取用户组成员出错 =>",err)
return nil,err
}
ids := make([]int,0)
for _,member := range groupMembers {
ids = append(ids,member.MemberId)
}
var members []*Member
_,err = o.QueryTable(NewMember().TableNameWithPrefix()).Filter("member_id__in",ids).All(&members)
if err != nil {
beego.Error("获取用户组成员出错 =>",err)
return nil,err
}
for _,member := range members {
for _,groupMember := range groupMembers {
if groupMember.MemberId == member.MemberId {
groupMember.Account = member.Account
groupMember.Avatar = member.Avatar
groupMember.RealName = member.RealName
}
}
}
return groupMembers,nil
}
//添加或更新用户组成员
func (m *MemberGroupMembers) InsertOrUpdate(cols ...string) error {
o := orm.NewOrm()
if m.GroupMemberId > 0 {
_,err := o.Update(m,cols...)
if err != nil {
beego.Error("更新用户组成员失败 =>",err)
}
return err
}else{
if m.GroupId <= 0 {
return errors.New("用户组不能为空")
}
_,err := o.Insert(m)
if err != nil {
beego.Error("添加用户组成员失败 =>",err)
}else{
o.Raw(fmt.Sprintf("UPDATE %s SET group_number=group_number+1 WHERE group_id=%d",NewMemberGroup().TableNameWithPrefix(), m.GroupId)).Exec()
}
return err
}
}
//删除用户组成员
func (m *MemberGroupMembers) Delete(id int) error {
o := orm.NewOrm()
i,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_member_id",id).Delete()
if err != nil {
beego.Error("删除用户组成员失败 =>",err)
return err
}
if i <= 0 {
beego.Info("删除用户组成员返回行数 =>",i)
}
return nil
}
//分页获取用户组成员
func (m *MemberGroupMembers) FindToPager(pageIndex, pageSize, groupId int) ([]*MemberGroupMemberResult,int,error) {
o := orm.NewOrm()
if pageIndex <= 0 {
pageIndex = 1
}
offset := (pageIndex - 1) * pageSize
var memberGroupMembers []*MemberGroupMembers
totalCount := 0
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).Offset(offset).Limit(pageSize).All(&memberGroupMembers)
memberGroupMemberList := make([]*MemberGroupMemberResult,0)
if err != nil {
beego.Error("分页查询用户组成员失败 =>",err)
}else{
i,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).Count()
if err != nil {
beego.Error("分页查询用户组成员失败 =>",err)
}else {
totalCount = int(i)
}
for _,member := range memberGroupMembers {
memberGroupMemberList = append(memberGroupMemberList,member.ToMemberRelationshipResult())
}
}
return memberGroupMemberList,totalCount,err
}
//将用户组信息转换为完整的用户信息
func (m *MemberGroupMembers) ToMemberRelationshipResult() *MemberGroupMemberResult {
memberGroupMemberResult := &MemberGroupMemberResult{}
memberGroupMemberResult.GroupId = m.GroupId
memberGroupMemberResult.GroupMemberId = m.GroupMemberId
memberGroupMemberResult.MemberId = m.MemberId
memberGroupMemberResult.CreateAt = m.CreateAt
memberGroupMemberResult.CreateTime = m.CreateTime
if m.MemberId > 0 {
o := orm.NewOrm()
member := NewMember()
_,err := o.QueryTable(member.TableNameWithPrefix()).Filter("member_id", m.MemberId).All(&member)
if err != nil {
beego.Error("查询用户组成员信息时出错 =>",err)
}else{
memberGroupMemberResult.RealName = member.RealName
memberGroupMemberResult.Avatar = member.Avatar
memberGroupMemberResult.Account = member.Account
}
}
return memberGroupMemberResult
}

View File

@ -1,116 +0,0 @@
package models
import (
"github.com/lifei6671/mindoc/conf"
"time"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego"
)
type MemberGroupMembers struct {
GroupMemberId int `orm:"column(group_member_id);pk;auto;unique;" json:"group_member_id"`
MemberId int `orm:"column(member_id);index" json:"member_id"`
GroupId int `orm:"column(group_id);index" json:"group_id"`
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
CreateAt int `orm:"type(int);column(create_at)" json:"create_at"`
}
type MemberGroupMemberResult struct {
GroupMemberId int
MemberId int
Account string
RealName string
Avatar string
GroupId int
CreateTime time.Time
CreateAt int
}
// TableName 获取对应数据库表名.
func (m *MemberGroupMembers) TableName() string {
return "member_group_members"
}
// TableEngine 获取数据使用的引擎.
func (m *MemberGroupMembers) TableEngine() string {
return "INNODB"
}
func (m *MemberGroupMembers) TableNameWithPrefix() string {
return conf.GetDatabasePrefix() + m.TableName()
}
// 多字段唯一键
func (u *MemberGroupMembers) TableUnique() [][]string {
return [][]string{
{"member_id", "group_id"},
}
}
func NewMemberGroupMembers() *MemberGroupMembers {
return &MemberGroupMembers{}
}
// 查询用户组成员
func (m *MemberGroupMembers) FindByGroupId(groupId int) ([]*MemberGroupMemberResult,error) {
o := orm.NewOrm()
var groupMembers []*MemberGroupMemberResult
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",groupId).All(&groupMembers);
if err != nil {
beego.Error("获取用户组成员出错 =>",err)
return nil,err
}
ids := make([]int,0)
for _,member := range groupMembers {
ids = append(ids,member.MemberId)
}
var members []*Member
_,err = o.QueryTable(NewMember().TableNameWithPrefix()).Filter("member_id__in",ids).All(&members)
if err != nil {
beego.Error("获取用户组成员出错 =>",err)
return nil,err
}
for _,member := range members {
for _,groupMember := range groupMembers {
if groupMember.MemberId == member.MemberId {
groupMember.Account = member.Account
groupMember.Avatar = member.Avatar
groupMember.RealName = member.RealName
}
}
}
return groupMembers,nil
}

View File

@ -48,8 +48,44 @@ func (m *MemberGroup) FindFirst(id int) (*MemberGroup,error){
return m,nil return m,nil
} }
//删除指定用户组
func (m *MemberGroup) Delete(id int) error {
o := orm.NewOrm()
_,err := o.QueryTable(m.TableNameWithPrefix()).Filter("group_id",id).Delete()
if err != nil {
beego.Error("删除用户组失败 =>",err)
}
return err
}
//分页查询用户组
func (m *MemberGroup) FindByPager(pageIndex, pageSize int) ([]*MemberGroup,int,error){
o := orm.NewOrm()
if pageIndex <= 0 {
pageIndex = 1
}
offset := (pageIndex - 1) * pageSize
var memberGroups []*MemberGroup
totalCount := 0
_,err := o.QueryTable(m.TableNameWithPrefix()).Offset(offset).Limit(pageSize).All(&memberGroups)
if err != nil {
beego.Error("分页查询用户组失败 =>",err)
}else{
i,err := o.QueryTable(m.TableNameWithPrefix()).Count()
if err != nil {
beego.Error("分页查询用户组失败 =>",err)
}else {
totalCount = int(i)
}
}
return memberGroups,totalCount,err
}

View File

@ -0,0 +1,348 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>用户组管理 - Powered by MinDoc</title>
<!-- Bootstrap -->
<link href="{{cdncss "/static/bootstrap/css/bootstrap.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/font-awesome/css/font-awesome.min.css"}}" rel="stylesheet">
<link href="{{cdncss "/static/css/main.css"}}" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="/static/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="/static/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<style type="text/css">
.table>tbody>tr>td{vertical-align: middle;}
</style>
</head>
<body>
<div class="manual-reader">
{{template "widgets/header.tpl" .}}
<div class="container manual-body">
<div class="row">
<div class="page-left">
<ul class="menu">
<li><a href="{{urlfor "ManagerController.Index"}}" class="item"><i class="fa fa-dashboard" aria-hidden="true"></i> 仪表盘</a> </li>
<li class="active"><a href="{{urlfor "ManagerController.Users" }}" class="item"><i class="fa fa-users" aria-hidden="true"></i> 用户管理</a> </li>
<li><a href="{{urlfor "ManagerController.Books" }}" class="item"><i class="fa fa-book" aria-hidden="true"></i> 项目管理</a> </li>
{{/*<li><a href="{{urlfor "ManagerController.Comments" }}" class="item"><i class="fa fa-comments-o" aria-hidden="true"></i> 评论管理</a> </li>*/}}
<li><a href="{{urlfor "ManagerController.Setting" }}" class="item"><i class="fa fa-cogs" aria-hidden="true"></i> 配置管理</a> </li>
<li><a href="{{urlfor "ManagerController.AttachList" }}" class="item"><i class="fa fa-cloud-upload" aria-hidden="true"></i> 附件管理</a> </li>
<li><a href="{{urlfor "ManagerController.LabelList" }}" class="item"><i class="fa fa-bookmark" aria-hidden="true"></i> 标签管理</a> </li>
</ul>
</div>
<div class="page-right">
<div class="m-box">
<div class="box-head">
<strong class="box-title"> 成员管理</strong>
{{if eq .Member.Role 0}}
<button type="button" class="btn btn-success btn-sm pull-right" data-toggle="modal" data-target="#addMemberDialogModal"><i class="fa fa-user-plus" aria-hidden="true"></i> 添加成员</button>
{{end}}
</div>
</div>
<div class="box-body">
<div class="users-list" id="userList">
<template v-if="lists.length <= 0">
<div class="text-center">暂无数据</div>
</template>
<template v-else>
<table class="table">
<thead>
<tr>
<th width="80">ID</th>
<th width="80">头像</th>
<th>账号</th>
<th>姓名</th>
<th>角色</th>
<th>类型</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in lists">
<td>${item.member_id}</td>
<td><img :src="item.avatar" onerror="this.src='{{cdnimg "/static/images/middle.gif"}}'" class="img-circle" width="34" height="34"></td>
<td>${item.account}</td>
<td>${item.real_name}</td>
<td>
<template v-if="item.role == 0">
超级管理员
</template>
<template v-else-if="item.member_id == {{.Member.MemberId}}">
${item.role_name}
</template>
<template v-else>
<div class="btn-group">
<button type="button" class="btn btn-default btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
${item.role_name}
<span class="caret"></span></button>
<ul class="dropdown-menu">
<li><a href="javascript:;" @click="setMemberRole(item.member_id,1)">管理员</a> </li>
<li><a href="javascript:;" @click="setMemberRole(item.member_id,2)">普通用户</a> </li>
</ul>
</div>
</template>
</td>
<td>
${item.auth_method}
</td>
<td>
<template v-if="item.status == 0">
<span class="label label-success">正常</span>
</template>
<template v-else>
<span class="label label-danger">禁用</span>
</template>
</td>
<td>
<template v-if="item.member_id == {{.Member.MemberId}}">
</template>
<template v-else-if="item.role != 0">
<a :href="'{{urlfor "ManagerController.EditMember" ":id" ""}}' + item.member_id" class="btn btn-sm btn-default" @click="editMember(item.member_id)">
编辑
</a>
<template v-if="item.status == 0">
<button type="button" class="btn btn-danger btn-sm" @click="setMemberStatus(item.member_id,1,$event)" data-loading-text="启用中...">禁用</button>
</template>
<template v-else>
<button type="button" class="btn btn-success btn-sm" @click="setMemberStatus(item.member_id,0,$event)" data-loading-text="禁用中...">启用</button>
</template>
<button type="button" class="btn btn-danger btn-sm" @click="deleteMember(item.member_id,$event)" data-loading-text="删除中">删除</button>
</template>
</td>
</tr>
</tbody>
</table>
</template>
<nav class="pagination-container">
{{.PageHtml}}
</nav>
</div>
</div>
</div>
</div>
</div>
{{template "widgets/footer.tpl" .}}
</div>
<!-- Modal -->
<div class="modal fade" id="addMemberDialogModal" tabindex="-1" role="dialog" aria-labelledby="addMemberDialogModalLabel">
<div class="modal-dialog" role="document">
<form method="post" autocomplete="off" class="form-horizontal" action="{{urlfor "ManagerController.CreateMember"}}" id="addMemberDialogForm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">创建用户</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label class="col-sm-2 control-label" for="account">账号<span class="error-message">*</span></label>
<div class="col-sm-10">
<input type="text" name="account" class="form-control" placeholder="用户账号" id="account" maxlength="50">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="password1">密码<span class="error-message">*</span></label>
<div class="col-sm-10">
<input type="password" class="form-control" placeholder="用户密码" name="password1" id="password1" maxlength="50">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="password2">确认密码<span class="error-message">*</span></label>
<div class="col-sm-10">
<input type="password" class="form-control" placeholder="确认密码" name="password2" id="password2" maxlength="50">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="email">邮箱<span class="error-message">*</span></label>
<div class="col-sm-10">
<input type="email" class="form-control" placeholder="邮箱" name="email" id="email" maxlength="50">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">真实姓名</label>
<div class="col-sm-10">
<input type="text" name="real_name" class="form-control" value="" placeholder="真实姓名">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">手机号</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="手机号" name="phone" maxlength="50">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">角色</label>
<div class="col-sm-10">
<select name="role" class="form-control">
<option value="1">管理员</option>
<option value="2">普通用户</option>
</select>
</div>
</div>
<div class="form-group">
</div>
<div class="clearfix"></div>
</div>
<div class="modal-footer">
<span id="form-error-message"></span>
<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
<button type="submit" class="btn btn-success" data-loading-text="保存中..." id="btnAddMember">保存</button>
</div>
</div>
</form>
</div>
</div><!--END Modal-->
<script src="{{cdnjs "/static/jquery/1.12.4/jquery.min.js"}}"></script>
<script src="{{cdnjs "/static/bootstrap/js/bootstrap.min.js"}}"></script>
<script src="{{cdnjs "/static/vuejs/vue.min.js"}}"></script>
<script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
<script src="{{cdnjs "/static/js/main.js"}}" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#addMemberDialogModal").on("show.bs.modal",function () {
window.addMemberDialogModalHtml = $(this).find("form").html();
}).on("hidden.bs.modal",function () {
$(this).find("form").html(window.addMemberDialogModalHtml);
});
$("#addMemberDialogForm").ajaxForm({
beforeSubmit : function () {
var account = $.trim($("#account").val());
if(account === ""){
return showError("账号不能为空");
}
var password1 = $.trim($("#password1").val());
var password2 = $("#password2").val();
if (password1 === "") {
return showError("密码不能为空");
}
if (password1 !== password2) {
return showError("确认密码不正确");
}
var email = $.trim($("#email").val());
if (email === "") {
return showError("邮箱不能为空");
}
$("#btnAddMember").button("loading");
return true;
},
success : function (res) {
if(res.errcode === 0){
app.lists.splice(0,0,res.data);
$("#addMemberDialogModal").modal("hide");
}else{
showError(res.message);
}
$("#btnAddMember").button("reset");
},
error : function () {
showError("服务器异常");
$("#btnAddMember").button("reset");
}
});
var app = new Vue({
el : "#userList",
data : {
lists : {{.Result}}
},
delimiters : ['${','}'],
methods : {
setMemberStatus : function (id,status,e) {
var $this = this;
$.ajax({
url : "{{urlfor "ManagerController.UpdateMemberStatus"}}",
type : "post",
data : { "member_id":id,"status" : status},
dataType : "json",
success : function (res) {
if (res.errcode === 0) {
for (var index in $this.lists) {
var item = $this.lists[index];
if (item.member_id === id) {
console.log(item);
$this.lists[index].status = status;
break;
//$this.lists.splice(index,1,item);
}
}
} else {
alert("操作失败:" + res.message);
}
}
})
},
setMemberRole : function (member_id, role) {
var $this = this;
$.ajax({
url :"{{urlfor "ManagerController.ChangeMemberRole"}}",
dataType :"json",
type :"post",
data : { "member_id" : member_id,"role" : role },
success : function (res) {
if(res.errcode === 0){
for (var index in $this.lists) {
var item = $this.lists[index];
if (item.member_id === member_id) {
$this.lists.splice(index,1,res.data);
break;
}
}
}else{
alert("操作失败:" + res.message);
}
}
})
},
deleteMember : function (id, e) {
var $this = this;
$.ajax({
url : "{{urlfor "ManagerController.DeleteMember"}}",
type : "post",
data : { "id":id },
dataType : "json",
success : function (res) {
if (res.errcode === 0) {
for (var index in $this.lists) {
var item = $this.lists[index];
if (item.member_id == id) {
console.log(item);
$this.lists.splice(index,1);
break;
}
}
} else {
alert("操作失败:" + res.message);
}
}
});
}
}
});
Vue.nextTick(function () {
$("[data-toggle='tooltip']").tooltip();
});
});
</script>
</body>
</html>