1 Star 0 Fork 1

flyiot / flylibs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
FlyApi.go 2.16 KB
一键复制 编辑 原始数据 按行查看 历史
flyrainning 提交于 2023-03-17 17:14 . u
package FlyAPIs
import (
"encoding/base64"
"net/http"
"strconv"
"strings"
)
type FlyApi struct {
W http.ResponseWriter
R *http.Request
ServerSettings FlyApiSettings
}
func NewFlyApi(w http.ResponseWriter, r *http.Request, settings FlyApiSettings) *FlyApi {
s := FlyApi{}
s.W = w
s.R = r
s.ServerSettings = settings
return &s
}
func (s *FlyApi) Ok(data interface{}) API_Resp {
resp := API_Resp{
Result: true,
Data: data,
}
return resp
}
func (s *FlyApi) Err(data interface{}) API_Resp {
if val, ok := data.(error); ok {
data = val.Error()
}
resp := API_Resp{
Result: false,
Data: data,
}
return resp
}
func (s *FlyApi) V(key string, defaults string) string {
val := defaults
// vars := r.URL.Query()
// if v, ok := vars[key]; ok {
// val = v[0]
// }
s.R.ParseForm()
if len(s.R.Form[key]) > 0 {
val = s.R.Form[key][0]
}
return val
}
func (s *FlyApi) VString(key string, defaults string) string {
return s.V(key, defaults)
}
func (s *FlyApi) VInt(key string, defaults int) int {
v := s.V(key, strconv.Itoa(defaults))
val, err := strconv.Atoi(v)
if err != nil {
return defaults
}
return val
}
func (s *FlyApi) VInt64(key string, defaults int64) int64 {
v := s.V(key, strconv.FormatInt(defaults, 10))
val, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return defaults
}
return val
}
func (s *FlyApi) VFloat64(key string, defaults float64) float64 {
v := s.V(key, strconv.FormatFloat(defaults, 'E', -1, 64))
val, err := strconv.ParseFloat(v, 64)
if err != nil {
return defaults
}
return val
}
func (s *FlyApi) VBool(key string, defaults bool) bool {
bstr := "0"
if defaults {
bstr = "1"
}
v := s.V(key, bstr)
v = strings.ToLower(v)
v = strings.TrimSpace(v)
if v == "0" || v == "false" || v == "f" || v == "null" || v == "nil" || v == "undefined" || v == "empty" || v == "[]" || v == "{}" {
return false
}
if len(v) > 0 {
return true
}
return false
}
func (s *FlyApi) VBase64(key string, defaults []byte) []byte {
bstr := s.V(key, "")
if bstr == "" {
return defaults
}
decodeBytes, err := base64.StdEncoding.DecodeString(bstr)
if err != nil {
return defaults
}
return decodeBytes
}
Go
1
https://gitee.com/flyiot/flylibs.git
git@gitee.com:flyiot/flylibs.git
flyiot
flylibs
flylibs
482d8b584f3f

搜索帮助

53164aa7 5694891 3bd8fe86 5694891