123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /**********************************************
- ** @Des: This file ...
- ** @Author: haodaquan
- ** @Date: 2017-09-14 14:23:32
- ** @Last Modified by: haodaquan
- ** @Last Modified time: 2017-09-17 11:31:13
- ***********************************************/
- package backgroundc
- import (
- "fmt"
- "strconv"
- "strings"
- "time"
- "github.com/astaxie/beego"
- "wuyebaoxiuapi/models/backgroundm"
- )
- type RoleController struct {
- BaseController
- }
- func (self *RoleController) List() {
- self.Data["pageTitle"] = "角色管理"
- self.display()
- }
- func (self *RoleController) Add() {
- self.Data["zTree"] = true //引入ztreecss
- self.Data["pageTitle"] = "新增角色"
- self.display()
- }
- func (self *RoleController) Edit() {
- self.Data["zTree"] = true //引入ztreecss
- self.Data["pageTitle"] = "编辑角色"
- id, _ := self.GetInt("id", 0)
- role, _ := backgroundm.RoleGetById(id)
- row := make(map[string]interface{})
- row["id"] = role.Id
- row["role_name"] = role.RoleName
- row["detail"] = role.Detail
- self.Data["role"] = row
- //获取选择的树节点
- roleAuth, _ := backgroundm.RoleAuthGetById(id)
- authId := make([]int, 0)
- for _, v := range roleAuth {
- authId = append(authId, v.AuthId)
- }
- self.Data["auth"] = authId
- fmt.Println(authId)
- self.display()
- }
- func (self *RoleController) AjaxSave() {
- role := new(backgroundm.Role)
- role.RoleName = strings.TrimSpace(self.GetString("role_name"))
- role.Detail = strings.TrimSpace(self.GetString("detail"))
- role.CreateTime = time.Now().Unix()
- role.UpdateTime = time.Now().Unix()
- role.Status = 1
- auths := strings.TrimSpace(self.GetString("nodes_data"))
- role_id, _ := self.GetInt("id")
- if role_id == 0 {
- //新增
- role.CreateTime = time.Now().Unix()
- role.UpdateTime = time.Now().Unix()
- role.CreateId = self.userId
- role.UpdateId = self.userId
- if id, err := backgroundm.RoleAdd(role); err != nil {
- self.ajaxMsg(err.Error(), MSG_ERR)
- } else {
- ra := new(backgroundm.RoleAuth)
- authsSlice := strings.Split(auths, ",")
- for _, v := range authsSlice {
- aid, _ := strconv.Atoi(v)
- ra.AuthId = aid
- ra.RoleId = id
- backgroundm.RoleAuthAdd(ra)
- }
- }
- self.ajaxMsg("", MSG_OK)
- }
- //修改
- role.Id = role_id
- role.UpdateTime = time.Now().Unix()
- role.UpdateId = self.userId
- if err := role.Update(); err != nil {
- self.ajaxMsg(err.Error(), MSG_ERR)
- } else {
- // 删除该角色权限
- backgroundm.RoleAuthDelete(role_id)
- ra := new(backgroundm.RoleAuth)
- authsSlice := strings.Split(auths, ",")
- for _, v := range authsSlice {
- aid, _ := strconv.Atoi(v)
- ra.AuthId = aid
- ra.RoleId = int64(role_id)
- backgroundm.RoleAuthAdd(ra)
- }
- }
- self.ajaxMsg("", MSG_OK)
- }
- func (self *RoleController) AjaxDel() {
- role_id, _ := self.GetInt("id")
- role, _ := backgroundm.RoleGetById(role_id)
- role.Status = 0
- role.Id = role_id
- role.UpdateTime = time.Now().Unix()
- if err := role.Update(); err != nil {
- self.ajaxMsg(err.Error(), MSG_ERR)
- }
- // 删除该角色权限
- //models.RoleAuthDelete(role_id)
- self.ajaxMsg("", MSG_OK)
- }
- func (self *RoleController) Table() {
- //列表
- page, err := self.GetInt("page")
- if err != nil {
- page = 1
- }
- limit, err := self.GetInt("limit")
- if err != nil {
- limit = 30
- }
- roleName := strings.TrimSpace(self.GetString("roleName"))
- self.pageSize = limit
- //查询条件
- filters := make([]interface{}, 0)
- filters = append(filters, "status", 1)
- if roleName != "" {
- filters = append(filters, "role_name__icontains", roleName)
- }
- result, count := backgroundm.RoleGetList(page, self.pageSize, filters...)
- list := make([]map[string]interface{}, len(result))
- for k, v := range result {
- row := make(map[string]interface{})
- row["id"] = v.Id
- row["role_name"] = v.RoleName
- row["detail"] = v.Detail
- row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i:s")
- row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i:s")
- list[k] = row
- }
- self.ajaxList("成功", MSG_OK, count, list)
- }
|