123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package myth
- const (
- ErrOk = 0
- ErrNotFound = 1001
- ErrException = 1002
- ErrBadRequest = 1003
- ErrMethodNotAllowed = 1004
- ErrParamsError = 1005
- ErrUnAuthorized = 1006
- ErrDataNotFound = 1007
- ErrNotAllowed = 1008
- ErrDataExists = 1009
- ErrDataValidate = 1010
- VarUserAuthorization = `access_token`
- HTTPHeaderAuthorization = `Authorization`
- HTTPHeaderToken = `X-Token`
- )
- var (
- statusMessage map[int]string
- )
- type ReplyData struct {
- Status int `json:"status" xml:"status"`
- Message string `json:"message" xml:"message"`
- Errs map[string]string `json:"errors,omitempty" xml:"errors,omitempty"`
- PageCount int `json:"pageCount,omitempty"`
- Total int `json:"total,omitempty" xml:"total,omitempty"`
- List interface{} `json:"rows,omitempty" xml:"rows,omitempty"`
- Data interface{} `json:"data,omitempty" xml:"data,omitempty"`
- }
- func init() {
- statusMessage = make(map[int]string)
- statusMessage[ErrOk] = `ok`
- statusMessage[ErrNotFound] = `Not found`
- statusMessage[ErrException] = `Exception`
- statusMessage[ErrBadRequest] = `Routing parameter error`
- statusMessage[ErrMethodNotAllowed] = `Method not allowed`
- statusMessage[ErrParamsError] = `Parameter or format error`
- statusMessage[ErrUnAuthorized] = `Not sign in or session has expired`
- statusMessage[ErrDataNotFound] = `Data not found`
- statusMessage[ErrNotAllowed] = `No access`
- statusMessage[ErrDataExists] = `Data exists`
- statusMessage[ErrDataValidate] = `Data verification failed`
- }
- func NewReplyData(status int) *ReplyData {
- var (
- text string
- exists bool
- )
- if text, exists = statusMessage[status]; !exists {
- text = `incorrect data type`
- }
- return &ReplyData{
- Status: status,
- Message: text,
- }
- }
- func ReplyOk() *ReplyData {
- message, _ := statusMessage[ErrOk]
- return &ReplyData{
- Status: ErrOk,
- Message: message,
- }
- }
- func ReplyErr(status int, message string) *ReplyData {
- text, _ := statusMessage[status]
- errs := map[string]string{
- "message": message,
- }
- return &ReplyData{
- Status: status,
- Message: text,
- Errs: errs,
- }
- }
- func ReplyErrors(status int, errors map[string]string) *ReplyData {
- message, _ := statusMessage[status]
- return &ReplyData{
- Status: status,
- Message: message,
- Errs: errors,
- }
- }
- func ReplyRows(total, pageCount int, rows interface{}) *ReplyData {
- message, _ := statusMessage[ErrOk]
- return &ReplyData{
- Status: ErrOk,
- Message: message,
- List: rows,
- Total: total,
- PageCount: pageCount,
- }
- }
- func ReplyRow(row interface{}) *ReplyData {
- message, _ := statusMessage[ErrOk]
- return &ReplyData{
- Status: ErrOk,
- Message: message,
- Data: row,
- }
- }
|