package controllers /* 核心控制器 所有api均通过该控制器进行业务分发 请求路由由router.go自动注册,通过在控制器中的方法注释注册为路由 */ import ( "encoding/json" "fmt" "os" "path/filepath" "regexp" "rtzh_elec_temperature/conf" "rtzh_elec_temperature/db" "rtzh_elec_temperature/global" "rtzh_elec_temperature/tools" "runtime" "strconv" "strings" "time" "rtzh_elec_temperature/models/bo" "github.com/astaxie/beego/context" //"github.com/astaxie/beego/orm" //"github.com/astaxie/beego/session" //"github.com/satori/go.uuid" "github.com/patrickmn/go-cache" ) //var GlobalSessions *session.Manager //基础功能服务 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() } // 语音文件播放 // @Summary 语音文件播放 // @Description 语音文件播放。仅支持windows系统。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param filename query string true "语音/录音文件名。需要将文件已上传到系统指定目录。" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /playWav [get] 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) //a := bo.Mp3Entry{Source: dir + "\\pc\\" + filename} //a.PlayWav() } c.Data["json"] = map[string]interface{}{"result": "1", "success": true} c.ServeJSON() } // 获取当前系统的版本号 // @Summary 获取当前系统的版本号 // @Description 获取当前系统的版本号 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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") fmt.Println("Authorization:" + Authorization) if Authorization == "" { return "" } return strings.TrimLeft(Authorization, "Bearer ") } // 心跳保活 // @Summary 心跳保活 // @Description 心跳保活。特殊应用场景下用于token自动续期。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /keep-alive [post] func (c *ApiController) KeepAlive() { c.Data["json"] = "ok" c.ServeJSON() } // 用户登出 // @Summary 用户登出 // @Description 用户登出。集成变电框架时,无需调用此接口。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param sessionid query string true "登录用户的token" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /logout [post] func (c *ApiController) Logout() { sessionid := c.GetString("sessionid", "") bo.RemoveSession(sessionid) delete(bo.CacheUserByToken, sessionid) c.Data["json"] = "ok" c.ServeJSON() } //获取当前登录用户信息 func GetCurrentUser(ctx *context.Context) (obj map[string]interface{}) { token := ctx.Request.Header.Get("Authorization") if token == "" { return nil } token = strings.ReplaceAll(strings.ReplaceAll(token, "Bearer ", ""), " ", "") usr, _ := bo.GetUserInfoByToken(token) return usr } // 用户登录 // @Summary 用户登录 // @Description 用户登录。集成变电框架时,无需调用此接口。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param login_account query string true "用户帐号" // @Param pwd query string true "用户密码" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /login [post] func (c *ApiController) Login() { username := c.GetString("login_account", "") password := c.GetString("pwd", "") if password == "" { c.Data["json"] = c.WarpError("密码不能为空") } else if username == "" { c.Data["json"] = c.WarpError("帐号不能为空") } else { //由变电框架app负责登录 } c.ServeJSON() } // @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() } // @router /getUser [post,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() } // @router /createuser [post] func (c *ApiController) CreateUser() { parameter := map[string]interface{}{} parameter["userid"] = c.GetString("userid", "0") parameter["role"] = c.GetString("role", "") parameter["username"] = c.GetString("username", "") parameter["account"] = c.GetString("account", "") parameter["password"] = c.GetString("password", "") parameter["memo"] = c.GetString("memo", "") status, err := bo.CreateUser(parameter) 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 api // @Accept x-www-form-urlencoded // @Produce json // @Param id query string true "用户ID" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /deleteUser [post] func (c *ApiController) DelUser() { datalist, err := bo.DelUser(c.GetString("id"), c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() } // 修改当前用户密码 // @Summary 修改当前用户密码 // @Description 修改当前用户密码。一般用于用户修改自己的密码。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param newpwd query string true "新密码。需满足系统当前采用的密码规则" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /resetUserPwd [post] func (c *ApiController) ResetUserPwd() { var id = 0 id, _ = strconv.Atoi(c.GetString("id", "0")) if id > 0 { pwd := c.GetString("newpwd") if pwd == "" { c.Data["json"] = c.WarpError("密码不能为空") } else { obj := bo.T_data_user{Id: id, 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 _, err := bo.SavePwd(obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(id) } } } else { c.Data["json"] = c.WarpError("无效的用户ID") } c.ServeJSON() } // @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() } // @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() } // @router /searchUserInfo [get] func (c *ApiController) SearchUserInfo() { c.ServeJSON() } // 获取角色可访问的功能资源 // @Summary 查询角色与功能资源可访问关系 // @Description 查询角色与功能资源可访问关系。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param roleid query string true "角色ID" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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 api // @Accept x-www-form-urlencoded // @Produce json // @Param roleid query string true "角色ID" // @Param funcids query string true "资源功能编号ID。多个ID使用半角逗号分隔" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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 物理清除指定数据表的数据 // @Description 物理清除指定数据表的数据。数据为物理删除,谨慎调用! // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param dtype query string true "表名称。" // @Param day query string fasle "需要数据保留的最近天数。默认为7天" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /clearData [post] func (c *ApiController) ClearData() { dtype := c.GetString("dtype", "") day, _ := c.GetInt("day", 7) if dtype == "" { c.Data["json"] = c.WarpError("清除的目标数据表不能为空") c.ServeJSON() return } _, err := bo.ClearData(dtype, day) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK("") } c.ServeJSON() } // 查询系统操作日志 // @Summary 查询系统操作日志 // @Description 查询系统操作日志。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param startdate query string false "查询的开始时间。格式为:yyyy-mm-dd HH:mi:ss。默认为当前天的0点" // @Param enddate query string false "查询的结束时间。格式为:yyyy-mm-dd HH:mi:ss。默认为当前天的23:59:59" // @Param description query string false "查询的日志内容。模糊匹配。" // @Param success query string false "日志操作结果。仅支持0或1。" // @Param logtype query string false "查询的日志操作类型。仅支持系统字典代码log_opttype定义的code值。" // @Param audittype query string false "查询的日志审计类型。仅支持系统字典代码log_audittype定义的code值。" // @Param eventtype query string false "查询的事件类型。仅支持系统字典代码eventtype定义的code值。" // @Param eventlevel query string false "查询的事件等级。仅支持系统字典代码eventlevel定义的code值。" // @Param pageindex query int false "当前分页页码.默认为第1页" // @Param pagesize query int false "当前每页记录数。默认为20" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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["success"] = c.GetString("success", "") con["logtype"] = c.GetString("logtype", "") con["audittype"] = c.GetString("audittype", "") con["eventtype"] = c.GetString("eventtype", "") con["eventlevel"] = c.GetString("eventlevel", "") userinfo := c.GetCurrentUserInfo() syslog := new(bo.SystemLog) syslog.UserInfo = 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 api // @Accept x-www-form-urlencoded // @Produce json // @Param name query string false "系统参数代码名称。为空时获取所有参数定义。" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @router /getSysParamList [get] func (c *ApiController) GetSystemParam() { param_name := c.GetString("name") obj := bo.Global_sys_param{Param_name: param_name} datalist, err := bo.GetSysParamList(obj, c.GetCurrentUserInfo()) db.CheckErr(err) c.Data["json"] = c.WarpOK(datalist) c.ServeJSON() } // 保存系统参数定义 // @Summary 保存系统参数定义 // @Description 保存系统参数定义。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param param_name query string true "系统参数代码名称" // @Param param_value query string true "系统参数值" // @Param param_memo query string false "系统参数描述" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @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} datalist, err := bo.SaveSysParam(obj) db.CheckErr(err) c.Data["json"] = c.WarpOK(datalist) c.ServeJSON() } // 获取系统常量字典码定义 // @Summary 获取系统常量字典码定义 // @Description 获取系统常量字典码定义。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param pcode query string false "根据指定代码获取其级子码定义" // @Param code query string false "根据指定代码的定义" // @Param pageindex query int false "当前分页页码.默认为第1页" // @Param pagesize query int false "当前每页记录数。默认为20" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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, _, err := obj.QueryGlobalCodeList(param) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(tableData) } c.ServeJSON() } // 保存系统常量字典码定义 // @Summary 保存系统常量字典码定义 // @Description 保存系统常量字典码定义。如果code已在存在,将对已有定义进行更新。 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param name query string true "当前字典名称或者描述" // @Param code query string true "当前字典编码" // @Param parentcode query string true "当前字典编码的上级编码" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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} datalist, err := bo.SaveGlobalCode(obj) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() } // 删除系统常量字典码定义 // @Summary 删除系统常量字典码定义 // @Description 删除系统常量字典码定义。如果指定代码有下级代码将会一并删除 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param code query string true "需要删除的字典编码" // @Success 200 {object} ApiOK 成功 // @Success 200 {object} ApiError 错误 // @Failure 500 status 失败 // @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} datalist, err := bo.DeleteGlobalCode(obj, c.GetCurrentUserInfo()) if err != nil { c.Data["json"] = c.WarpError(err.Error()) } else { c.Data["json"] = c.WarpOK(datalist) } c.ServeJSON() }