mirror of
https://github.com/tiger1103/gfast.git
synced 2026-09-21 18:20:47 +00:00
项目初始
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gview"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
const (
|
||||
SuccessCode int = 0
|
||||
ErrorCode int = -1
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
// 代码
|
||||
Code int `json:"code" example:"200"`
|
||||
// 数据集
|
||||
Data interface{} `json:"data"`
|
||||
// 消息
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
var response = new(Response)
|
||||
|
||||
func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
||||
response.JsonExit(r, code, msg, data...)
|
||||
}
|
||||
|
||||
func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
||||
response.RJson(r, code, msg, data...)
|
||||
}
|
||||
|
||||
func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
||||
response.SusJson(isExit, r, msg, data...)
|
||||
}
|
||||
|
||||
func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
||||
response.FailJson(isExit, r, msg, data...)
|
||||
}
|
||||
|
||||
func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
|
||||
return response.WriteTpl(r, tpl, view, params...)
|
||||
}
|
||||
|
||||
// 返回JSON数据并退出当前HTTP执行函数。
|
||||
func (res *Response) JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
||||
res.RJson(r, code, msg, data...)
|
||||
r.Exit()
|
||||
}
|
||||
|
||||
// 标准返回结果数据结构封装。
|
||||
// 返回固定数据结构的JSON:
|
||||
// code: 状态码(200:成功,302跳转,和http请求状态码一至);
|
||||
// msg: 请求结果信息;
|
||||
// data: 请求结果,根据不同接口返回结果的数据结构不同;
|
||||
func (res *Response) RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
||||
responseData := interface{}(nil)
|
||||
if len(data) > 0 {
|
||||
responseData = data[0]
|
||||
}
|
||||
response = &Response{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
Data: responseData,
|
||||
}
|
||||
r.SetParam("apiReturnRes", response)
|
||||
r.Response.WriteJson(response)
|
||||
}
|
||||
|
||||
//成功返回JSON
|
||||
func (res *Response) SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
||||
if isExit {
|
||||
res.JsonExit(r, SuccessCode, msg, data...)
|
||||
}
|
||||
res.RJson(r, SuccessCode, msg, data...)
|
||||
}
|
||||
|
||||
//失败返回JSON
|
||||
func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
||||
if isExit {
|
||||
res.JsonExit(r, ErrorCode, msg, data...)
|
||||
}
|
||||
res.RJson(r, ErrorCode, msg, data...)
|
||||
}
|
||||
|
||||
//模板输出
|
||||
func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
|
||||
//绑定模板中需要用到的方法
|
||||
view.BindFuncMap(g.Map{
|
||||
// 根据长度i来切割字符串
|
||||
"subStr": func(str interface{}, i int) (s string) {
|
||||
s1 := gconv.String(str)
|
||||
if gstr.LenRune(s1) > i {
|
||||
s = gstr.SubStrRune(s1, 0, i) + "..."
|
||||
return s
|
||||
}
|
||||
return s1
|
||||
},
|
||||
// 格式化时间戳 年月日
|
||||
"timeFormatYear": func(time interface{}) string {
|
||||
return TimeStampToDate(gconv.Int64(time))
|
||||
},
|
||||
// 格式化时间戳 年月日时分秒
|
||||
"timeFormatDateTime": func(time interface{}) string {
|
||||
return TimeStampToDateTime(gconv.Int64(time))
|
||||
},
|
||||
"add": func(a, b interface{}) int {
|
||||
return gconv.Int(a) + gconv.Int(b)
|
||||
},
|
||||
})
|
||||
//设置全局变量
|
||||
domain, _ := GetDomain(r)
|
||||
view.Assigns(g.Map{
|
||||
"domain": domain,
|
||||
})
|
||||
return r.Response.WriteTpl(tpl, params...)
|
||||
}
|
||||
|
||||
func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
|
||||
r.Response.RedirectTo(location, code...)
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package library
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/crypto/gmd5"
|
||||
"github.com/gogf/gf/encoding/gcharset"
|
||||
"github.com/gogf/gf/encoding/gjson"
|
||||
"github.com/gogf/gf/encoding/gurl"
|
||||
"github.com/gogf/gf/errors/gerror"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/net/ghttp"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"net"
|
||||
)
|
||||
|
||||
//密码加密
|
||||
func EncryptPassword(password, salt string) string {
|
||||
return gmd5.MustEncryptString(gmd5.MustEncryptString(password) + gmd5.MustEncryptString(salt))
|
||||
}
|
||||
|
||||
//时间戳转 yyyy-MM-dd HH:mm:ss
|
||||
func TimeStampToDateTime(timeStamp int64) string {
|
||||
tm := gtime.NewFromTimeStamp(timeStamp)
|
||||
return tm.Format("Y-m-d H:i:s")
|
||||
}
|
||||
|
||||
//时间戳转 yyyy-MM-dd
|
||||
func TimeStampToDate(timeStamp int64) string {
|
||||
tm := gtime.NewFromTimeStamp(timeStamp)
|
||||
return tm.Format("Y-m-d")
|
||||
}
|
||||
|
||||
//获取当前请求接口域名
|
||||
func GetDomain(r *ghttp.Request) (string, error) {
|
||||
pathInfo, err := gurl.ParseURL(r.GetUrl(), -1)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("解析附件路径失败")
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("%s://%s:%s/", pathInfo["scheme"], pathInfo["host"], pathInfo["port"]), nil
|
||||
}
|
||||
|
||||
//获取客户端IP
|
||||
func GetClientIp(r *ghttp.Request) string {
|
||||
ip := r.Header.Get("X-Forwarded-For")
|
||||
if ip == "" {
|
||||
ip = r.GetClientIp()
|
||||
}
|
||||
return ip
|
||||
}
|
||||
|
||||
//服务端ip
|
||||
func GetLocalIP() (ip string, err error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
ipAddr, ok := addr.(*net.IPNet)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if ipAddr.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
if !ipAddr.IP.IsGlobalUnicast() {
|
||||
continue
|
||||
}
|
||||
return ipAddr.IP.String(), nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//获取ip所属城市
|
||||
func GetCityByIp(ip string) string {
|
||||
if ip == "" {
|
||||
return ""
|
||||
}
|
||||
if ip == "[::1]" || ip == "127.0.0.1" {
|
||||
return "内网IP"
|
||||
}
|
||||
url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
|
||||
bytes := g.Client().GetBytes(url)
|
||||
src := string(bytes)
|
||||
srcCharset := "GBK"
|
||||
tmp, _ := gcharset.ToUTF8(srcCharset, src)
|
||||
json, err := gjson.DecodeToJson(tmp)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if json.GetInt("code") == 0 {
|
||||
city := fmt.Sprintf("%s %s", json.GetString("pro"), json.GetString("city"))
|
||||
return city
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
//日期字符串转时间戳(秒)
|
||||
func StrToTimestamp(dateStr string) int64 {
|
||||
tm, err := gtime.StrToTime(dateStr)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
return 0
|
||||
}
|
||||
return tm.Timestamp()
|
||||
}
|
||||
Reference in New Issue
Block a user