mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-12 04:03:44 +08:00
发布代码生成、更新20+表单组件,优化数据字典,gf版本更新到2.3.1
This commit is contained in:
69
server/utility/url/url.go
Normal file
69
server/utility/url/url.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package url
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/utility/validate"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// UriToMap 将URL参数转为map
|
||||
func UriToMap(uri string) g.MapStrStr {
|
||||
m := make(map[string]string)
|
||||
if len(uri) < 1 {
|
||||
return nil
|
||||
}
|
||||
if uri[0:1] == "?" {
|
||||
uri = uri[1:]
|
||||
}
|
||||
pars := strings.Split(uri, "&")
|
||||
for _, par := range pars {
|
||||
kv := strings.Split(par, "=")
|
||||
m[kv[0]] = kv[1]
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// MapToUri 将map转为URL参数
|
||||
func MapToUri(params g.MapStrStr) string {
|
||||
escape := ""
|
||||
for k, v := range params {
|
||||
if escape != "" {
|
||||
escape = escape + "&"
|
||||
}
|
||||
escape = escape + k + "=" + v
|
||||
}
|
||||
return escape
|
||||
}
|
||||
|
||||
// GetAddr 获取请求中的请求地址,协议+域名/IP:端口
|
||||
func GetAddr(ctx context.Context) string {
|
||||
r := ghttp.RequestFromCtx(ctx)
|
||||
if r == nil {
|
||||
return ""
|
||||
}
|
||||
var (
|
||||
scheme = "http"
|
||||
proto = r.Header.Get("X-Forwarded-Proto")
|
||||
)
|
||||
if r.TLS != nil || gstr.Equal(proto, "https") {
|
||||
scheme = "https"
|
||||
}
|
||||
return fmt.Sprintf(`%s://%s`, scheme, r.Host)
|
||||
}
|
||||
|
||||
// GetDomain 获取请求中的域名,如果请求不是域名则返回空
|
||||
func GetDomain(ctx context.Context) string {
|
||||
r := ghttp.RequestFromCtx(ctx)
|
||||
if r == nil {
|
||||
g.Log().Warningf(ctx, "GetDomain ctx not request")
|
||||
return ""
|
||||
}
|
||||
if validate.IsDNSName(r.Host) {
|
||||
return r.Host
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user