package controllers /* 核心控制器 所有api均通过该控制器进行业务分发 请求路由由router.go自动注册,通过在控制器中的方法注释注册为路由 */ import ( "bufio" "crypto/md5" "encoding/hex" "encoding/json" "fmt" "log" "net" "os" "path/filepath" "regexp" "runtime" "scd_check_tools/conf" "scd_check_tools/global" "scd_check_tools/logger" "scd_check_tools/models/bo" "scd_check_tools/models/enum" "scd_check_tools/mqtt" "scd_check_tools/tools" "strconv" "strings" "time" "github.com/astaxie/beego/orm" //"github.com/shopspring/decimal" "github.com/patrickmn/go-cache" ) //基础功能服务 type ApiController struct { BaseController } func (this *ApiController) Prepare() { fmt.Println(this.Data["RouterPattern"].(string)) } func init() { global.GoCahce = cache.New(5*time.Minute, 60*time.Second) //GlobalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":300}`) //go GlobalSessions.GC() } // @Title 语音播放 // @Summary 语音播放 // @Description 语音播放 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param filename query string false "文件名称" // @Success 200 status 成功 // @Failure 500 status 失败 // @router /playWav [POST] func (c *ApiController) PlayWav() { filename := c.GetString("filename", "") buf := make([]byte, 4096) n, _ := c.Ctx.Request.Body.Read(buf) if filename == "" && n > 0 { jsondata := map[string]string{} json.Unmarshal(buf[0:n], &jsondata) filename = jsondata["filename"] } if filename != "" { dir, _ := filepath.Abs(filepath.Dir(os.Args[0])) fmt.Println(dir) } c.Data["json"] = map[string]interface{}{"result": "1", "success": true} c.ServeJSON() } // @Summary 视频播放 // @router /net/video/play [post] func (c *ApiController) PlayNetVideo() { filename := c.GetString("url", "") title := c.GetString("title") clientname := c.GetString("clientname") videoinfo := map[string]string{"url": filename, "title": title} msg, _ := json.Marshal(videoinfo) if clientname == "" { ip := c.Ctx.Request.Header.Get("X-Real-IP") if net.ParseIP(ip) == nil { ips := c.Ctx.Request.Header.Get("X-Forward-For") for _, i := range strings.Split(ips, ",") { if net.ParseIP(i) != nil { ip = i break } } if ip == "" { ip, _, _ = net.SplitHostPort(c.Ctx.Request.RemoteAddr) } } clientname = ip } mqtt.PublishMessage("/jujutong/police/videoplayer/"+clientname, string(msg)) c.Data["json"] = c.WarpOK("") c.ServeJSON() } // @Summary 获取当前程序版本 // @NotAuth 不需要授权可访问 // @Description 获取当前程序版本 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /getVersion [get] func (c *ApiController) GetVersion() { exename := conf.GlobalConfig["appid"] if string(runtime.GOOS) == "windows" { exename = exename + ".exe" } f, err1 := os.Open(exename) if err1 != nil { fmt.Println(tools.NowTime()+":", err1.Error()) c.Data["json"] = c.WarpError("获取版本号失败") c.ServeJSON() return } fi, err2 := f.Stat() if err2 != nil { fmt.Println(tools.NowTime()+":", err2.Error()) c.Data["json"] = c.WarpError("获取版本号失败") c.ServeJSON() return } data := fi.ModTime().Format("20060102-15") c.Data["json"] = c.WarpOK(data) c.ServeJSON() } func (c *ApiController) CheckSeesion() bool { userid := c.Ctx.Input.Session("userid") r := c.GetSession(userid) if r == nil || r == "" { return false } return true } func (c *ApiController) GetUserToken() string { Authorization := c.Ctx.Request.Header.Get("Authorization") if Authorization == "" { return "" } return strings.TrimLeft(Authorization, "Bearer ") } // @Summary 正则表达式匹配验证 // @NotAuth 不需要授权可访问 // @Description 正则表达式匹配验证 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Param regstr query string true "正则表达式" // @Param str query string true "用于匹配的目标字符串" // @Produce json // @Success 200 status 成功 // @Failure 500 status 失败 // @router /reg/test [get] func (c *ApiController) RegTest() { r, err := tools.RexGroupTestMatch(c.GetString("regstr"), c.GetString("str")) if err != nil { c.Data["json"] = c.ResultError(err) } else { if !r { c.Data["json"] = c.ResultError("验证未通过,请检查正则表达式是否正确") } else { c.Data["json"] = c.ResultOK(r, 1) } } c.ServeJSON() } // @Summary 客户端心跳 // @NotAuth 不需要授权可访问 // @Description 客户端心跳保持,由浏览器或客户端发起请求,以表示其还处于使用中 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Success 200 status 成功 // @Failure 500 status 失败 // @router /keep-alive [get] func (c *ApiController) KeepAlive() { loginip := c.GetClientIP() fmt.Println("keep-alive IP:" + loginip) if loginip != "" { curtoken := c.GetUserToken() if curtoken == "" { global.GoCahce.Delete(loginip) } else { global.GoCahce.Set(loginip, "", 62*time.Second) } } c.Data["json"] = "ok" c.ServeJSON() } // @Summary 登出 // @NotAuth 不需要授权可访问 // @Description 登出系统。系统会自动清理当前用户的相关信息,如缓存、token等 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param sessionid query string true "用户token码" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /logout [post] func (c *ApiController) Logout() { loginip := c.GetClientIP() sessionid := c.GetString("sessionid", "") bo.RemoveSession(sessionid) //清除缓存的该token的ip地址数据 global.GoCahce.Delete(sessionid + "-ip") if loginip != "" { global.GoCahce.Delete(loginip) } c.Data["json"] = "ok" c.ServeJSON() } // @Summary 登录 // @Description 登录系统。系统会返回当前用户的相关信息,如基本信息、token等 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param login_account query string true "用户帐号" // @Param pwd query string true "用户密码" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /login [post] func (c *ApiController) Login() { username := c.GetString("login_account", "") password := c.GetString("pwd", "") loginip := c.GetClientIP() userInfo := map[string]interface{}{} userInfo["ip"] = loginip userInfo["name"] = username if loginip != "" { if global.AllowAccessIps != "*" { //判断当前客户端是否在允许范围内 ips := strings.Split(global.AllowAccessIps, ",") allowLogin := false for _, iplimt := range ips { if iplimt == loginip { allowLogin = true break } tmppos := strings.Index(iplimt, ".*") if tmppos > 1 { //ip段 if len(loginip) < tmppos { continue } if loginip[:tmppos] == iplimt[:tmppos] { allowLogin = true break } } } if !allowLogin { new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在未授权终端(IP:%s)尝试访问系统。被拦截!", username, loginip), userInfo) c.Ctx.Redirect(430, "/static/430.html") return } } //判断当前客户端是否已存在登录用户 _, ok := global.GoCahce.Get(loginip) if ok && global.UserLoginClientLimt == "o2o" { new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:帐号%s在多个终端登录", username), userInfo) c.Data["json"] = c.WarpError("系统限制:同时只能登录一个用户帐号!") c.ServeJSON() return } } if password == "" { c.Data["json"] = c.WarpError("密码不能为空") new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:帐号%s尝试使用空密码登录!", username), userInfo) } else if username == "" { c.Data["json"] = c.WarpError("帐号不能为空") new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("登录失败:用户尝试使用空帐号登录!"), userInfo) } else { logininfo2 := map[string]interface{}{} if info, ok := bo.LoginInfo.Load(username); ok { logininfo2 = info.(map[string]interface{}) count := logininfo2["count"].(int) lasttime := logininfo2["lasttime"].(int64) timeout := global.LoginFailLockTime if count >= 6 { timeout = timeout + 60 } else if count >= 7 { timeout = timeout + 1800 } else if count >= 8 { timeout = timeout + 3600 } if count >= global.LoginFailMaxCount { subtime := lasttime + int64(timeout) - time.Now().Unix() if subtime > 0 { if subtime > 60 { min := subtime / 60 sen := subtime % 60 c.Data["json"] = c.WarpError(fmt.Sprintf("连续失败超过%d次,帐号已被锁定,请%d分%d秒后重试", global.LoginFailMaxCount, min, sen)) } else { c.Data["json"] = c.WarpError(fmt.Sprintf("连续失败超过%d次,帐号已被锁定,请%d秒后重试", global.LoginFailMaxCount, subtime)) } userInfo := map[string]interface{}{} userInfo["name"] = username userInfo["ip"] = c.GetClientIP() new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s连续失败超过%d次,帐号已被锁定", username, global.LoginFailMaxCount), userInfo) c.ServeJSON() return } } } password = tools.OriginalCode(password) user := make(map[string]interface{}) userInfo, err := bo.UserLogin(username, password) if err != nil { logger.Logger.Debug(password) logger.Logger.Error(err, "登录失败:"+err.Error()) c.Data["json"] = c.WarpError(err.Error()) userInfo = map[string]interface{}{} userInfo["name"] = username userInfo["ip"] = loginip new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, "登录失败:帐号"+username+"登录失败:"+err.Error(), userInfo) //bo.SaveSyslog("用户帐号或密码不正确", "系统登录", false, username) } else { bindip := tools.IsEmpty(userInfo["bind_ips"]) userInfo["ip"] = loginip if bindip != "" { //限制登录IP allowLogin := false ips := strings.Split(bindip, ",") for _, ipitem := range ips { if ipitem == loginip { allowLogin = true break } tmppos := strings.Index(ipitem, ".*") if tmppos > 1 { //ip段 if len(loginip) < tmppos { continue } if loginip[:tmppos] == ipitem[:tmppos] { allowLogin = true break } } } if !allowLogin { new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("未授权终端(IP:%s)尝试访问系统被拦截", loginip), userInfo) c.Ctx.Redirect(430, "/static/430.html") return } } //判断是否有日期限制 datelimit_start := tools.IsEmpty(userInfo["datelimit_start"]) if datelimit_start != "" { nowDay := time.Now().Format("2006-01-02") if nowDay < datelimit_start { new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在当前日期内禁止访问系统", username), userInfo) c.Ctx.Redirect(431, "/static/431.html") return } datelimit_end := tools.IsEmpty(userInfo["datelimit_end"]) if datelimit_end != "" { if nowDay > datelimit_end { new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s在当前日期内禁止访问系统", username), userInfo) c.Ctx.Redirect(431, "/static/431.html") return } } } user["userid"] = userInfo["userid"].(string) //生成sessionid h := md5.New() h.Write([]byte(tools.NowTime())) cipherStr := h.Sum(nil) sessionid := hex.EncodeToString(cipherStr) // 输出加密结果 err = bo.SetSession(sessionid, userInfo["userid"].(string)) if err != nil { logger.Logger.Error(err) c.Data["json"] = c.WarpError(err.Error()) new(bo.SystemLog).Fail(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, fmt.Sprintf("帐号%s登录系统失败:%s", username, err.Error()), userInfo) //bo.SaveSyslog(err.Error(), "系统登录", false, username) c.ServeJSON() return } //获取用户权限 useridint, _ := strconv.Atoi(userInfo["userid"].(string)) funcs, err2 := bo.GetUserFunc(useridint) if err2 != nil { tools.Log(err2) } userInfo["funcs"] = funcs //获取系统的帐号密码修改模式 pwd_edit_mod, _ := bo.GetSysParamValue("user_pwd_edit_mod", "admin") userInfo["pwdeditmod"] = pwd_edit_mod user["userinfo"] = userInfo user["sessionid"] = sessionid //c.SetSession("sessionid", sessionid) c.Data["json"] = c.WarpOK(user) thrid_uer_roleid := "" thrid_uer_role, has := global.GoCahce.Get("global_code_pl_role_coderole_third_user") if has && thrid_uer_role != nil { thrid_uer_roleid = tools.IsEmpty(thrid_uer_role.(orm.Params)["id"]) } if tools.IsEmpty(userInfo["role"]) != thrid_uer_roleid { new(bo.SystemLog).Success(enum.AuditType_Login, enum.LogType_Logon, enum.OptEventType_System, enum.OptEventLevel_Hight, "帐号"+username+"登录系统成功", userInfo) //bo.SaveSyslog("登录系统成功", "系统登录", true, userInfo["name"].(string)) } global.GoCahce.Set(loginip, username, 62*time.Second) } } c.ServeJSON() } // @Summary 保存用户信息(已废弃) // @router /saveUser [post] func (c *ApiController) SaveUser() { name := c.GetString("name", "") reg := regexp.MustCompile(`(#|\?|%|,)`) if len(reg.FindAllString(name, -1)) > 0 { c.Data["json"] = c.WarpError("名称不能包含特殊字符:#?%,") c.ServeJSON() return } var err error id, _ := strconv.Atoi(c.GetString("id", "0")) memo := c.GetString("memo", "") account := c.GetString("account", "") pwd := c.GetString("pwd", "") obj := bo.T_data_user{Id: id, Name: name, Memo: memo, Account: account, Pwd: pwd} obj.Createtime = strings.Trim(tools.NowTime(), " ") userid, err2 := strconv.Atoi(c.GetUserToken()) if err2 != nil { c.Ctx.ResponseWriter.WriteHeader(401) //token获取失败或无效时,直接返回401 return } obj.Createuser = userid insertId, err := bo.SaveUser(obj, c.GetCurrentUserInfo()) //tmp, _ := json.Marshal(obj) //fmt.Println(string(tmp)) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(insertId) } c.ServeJSON() } // @Summary 查询指定用户的详细信息 // @Description 查询用户信息 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "用户ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /getUser [get] func (c *ApiController) GetUser() { var id = 0 id, _ = strconv.Atoi(c.GetString("id", "0")) param := tools.ParseFormParam(c.Ctx.Request.Form) obj := bo.T_data_user{Id: id} datalist, err := bo.QueryUserList(param, obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpPageOk(datalist["meta"].(map[string]interface{}), datalist["data"]) } c.ServeJSON() } // @Summary 创建用户 // @Description 查询用户信息 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param userid query int true "用户ID" // @Param role query int true "角色ID" // @Param username query string true "姓名" // @Param account query string true "登录帐号" // @Param password query string true "登录密码" // @Param mobilephone query string false "手机号" // @Param pwd_expire query string false "密码过期日期" // @Param bind_ips query string false "允许登录的IP" // @Param datelimit_start query string false "帐号有效期开始日期" // @Param datelimit_end query string false "帐号有效期结束日期" // @Param memo query string false "其他说明" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /createuser [post] func (c *ApiController) CreateUser() { parameter := map[string]interface{}{} parameter["userid"] = c.GetString("userid", "0") parameter["role"] = c.GetString("role", "") username := c.GetString("username", "") parameter["username"] = username reg := regexp.MustCompile(`(#|\?|%|,',\*,&)`) if len(reg.FindAllString(username, -1)) > 0 { c.Data["json"] = c.WarpError("名称不能包含特殊字符:#?%,'*&") c.ServeJSON() return } parameter["account"] = c.GetString("account", "") parameter["password"] = c.GetString("password", "") parameter["memo"] = c.GetString("memo", "") parameter["mobilephone"] = c.GetString("mobilephone", "") parameter["pwd_expire"] = c.GetString("pwd_expire") //密码过期日期 parameter["bind_ips"] = c.GetString("bind_ips", "") //允许登录的IP parameter["datelimit_start"] = c.GetString("datelimit_start", "") //帐号有效期开始日期 parameter["datelimit_end"] = c.GetString("datelimit_end", "") //帐号有效期结束日期 status, err := bo.CreateUser(parameter, c.GetCurrentUserInfo()) tools.CheckErr(err) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(status) } c.ServeJSON() } //修改密码。只能修改自己的密码 // @Summary 修改自己密码 // @Description 修改密码。只能修改自己的密码 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param newpwd query int true "原密码" // @Param oldpwd query int true "新密码" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /resetUserPwd [post] func (c *ApiController) ResetUserPwd() { newPwd := c.GetString("newpwd") oldpwd := c.GetString("oldpwd") if oldpwd == "" { c.Data["json"] = c.WarpError("原密码不能为空") c.ServeJSON() return } if newPwd == "" { c.Data["json"] = c.WarpError("密码不能为空") } else { editmod, _ := bo.GetSysParamValue("user_pwd_edit_mod", "admin") if editmod == "" || editmod == "admin" { c.Data["json"] = c.WarpError("当前不允许修改密码,请联系管理员!") c.ServeJSON() return } if len(newPwd) > 32 { newPwd = tools.OriginalCode(newPwd) } check, msg := bo.PwdRuleCheck(newPwd) if !check { c.Data["json"] = c.WarpError(msg) c.ServeJSON() return } //原密码 if len(oldpwd) > 32 { oldpwd = tools.OriginalCode(oldpwd) } logger.Logger.Debug(fmt.Sprintf("用户修改密码:新密码:%s \n 原密码:%s", newPwd, oldpwd)) has := md5.Sum([]byte(oldpwd)) oldpwd = fmt.Sprintf("%x", has) userinfo := c.GetCurrentUserInfo() userid := tools.IsEmpty(userinfo["userid"]) obj := bo.T_data_user{Id: 0} obj.Id, _ = strconv.Atoi(userid) userOneInfo := bo.OneUser(obj) if userOneInfo.Pwd != oldpwd { c.Data["json"] = c.WarpError("原密码不正确!") c.ServeJSON() return } obj.Pwd = newPwd _, err := bo.SavePwd(obj, userinfo) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK("") } } c.ServeJSON() } // @Summary 指定用户的功能列表 // @Description 指定用户的功能列表。非RBAC模式下有效 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "用户ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /getUserPrivList [get] func (c *ApiController) GetUserPrivListUser() { var id = 0 id, _ = strconv.Atoi(c.GetString("id", "0")) obj := bo.T_data_user_func{Userid: id} datalist, err := bo.QueryUserPrivList(obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist["data"]) } c.ServeJSON() } // @Summary 保存指定用户的功能列表 // @Description 保存指定用户的功能列表。非RBAC模式下有效 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "用户ID" // @Param privids query string true "功能ID列表,使用逗号分隔" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /saveUserPrivList [post] func (c *ApiController) SaveUserPrivList() { uid, _ := strconv.Atoi(c.GetString("id", "0")) privids := c.GetString("privids") datalist, err := bo.SaveUserPrivList(uid, privids, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() } // @Summary 搜索查询用户 // @Description 查询用户 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param name query int false "姓名" // @Param pageindex query int false "分页页码,默认为1" // @Param pagesize query int false "每页最大记录数,默认为20" // @Param area_id query int false "区域ID" // @Param role_id query int false "角色ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /searchUserInfo [get] func (c *ApiController) SearchUserInfo() { pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 20) name := c.GetString("name") area_id := c.GetString("area_id") role_id := c.GetString("role_id") tableData, number, err := bo.SearchUserInfo(area_id, name, role_id, pageIndex, pageSize, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, number) } c.ServeJSON() } // 获取指定用户所管理的区域列表 // @Summary 获取指定用户所管理的区域列表 // @Description 获取指定用户所管理的区域列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param userid query int true "用户ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getUserAreaList [get] func (c *ApiController) GetUserAreaList() { uid, _ := c.GetInt("userid", 0) if uid == 0 { c.Data["json"] = c.ResultError("无效的用户ID") c.ServeJSON() return } obj := new(bo.UserAreaRelationObject) obj.Model.Userid = uid tableData, err := obj.List() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, len(tableData)) } c.ServeJSON() } //保存用户所管理的区域列表 // @Summary 保存指定用户所管理的区域列表 // @Description 保存指定用户所管理的区域列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param userid query int true "用户ID" // @Param areaid query string true "区域列表。JSON串,格式如:[{"id":"区域ID","allchecked":"all|空"}]" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /saveUserAreaList [post] func (c *ApiController) SaveUserAreaList() { uid, _ := c.GetInt("userid", 0) if uid == 0 { c.Data["json"] = c.ResultError("无效的用户ID") c.ServeJSON() return } areaids := c.GetString("areaid") obj := new(bo.UserAreaRelationObject) obj.SetUserInfo(c.GetCurrentUserInfo()) obj.Model.Userid = uid err := obj.Delete() //清除原来的数据 if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } if areaids != "" { ids := []map[string]string{} json.Unmarshal([]byte(areaids), &ids) for _, id := range ids { obj.Model.Areaid, _ = strconv.Atoi(id["id"]) //如果是该节点是全选的,需要将节点下的所有节点添加到关系表 isall := id["allchecked"] if isall == "all" { err = obj.AppendNodeAll(uid, obj.Model.Areaid) } else { //添加单个节点 err = obj.Save() } if err != nil { c.Data["json"] = c.ResultError(err.Error()) c.ServeJSON() return } } } c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() } // @Summary 获取指定角色的功能点列表 // @Description 获取指定角色的功能点列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param roleid query int true "角色ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /getRoleFuncList [get] func (c *ApiController) GetRoleFuncList() { roleid := c.GetString("roleid", "") if roleid == "" { c.Data["json"] = c.WarpError("角色ID不能为空") c.ServeJSON() return } roleidint, _ := strconv.Atoi(roleid) datalist, err := bo.GetRoleFunc(roleidint) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() } // @Summary 保存指定角色的功能点列表 // @Description 保存指定角色的功能点列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param roleid query int true "角色ID" // @Param areaid query string true "区域列表。JSON串,格式如:[{"id":"区域ID","allchecked":"all|空"}]" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /saveRoleFunc [post] func (c *ApiController) SaveRoleFunc() { roleid := c.GetString("roleid", "") if roleid == "" { c.Data["json"] = c.WarpError("角色ID不能为空") c.ServeJSON() return } funcids := c.GetString("funcids", "") tmpAry := strings.Split(funcids, ",") roleidint, _ := strconv.Atoi(roleid) err := bo.SaveRoleFunc(roleidint, tmpAry, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK("") } c.ServeJSON() } // @Summary 初始化系统数据 // @router /sys/init [post] func (c *ApiController) SysInit() { orgname := c.GetString("orgname") sn := c.GetString("sn") account := c.GetString("account") pwd := c.GetString("pwd") if orgname == "" || sn == "" { c.Data["json"] = c.WarpError("参数不正确") c.ServeJSON() return } pm := bo.Global_sys_param{Param_name: "orgname", Param_value: orgname, Param_memo: "单位"} _, err := bo.SaveSysParam(pm) if err != nil { c.Data["json"] = c.WarpError(err.Error()) c.ServeJSON() return } if account != "" && pwd != "" { //清除原来的非超级管理员帐号 bo.InitUser() bo.ClearAllSyslog() parameter := map[string]interface{}{} parameter["userid"] = "" parameter["role"] = c.GetString("role", "0") parameter["username"] = c.GetString("username", "") parameter["account"] = account parameter["password"] = pwd parameter["memo"] = c.GetString("memo", "") _, err = bo.CreateUser(parameter) if err != nil { c.Data["json"] = c.WarpError(err.Error()) c.ServeJSON() return } } c.Data["json"] = c.WarpOK("") c.ServeJSON() } // @Summary 查询操作日志列表 // @Description 查询操作日志列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int true "当前查询页码。默认为1" // @Param pagesize query int true "每页显示数据条数。默认20" // @Param enddate query string false "查询条件:结束日期" // @Param startdate query string false "查询条件:开始日期" // @Param description query string false "查询条件:日志内容。模糊匹配" // @Param staff query string false "查询条件:操作人姓名。模糊匹配" // @Param ip query string false "查询条件:操作IP地址。模糊匹配" // @Param success query int false "查询条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功" // @Param opttype query int false "查询条件:操作类型。关联系统字典log_opttype。" // @Param audittype query int false "查询条件:审计类型。关联系统字典log_audittype。" // @Param eventtype query int false "查询条件:事件类型。关联系统字典log_eventtype。" // @Param eventlevel query int false "查询条件:事件等级。关联系统字典log_eventlevel。" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getSyslogList [get] func (c *ApiController) GetSysLog() { pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 20) con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") con["description"] = c.GetString("description", "") con["staff"] = c.GetString("staff", "") con["ip"] = c.GetString("ip", "") con["success"] = c.GetString("success", "") con["logtype"] = c.GetString("opttype", "") con["audittype"] = c.GetString("audittype", "") con["eventtype"] = c.GetString("eventtype", "") con["eventlevel"] = c.GetString("eventlevel", "") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.SetUserInfo(userinfo) tableData, number, err := syslog.SearchLogList(con, pageIndex, pageSize) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, number) } c.ServeJSON() } // @Summary 操作日志分类(图形)统计 // @Description 操作日志分类(图形)统计 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param enddate query string false "统计条件:结束日期" // @Param startdate query string false "统计条件:开始日期" // @Param description query string false "统计条件:日志内容。模糊匹配" // @Param staff query string false "统计条件:操作人姓名。模糊匹配" // @Param ip query string false "统计条件:操作IP地址。模糊匹配" // @Param success query int false "统计条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功" // @Param opttype query int false "统计条件:操作类型。关联系统字典log_opttype。" // @Param audittype query int false "统计条件:审计类型。关联系统字典log_audittype。" // @Param eventtype query int false "统计条件:事件类型。关联系统字典log_eventtype。" // @Param eventlevel query int false "统计条件:事件等级。关联系统字典log_eventlevel。" // @Param cycletype query int false "统计周期。值列表为:year,month,day,hour,week,默认为hour。" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/chartstat [get] func (c *ApiController) GetECharsSysLogStat() { con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") con["description"] = c.GetString("description", "") con["staff"] = c.GetString("staff", "") con["ip"] = c.GetString("ip", "") con["success"] = c.GetString("success", "") con["logtype"] = c.GetString("opttype", "") con["audittype"] = c.GetString("audittype", "") con["eventtype"] = c.GetString("eventtype", "") con["eventlevel"] = c.GetString("eventlevel", "") con["cycletype"] = c.GetString("cycletype", "") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.SetUserInfo(userinfo) tableData, err := syslog.EchartStat(con) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @Summary 操作日志分类(数据列表)统计 // @Description 操作日志分类(数据列表)统计 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param enddate query int false "统计条件:结束日期" // @Param startdate query int false "统计条件:开始日期" // @Param description query int false "统计条件:日志内容。模糊匹配" // @Param staff query string false "统计条件:操作人姓名。模糊匹配" // @Param ip query string false "统计条件:操作IP地址。模糊匹配" // @Param success query int false "统计条件:操作结果。关联系统字典log_optresult。0: 失败 1:成功" // @Param opttype query int false "统计条件:操作类型。关联系统字典log_opttype。" // @Param audittype query int false "统计条件:审计类型。关联系统字典log_audittype。" // @Param eventtype query int false "统计条件:事件类型。关联系统字典log_eventtype。" // @Param eventlevel query int false "统计条件:事件等级。关联系统字典log_eventlevel。" // @Param stattype query int false "统计类型。值为:logtype或者空。" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/datastat [get] func (c *ApiController) GetDataSysLogStat() { con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") con["description"] = c.GetString("description", "") con["staff"] = c.GetString("staff", "") con["ip"] = c.GetString("ip", "") con["success"] = c.GetString("success", "") con["logtype"] = c.GetString("opttype", "") con["audittype"] = c.GetString("audittype", "") con["eventtype"] = c.GetString("eventtype", "") con["eventlevel"] = c.GetString("eventlevel", "") con["stattype"] = c.GetString("stattype", "") //统计字段 userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.SetUserInfo(userinfo) tableData, err := syslog.DataStat(con) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @Summary 操作日志备份 // @Description 操作日志备份 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param enddate query string false "备份结束日期" // @Param startdate query string false "备份开始日期" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/backup [post] func (c *ApiController) BackupSysLog() { con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.SetUserInfo(userinfo) err := syslog.Backup(con) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 获取操作日志备份列表 // @Description 获取操作日志备份列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int false "当前查询的分页页码,默认为1" // @Param pagesize query int false "每页显示的数据条数,默认为20" // @Param enddate query string false "备份结束日期" // @Param startdate query string false "备份开始日期" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/backup/list [get] func (c *ApiController) BackupSysLogList() { pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 20) con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.SetUserInfo(userinfo) lst, total, err := syslog.BackupList(con, pageIndex, pageSize) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(lst, total) } c.ServeJSON() } // @Summary 操作日志备份删除 // @Description 操作日志备份删除 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "备份记录ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/backup/del [post] func (c *ApiController) DelBackupLog() { userinfo := c.GetCurrentUserInfo() sysdb := new(bo.SystemLog) sysdb.SetUserInfo(userinfo) id, _ := c.GetInt("id") err := sysdb.Delete(id) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 获取数据备份列表 // @Description 获取数据备份列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int false "当前查询的分页页码,默认为1" // @Param pagesize query int false "每页显示的数据条数,默认为20" // @Param enddate query string false "备份结束日期" // @Param startdate query string false "备份开始日期" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /db/backup/list [get] func (c *ApiController) BackupDBList() { pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 20) con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") userinfo := c.GetCurrentUserInfo() sysdb := new(bo.GlobalBackupMgr) sysdb.SetUserInfo(userinfo) lst, total, err := sysdb.List(con, pageIndex, pageSize) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(lst, total) } c.ServeJSON() } // @Summary 创建新数据备份 // @Description 创建新数据备份 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /db/backup/new [post] func (c *ApiController) NewBackupDB() { userinfo := c.GetCurrentUserInfo() sysdb := new(bo.GlobalBackupMgr) sysdb.SetUserInfo(userinfo) err := sysdb.Save() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 删除指定的数据备份 // @Description 删除指定的数据备份 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int false "备份记录ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /db/backup/del [post] func (c *ApiController) DelBackupDB() { userinfo := c.GetCurrentUserInfo() sysdb := new(bo.GlobalBackupMgr) sysdb.SetUserInfo(userinfo) sysdb.Model.Id, _ = c.GetInt("id") err := sysdb.Delete() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 数据还原 // @Description 还原指定的数据备份 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "备份记录ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /db/backup/recover [post] func (c *ApiController) RecorverBackupDB() { cid, _ := c.GetInt("id") userinfo := c.GetCurrentUserInfo() sysdb := new(bo.GlobalBackupMgr) sysdb.SetUserInfo(userinfo) err := sysdb.Recover(cid) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 获取当前的服务器运行日志 // @Description 获取当前的服务器运行日志。每次获取10Kb的日志内容,第一次获取时默认为0,继续获取时需要将上次的最大位置传给offset参数。 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param offset query int true "获取的日志起始字节位置。" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /log/runtime_log [get] func (c *ApiController) GetSystemRuntimeLog() { logfilename := "./" + time.Now().Format("20060102") + ".log" fH, ferr := os.Stat(logfilename) if ferr != nil { c.Data["json"] = c.ResultError(ferr.Error()) c.ServeJSON() return } logsize := fH.Size() if logsize == 0 { c.Data["json"] = c.ResultOK("", 0) c.ServeJSON() return } offsetkb, _ := c.GetInt64("offset", 0) loghander, err2 := os.Open(logfilename) if err2 != nil { logger.Logger.Error(err2) c.Data["json"] = c.ResultError(ferr.Error()) c.ServeJSON() return } defer loghander.Close() logreader := bufio.NewReader(loghander) if offsetkb == 0 { //初始加载,只获取最新的日志(10Kb) if logsize > int64(1024*10) { offsetkb = logsize - int64(1024*10) } } _, ferr = loghander.Seek(offsetkb, 0) if ferr != nil { logger.Logger.Error(ferr) c.Data["json"] = c.ResultError(ferr.Error()) c.ServeJSON() return } var buf []byte if (logsize - offsetkb) > int64(1024*10) { buf = make([]byte, 1024*10) } else { buf = make([]byte, logsize-offsetkb) } n, ferr := logreader.Read(buf) if ferr != nil { logger.Logger.Error(ferr) c.Data["json"] = c.ResultError(ferr.Error()) c.ServeJSON() return } c.Data["json"] = c.ResultOK(string(buf), offsetkb+int64(n)) c.ServeJSON() } // @Summary 获取系统告警列表 // @Description 获取系统告警列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int false "当前查询的分页页码,默认为1" // @Param pagesize query int false "每页显示的数据条数,默认为20" // @Param enddate query string false "查询条件:结束日期" // @Param startdate query string false "查询条件:开始日期" // @Param eventtype query int false "查询条件:事件类型。关联系统字典代码log_eventtype。" // @Param alarmtype query string false "查询条件:告警类型。值为disk|cpu|mem之一。" // @Param description query string false "查询条件:告警内容。模糊查询" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /sys/alarm/list [get] func (c *ApiController) GetSysAlarmList() { pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 20) con := map[string]string{} con["enddate"] = c.GetString("enddate", "") con["startdate"] = c.GetString("startdate", "") con["eventtype"] = c.GetString("eventtype", "") con["alarmtype"] = c.GetString("alarmtype", "") con["description"] = c.GetString("description") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemAlarm) syslog.SetUserInfo(userinfo) lst, total, err := syslog.SearchList(con, pageIndex, pageSize) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(lst, total) } c.ServeJSON() } // @Summary 获取全部或者指定的系统参数列表 // @NotAuth 不需要授权可访问 // @Description 获取全部或者指定的系统参数列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param param_name query string false "参数名" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getSysParamList [get] func (c *ApiController) GetSystemParam() { param_name := c.GetString("param_name") obj := bo.Global_sys_param{Param_name: param_name} datalist, err := bo.GetSysParamList(obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(datalist, len(datalist)) } c.ServeJSON() } // 保存系统参数 godoc // @Summary 保存系统参数 // @Description 保存系统参数 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param param_name query string false "参数缟编码" // @Param param_value query string false "参数值" // @Param param_memo query string false "参数描述" // @Success 200 {object} ResultOK 成功 // @Success 200 {object} ResultError 错误 // @Failure 500 status 失败 // @router /saveSysParam [post] func (c *ApiController) SaveSystemParam() { param_name := c.GetString("param_name") param_value := c.GetString("param_value") param_memo := c.GetString("param_memo") obj := bo.Global_sys_param{Param_name: param_name, Param_memo: param_memo, Param_value: param_value} _, err := bo.SaveSysParam(obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } c.ServeJSON() } // @Summary 获取指定系统字典的下级字典码定义 // @Description 获取指定系统字典的下级字典码定义 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param code query string false "未指定code时,将返回根字典下的子字典定义列表。" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getGlobalChildrenCode [get] func (c *ApiController) GetGlobalChildrenCode() { param := tools.ParseFormParam(c.Ctx.Request.Form) param["code"] = c.GetString("code", "") obj := new(bo.Global) tableData, err := obj.GetChildrenGlobalCode(tools.IsEmpty(param["code"])) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @Summary 根据id获取字典定义 // @Description 根据id获取字典定义 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query string false "系统字典码ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getGlobalCode/byid [get] func (c *ApiController) GetGlobalCodeByID() { param := tools.ParseFormParam(c.Ctx.Request.Form) param["id"] = c.GetString("id", "") param["pageindex"] = c.GetString("pageindex", "1") param["pagesize"] = c.GetString("pagesize", "1") obj := new(bo.Global) tableData, number, err := obj.QueryGlobalCodeList(param) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, number) } c.ServeJSON() } // @Summary 查询系统字典列表 // @Description 查询系统字典列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int false "当前查询的页码。默认为1" // @Param pagesize query int false "每页显示的数据记录数,默认为20" // @Param pcode query string false "查询条件:指定的父级字典码" // @Param code query string false "查询条件:指定的字典码" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /getGlobalCode [get] func (c *ApiController) GetGlobalCode() { param := tools.ParseFormParam(c.Ctx.Request.Form) param["pcode"] = c.GetString("pcode", "") param["code"] = c.GetString("code", "") param["pageindex"] = c.GetString("pageindex", "1") param["pagesize"] = c.GetString("pagesize", "20") obj := new(bo.Global) tableData, number, err := obj.QueryGlobalCodeList(param) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, number) } c.ServeJSON() } // @Summary 获取区域列表 // @Description 获取区域列表。以tree结构形式返回。 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /area/tree [get] func (c *ApiController) GetAdminAreaTree() { obj := bo.BasicArea{} obj.SetUserInfo(c.GetCurrentUserInfo()) gettype := c.GetString("type") if gettype == "1" { tableData, err := new(bo.Global).GetCategoryTree() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } } else { tableData, err := obj.GetAreaTree() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } } c.ServeJSON() } // @Summary 获取指定获取的下级区域列表 // @Description 获取指定获取的下级区域列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query string true "查询条件:区域ID" // @Param all query int false "查询条件:是否获取全部子区域。1表示是,反之表示否。默认为0" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /area/children/list [get] func (c *ApiController) GetAdminAreaChildren() { obj := new(bo.BasicArea) obj.SetUserInfo(c.GetCurrentUserInfo()) isall := c.GetString("all") ext_datatype := c.GetString("ext_datatype") //扩展。需要追加节点的数据类型。支持电梯equ、摄像头camera、电子围栏ele、停车场park、门禁access_control等 var err error var tableData []orm.Params areaIdStr := c.GetString("id") if isall == "1" { tableData, err = obj.GetAreaAllChildrens(areaIdStr) } else { tableData, err = obj.GetAreaChildrens(areaIdStr) } if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { areaId, _ := c.GetInt("id", -1) //是否需要追加业务数据节点 if areaId > -1 && ext_datatype != "" { statCountLst := []orm.Params{} //根据统计的下级区域子元素数量,设置区域节点的相关属性 if err != nil { log.Println(err) } else if len(statCountLst) > 0 { for k1, row := range tableData { //判断是否有子节点(isParent:true),已有子节点时不需要再次处理 if tools.IsEmpty(row["isParent"]) != "false" { continue } tmpareaid := tools.IsEmpty(row["id"]) + tools.IsEmpty(row["ID"]) for _, row2 := range statCountLst { area_id := tools.IsEmpty(row2["area_id"]) + tools.IsEmpty(row2["AREA_ID"]) if tmpareaid == area_id { tableData[k1]["isParent"] = row2["isParent"] tableData[k1]["count"] = row2["cnt"] } } } } } c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @router /code/tree [get] func (c *ApiController) GetCategoryTree() { obj := new(bo.Global) tableData, err := obj.GetCategoryTree() if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @Summary 获取角色的已分配资源ID列表 // @Description 获取角色的已分配资源ID列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param roleid query string true "角色ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /get_menu_tree [get] func (c *ApiController) GetMenuTree() { Roleid, _ := c.GetInt("roleid", -1) obj := new(bo.Global) tableData, err := obj.GetMenuTree(Roleid) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } c.ServeJSON() } // @Summary 保存系统字典 // @Description 保存系统字典 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param name query string true "系统字典名称" // @Param parentcode query string true "上级系统字典编码" // @Param code query string true "系统字典编码" // @Param id query string false "系统字典ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /saveGlobalcode [post] func (c *ApiController) SaveGlobalCode() { name := c.GetString("name") parentcode := c.GetString("parentcode") code := c.GetString("code") id, _ := strconv.Atoi(c.GetString("id", "0")) obj := bo.Global_const_code{Id: id, Code: code, Parentcode: parentcode, Name: name} user := c.GetCurrentUserInfo() _, err := bo.SaveGlobalCode(obj, user) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK("") } c.ServeJSON() } // @Summary 删除指定系统字典定义 // @Description 根据字典编码或ID删除指定系统字典定义 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param code query string false "系统字典编码" // @Param id query string false "系统字典ID" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /deleteGlobalcode [post] func (c *ApiController) DelGlobalCode() { code := c.GetString("code") id, _ := strconv.Atoi(c.GetString("id", "0")) obj := bo.Global_const_code{Code: code, Id: id} user := c.GetCurrentUserInfo() datalist, err := bo.DeleteGlobalCode(obj, user) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() } // @Summary 删除指定模型的某条数据记录 // @Description 删除指定模型的某条数据记录 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param tablename query string true "数据模型名称" // @Param id query string true "数据记录ID" // @Param primaryfield query string false "数据记录ID列名。默认为id" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /record/delete [post] func (c *ApiController) DeleteRecord() { primaryField := c.GetString("primaryfield", "id") Id := c.GetString("id", "") tableName := c.GetString("tablename", "") if Id == "" { c.Data["json"] = c.ResultError("id参数不允许为空!") } else if tableName == "" { c.Data["json"] = c.ResultError("tablename参数不允许为空!") } else { err := tools.DeleteTableReorce(tableName, primaryField, Id) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", 0) } } c.ServeJSON() } // @Summary 获取指定模型的某条数据记录 // @Description 获取指定模型的某条数据记录 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param tablename query string true "数据模型名称" // @Param id query string true "数据记录ID" // @Param primaryfield query string false "数据记录ID列名。默认为id" // @Success 200 {object} WarpOK 成功 // @Failure 500 {object} WarpError 失败 // @router /record/get [get] func (c *ApiController) GetSingleRecord() { primaryField := c.GetString("primaryfield", "id") Id := c.GetString("id", "") tableName := c.GetString("tablename", "") if Id == "" { c.Data["json"] = c.ResultError("id参数不允许为空!") } else if tableName == "" { c.Data["json"] = c.ResultError("tablename参数不允许为空!") } else { tableData, err := tools.GetSingleRecord(tableName, primaryField, Id) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, 0) } } c.ServeJSON() } // @Summary 根据查询条件搜索区域(变电站)列表 // @Description 根据查询条件搜索区域(变电站)列表 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pageindex query int false "当前查询的分页页码,默认为1" // @Param pagesize query int false "每页显示的数据条数,默认为20" // @Param name query string false "查询条件:区域名称。模糊匹配" // @Param id query int false "查询条件:区域ID" // @Param pid query int false "查询条件:父级区域ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /basic_area/list [get] func (c *ApiController) BasicAreaList() { enableOrgCode, _ := bo.GetSysParamValue("xrootcode", "") areaObject := new(bo.BasicArea) areaObject.SetUserInfo(c.GetCurrentUserInfo()) pageIndex, _ := c.GetInt("pageindex", 1) pageSize, _ := c.GetInt("pagesize", 1000) areaObject.Model.Pid, _ = c.GetInt("pid", -1) if areaObject.Model.Pid > -1 { enableOrgCode = "" } areaObject.Model.AreaName = c.GetString("name", "") areaObject.Model.AreaKind = c.GetString("area_kind", "") areaObject.Model.AreaType = c.GetString("area_type", enableOrgCode) areaObject.Model.AreaLevel, _ = c.GetInt("area_level", 0) areaObject.Model.Id, _ = c.GetInt("id", 0) tableData, number, err := areaObject.SearchArea(pageIndex, pageSize) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(tableData, number) } c.ServeJSON() } // @Summary 查询指定区域的下级区域数量 // @Description 查询指定区域的下级区域数量 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param pid query int true "查询条件:父级区域ID" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /basic_area/getchildrencount [get] func (c *ApiController) BasicAreaChildrenCount() { areaObject := new(bo.BasicArea) pid, _ := c.GetInt("pid", -1) if pid == -1 { c.Data["json"] = c.ResultError("pid不能为空") c.ServeJSON() return } number, err := areaObject.GetAreaTotal(pid) if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK("", number) } c.ServeJSON() } // @Summary 保存区域信息 // @Description 保存区域信息 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "区域ID。编辑时必填。" // @Param pid query int true "父级区域ID" // @Param area_type query int true "区域类型" // @Param area_name query string true "区域名称" // @Param area_addr query string false "区域地址" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /basic_area/save [post] func (c *ApiController) BasicAreaSave() { object := new(bo.BasicArea) object.Model.Id, _ = c.GetInt("id", 0) object.Model.Pid, _ = c.GetInt("pid", 0) object.Model.AreaType = c.GetString("area_type", "") object.Model.AreaName = c.GetString("area_name", "") object.Model.AreaCode = "" object.Model.AreaKind = c.GetString("area_kind", "") object.Model.Jd, _ = strconv.ParseFloat(c.GetString("jd", "0"), 32) object.Model.Wd, _ = strconv.ParseFloat(c.GetString("wd", "0"), 32) object.Model.LiveNum, _ = c.GetInt("live_number", 0) object.Model.AreaLevel, _ = c.GetInt("area_level", 0) object.Model.IsLeaf, _ = c.GetInt("leaf", 0) object.Model.AreaSort, _ = c.GetInt("area_sort", 0) object.Model.AreaAddr = c.GetString("area_addr", "") object.Model.Apmac = c.GetString("apmac", "") RoomColor := c.GetString("room_color", "") var unit = c.GetString("unit", "") object.SetUserInfo(c.GetCurrentUserInfo()) AreaId, err := object.SaveArea(unit, RoomColor) if err != nil { if strings.Contains(err.Error(), "") { c.Data["json"] = c.ResultError(object.Model.AreaName + "已存在") } else { c.Data["json"] = c.ResultError(err.Error()) } } else { c.Data["json"] = c.ResultOK(AreaId, 0) } c.ServeJSON() } // @Summary 删除指定区域信息 // @Description 删除指定区域信息 // @Tags 基础服务接口 // @Accept x-www-form-urlencoded // @Produce json // @Param id query int true "区域ID。" // @Param isall query int false "是否同时删除所有下级区域。1为是,反之为否。默认为否" // @Success 200 {object} ResultOK 成功 // @Failure 500 {object} ResultError 失败 // @router /basic_area/del [post] func (c *ApiController) BasicAreaDelete() { areaObject := new(bo.BasicArea) id, _ := c.GetInt("id") //是否批量删除所有下级节点 isall := c.GetString("isall") var err error if isall == "1" { err = areaObject.DelAreaInfo(id) } else { err = areaObject.DeleteArea() } if err != nil { c.Data["json"] = c.ResultError(err.Error()) } else { c.Data["json"] = c.ResultOK(nil, 0) } c.ServeJSON() }