func Role(r render.Render) { l, err := dao.GetAllRole() if err != nil { service.Logs.Error("dao.GetAllRole() err(%v)", err) } data := make(map[string]interface{}) data["l"] = l r.HTML(200, "role", data) return }
//从数据库读入缓存 func Reload() (err error) { //reload user userList, err := dao.GetAllUser() if err != nil { Logs.Error("dao.GetAllUser err (%v)", err) return } for _, v := range userList { cache.Users[v.Id] = v } //reload permission pList, err := dao.GetAllPermission() if err != nil { Logs.Error("dao.GetAllPermission err (%v)", err) return } for _, v := range pList { cache.Permissions[v.Id] = v } //reload role rList, err := dao.GetAllRole() if err != nil { Logs.Error("dao.GetAllRole err (%v)", err) return } for _, v := range rList { cache.Roles[v.Id] = v } //reload userRole urList, err := dao.GetAllUserRole() if err != nil { Logs.Error("dao.GetAllUserRole err (%v)", err) return } for _, v := range urList { cache.UserRoleCache.Add(v.UserId, v.RoleId) } //reload rolePermission rpList, err := dao.GetAllRolePermission() if err != nil { Logs.Error("dao.GetAllRolePermission err (%v)", err) return } for _, v := range rpList { cache.RolePermissionCache.Add(v.RoleId, v.PermissionId) } return }
//后台添加用户 func UserAdd(r render.Render, req *http.Request) { var err error data := make(map[string]interface{}) if req.Method == "GET" { rs, err := dao.GetAllRole() if err != nil { service.Logs.Error("dao.GetAllUser() err(%v)", err) return } data["roleList"] = rs r.HTML(200, "user_add", data) return } req.ParseForm() values := req.Form m := &model.User{} m.Account = values.Get("account") m.Password = values.Get("password") m.Info = values.Get("info") m.Name = values.Get("name") if !checkNull([]string{m.Account, m.Password, m.Info, m.Name}...) { service.Logs.Error("args err") return } status, _ := strconv.Atoi(values.Get("status")) m.Status = status roleId, _ := strconv.Atoi(values.Get("role_id")) //添加用户 userId, err := dao.AddUser(m) if err != nil { service.Logs.Error("dao.InsertUser err(%v)", err) return } //添加用户角色 ur := &model.UserRole{} ur.RoleId = int64(roleId) ur.UserId = userId _, err = dao.AddUserRole(ur) if err != nil { service.Logs.Error("dao.AddUserRole err(%v)", err) return } r.Redirect("/", 302) return }
func Index(r render.Render) { users, err := dao.GetAllUser() if err != nil { service.Logs.Error("dao.GetAllUser() err(%v)", err) return } data := make(map[string]interface{}) rs, err := dao.GetAllRole() if err != nil { service.Logs.Error("dao.GetAllUser() err(%v)", err) return } data["users"] = users data["r"] = rs r.HTML(200, "admin", data) return }
//管理后台修改用户 func UserEdit(r render.Render, req *http.Request) { req.ParseForm() values := req.Form id := values.Get("id") var idInt64 int64 var err error m := &model.User{} data := make(map[string]interface{}) l, err := dao.GetAllRole() if err != nil { service.Logs.Error("dao.GetAllRole() err(%v)", err) return } data["l"] = l if id == "" { service.Logs.Error("id==null)") return } idInt64, err = strconv.ParseInt(id, 10, 64) if err != nil { service.Logs.Error("strconv.ParseInt err(%v)", err) return } rs, err := dao.GetAllRole() if err != nil { service.Logs.Error("dao.GetAllUser() err(%v)", err) return } data["roleList"] = rs if req.Method == "GET" { m, err = dao.GetUserById(idInt64) if err != nil { service.Logs.Error("dao.GetRoleById err(%v)", err) return } data["m"] = m r.HTML(200, "user_edit", data) return } m.Account = values.Get("account") m.Info = values.Get("info") m.Name = values.Get("name") m.Id = idInt64 status, _ := strconv.Atoi(values.Get("status")) m.Status = status roleId := values.Get("role_id") roleIdInt, _ := strconv.Atoi(roleId) //更新用户角色 err = service.EditUserRole(m.Id, int64(roleIdInt)) if err != nil { service.Logs.Error("service.EditUserRole err(%v)", err) return } //更新用户 err = dao.UpdateUserById(m) if err != nil { service.Logs.Error("dao.UpdateUserById err(%v)", err) return } data["m"] = m r.HTML(200, "user_edit", data) return }