api

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2020 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCode = 1

Variables

View Source
var (
	ErrNoParams = errors.New("No params")
	ErrMD5      = errors.New("MD5 Check Error")

	ErrCustom = func(str string) error {
		return errors.New(str)
	}

	RequestBody = "requestBody"
)
View Source
var HttpStatus = http.StatusOK

Functions

func ErrorLogRecover

func ErrorLogRecover(c echo.Context)

捕获panic异样防止程序终止 并且记录到日志

Types

type Js

type Js struct {
	Data interface{}
}

func Json

func Json(data string) *Js

Initialize the json configruation

func (*Js) Arrayindex

func (j *Js) Arrayindex(i int) string

When the data {"result":["username","password"]} can use arrayindex(1) get the username

func (*Js) Get

func (j *Js) Get(key string) *Js

According to the key of the returned data information,return js.data

func (*Js) Getdata

func (j *Js) Getdata() map[string]interface{}

return json data

func (*Js) Getindex

func (j *Js) Getindex(i int) *Js

func (*Js) Getkey

func (j *Js) Getkey(key string, i int) *Js

The data must be []interface{} ,According to your custom number to return your key and array data

func (*Js) Getpath

func (j *Js) Getpath(args ...string) *Js

According to the custom of the PATH to find the PATH

func (*Js) StringtoArray

func (j *Js) StringtoArray() []string

获取[]string类型,整数和浮点类型会被转换成string

func (*Js) ToArray

func (j *Js) ToArray() (k, d []string)

func (*Js) ToData

func (j *Js) ToData() interface{}

func (*Js) ToFloat

func (j *Js) ToFloat() float64

func (*Js) ToInt

func (j *Js) ToInt() int

func (*Js) ToSlice

func (j *Js) ToSlice() []interface{}

func (*Js) Tostring

func (j *Js) Tostring() string

type Jsonparam

type Jsonparam struct {
	// contains filtered or unexported fields
}

type Request

type Request struct {
	Context echo.Context

	Jsonparam *Jsonparam

	Json        gjson.Result
	IsJsonParam bool

	Debug bool
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(c echo.Context) *Request

初始化request

func (*Request) Alpha

func (this *Request) Alpha() *Request

alpha字符

func (*Request) AlphaDash

func (this *Request) AlphaDash() *Request

alpha字符或数字或横杠-_

func (*Request) AlphaNumeric

func (this *Request) AlphaNumeric() *Request

alpha字符或数字

func (*Request) Base64

func (this *Request) Base64() *Request

base64编码

func (*Request) Clean

func (this *Request) Clean()

清理参数

func (*Request) Email

func (this *Request) Email() *Request

邮箱格式

func (*Request) GetError

func (this *Request) GetError() error

返回报错信息

func (*Request) GetFloat

func (this *Request) GetFloat() float64

获取并且验证参数 float64类型 适用于GET或POST参数

func (*Request) GetInt

func (this *Request) GetInt() int

获取并且验证参数 int类型 适用于GET或POST参数

func (*Request) GetJson

func (this *Request) GetJson() gjson.Result

获取并且验证参数 Json类型 适用于Json参数

func (*Request) GetJsonObject

func (this *Request) GetJsonObject(jsonInterface interface{}) interface{}

获取并且验证参数 入参为json对应的结构体 适用于GET或POST参数

  示例
 	request := api.NewRequest(c)
 	err := request.InitDES()
 	if err != nil {
 		//deal error
 	}
 	jsonObject := struct {
		Id     int
		Name   string `valid:"Required;"` // Name 不能为空
		Age    int    `valid:"Range(1, 140)"` // 1 <= Age <= 140,超出此范围即为不合法
		Email  string `valid:"Email; MaxSize(100)"` // Email 字段需要符合邮箱格式,并且最大长度不能大于 100 个字符
		Mobile string `valid:"Mobile"` // Mobile 必须为正确的手机号
		IP     string `valid:"IP"` // IP 必须为一个正确的 IPv4 地址
 	}{}
	//获取params字段内数据就符合json的结构体
 	root := request.GetRoot().NeedValid(true).GetJsonObject(&jsonObject)
	//获取params内key字段符合json的结构体
 	key := request.Param("key").NeedValid(true).GetJsonObject(&jsonObject)
 	err = request.GetError()
 	if err != nil {
 		//deal error
 	}

func (*Request) GetParam

func (this *Request) GetParam(key string) *Request

获取Get参数

func (*Request) GetRoot

func (this *Request) GetRoot() *Request

func (*Request) GetString

func (this *Request) GetString() string

获取并且验证参数 string类型 适用于GET或POST参数

func (*Request) IP

func (this *Request) IP() *Request

IP格式,目前只支持IPv4格式验证

func (*Request) InitDES

func (this *Request) InitDES() error

进行签名验证以及DES加密验证

func (*Request) InitRawJson

func (this *Request) InitRawJson() error

初始化restful-raw参数

func (*Request) InitWithoutDES added in v1.1.9

func (this *Request) InitWithoutDES() error

跳过签名和加密

func (*Request) Match

func (this *Request) Match(match string) *Request

正则匹配,其他类型都将被转成字符串再匹配(fmt.Sprintf(“%v”, obj).Match)

func (*Request) Max

func (this *Request) Max(i int) *Request

设置参数最大值

func (*Request) Min

func (this *Request) Min(i int) *Request

设置参数最小值

func (*Request) Mobile

func (this *Request) Mobile() *Request

手机号

func (*Request) NeedValid

func (this *Request) NeedValid(b bool) *Request

仅配合GetJsonObject使用

func (*Request) NoMatch

func (this *Request) NoMatch(match string) *Request

反正则匹配,其他类型都将被转成字符串再匹配(fmt.Sprintf(“%v”, obj).Match)

func (*Request) Numeric

func (this *Request) Numeric() *Request

数字

func (*Request) Param

func (this *Request) Param(key string) *Request

获取请求参数顺序get->post

func (*Request) Phone

func (this *Request) Phone() *Request

手机号或固定电话号

func (*Request) PostParam

func (this *Request) PostParam(key string) *Request

获取post参数

func (*Request) Require

func (this *Request) Require(b bool) *Request

GET,POST或JSON参数是否必须

func (*Request) SetDefault

func (this *Request) SetDefault(val string) *Request

func (*Request) SetJson

func (this *Request) SetJson(json string)

使用Json参数传入Json字符

func (*Request) Tel

func (this *Request) Tel() *Request

固定电话号

func (*Request) ZipCode

func (this *Request) ZipCode() *Request

邮政编码

type Response

type Response struct {
	Context echo.Context
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse(c echo.Context) *Response

初始化Response

func (*Response) GetParameter

func (this *Response) GetParameter() *RetParameter

func (*Response) RetCustomize

func (this *Response) RetCustomize(code int, d interface{}, msg string) error

返回自定自定义的消息格式

func (*Response) RetError

func (this *Response) RetError(e error, c int) error

返回失败结果

func (*Response) RetSuccess

func (this *Response) RetSuccess(d interface{}) error

返回成功的结果 默认code为1

func (*Response) SetData

func (this *Response) SetData(d interface{})

func (*Response) SetMsg

func (this *Response) SetMsg(s string)

func (*Response) SetStatus

func (this *Response) SetStatus(i int)

设置返回的Status值默认http.StatusOK

func (*Response) Write

func (this *Response) Write(b []byte)

输出返回结果

type RetParameter

type RetParameter struct {
	Code int         `json:"code";xml:"code"`
	Data interface{} `json:"data";xml:"data"`
	Msg  string      `json:"msg";xml:"msg"`
}

Directories

Path Synopsis
Package validation for validations import ( "github.com/astaxie/beego/validation" "log" ) type User struct { Name string Age int } func main() { u := User{"man", 40} valid := validation.Validation{} valid.Required(u.Name, "name") valid.MaxSize(u.Name, 15, "nameMax") valid.Range(u.Age, 0, 140, "age") if valid.HasErrors() { // validation does not pass // print invalid message for _, err := range valid.Errors { log.Println(err.Key, err.Message) } } // or use like this if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok { log.Println(v.Error.Key, v.Error.Message) } } more info: http://beego.me/docs/mvc/controller/validation.md
Package validation for validations import ( "github.com/astaxie/beego/validation" "log" ) type User struct { Name string Age int } func main() { u := User{"man", 40} valid := validation.Validation{} valid.Required(u.Name, "name") valid.MaxSize(u.Name, 15, "nameMax") valid.Range(u.Age, 0, 140, "age") if valid.HasErrors() { // validation does not pass // print invalid message for _, err := range valid.Errors { log.Println(err.Key, err.Message) } } // or use like this if v := valid.Max(u.Age, 140, "ageMax"); !v.Ok { log.Println(v.Error.Key, v.Error.Message) } } more info: http://beego.me/docs/mvc/controller/validation.md

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL