代码拉取完成,页面将自动刷新
package xjhttp
import (
"encoding/json"
"fmt"
"gitee.com/xjieinfo/xjgo/xjcore/xjtypes"
"net/http"
"net/url"
"strconv"
"strings"
)
type Context struct {
Request *http.Request
Writer http.ResponseWriter
}
func (c *Context) QueryAll() url.Values {
return c.Request.URL.Query()
}
func (c *Context) QueryStr(key string) string {
querys := c.Request.URL.Query()
return querys.Get(key)
}
func (c *Context) QueryInt(key string) (int, error) {
querys := c.Request.URL.Query()
str := querys.Get(key)
val, err := strconv.Atoi(str)
return val, err
}
func (c *Context) QueryInt64(key string) (int64, error) {
querys := c.Request.URL.Query()
str := querys.Get(key)
val, err := strconv.ParseInt(str, 10, 64)
return val, err
}
func (c *Context) PathParam(index int) string {
uri := c.Request.RequestURI
strs := strings.Split(uri, "/")
if len(strs) > index {
return strs[index]
} else {
return ""
}
}
func (c *Context) String(code int, format string, values ...interface{}) {
c.Writer.WriteHeader(code)
fmt.Fprintf(c.Writer, format, values...)
}
func (c *Context) JSON(code int, obj interface{}) {
c.Writer.WriteHeader(code)
j, _ := json.Marshal(obj)
fmt.Fprintf(c.Writer, string(j))
}
func (c *Context) Make(code int, rcode int, data interface{}, msg string) {
c.Writer.WriteHeader(code)
r := new(xjtypes.R).Make(rcode, data, msg)
j, _ := json.Marshal(r)
fmt.Fprintf(c.Writer, string(j))
}
func (c *Context) Success(code int, data interface{}) {
c.Writer.WriteHeader(code)
r := new(xjtypes.R).Success(data)
j, _ := json.Marshal(r)
fmt.Fprintf(c.Writer, string(j))
}
func (c *Context) Fail(code int, msg string) {
c.Writer.WriteHeader(code)
r := new(xjtypes.R).Fail(msg)
j, _ := json.Marshal(r)
fmt.Fprintf(c.Writer, string(j))
}
func (c *Context) Error(code int, err error) {
c.Writer.WriteHeader(code)
r := new(xjtypes.R).Fail(err.Error())
j, _ := json.Marshal(r)
fmt.Fprintf(c.Writer, string(j))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。