-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from iGoogle-ink/gopay/remove-gorequest
fix 去掉第三方http请求库
- Loading branch information
Showing
14 changed files
with
453 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package gopay | ||
|
||
import ( | ||
"encoding/json" | ||
"encoding/xml" | ||
"io" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
type BodyMap map[string]interface{} | ||
|
||
// 设置参数 | ||
func (bm BodyMap) Set(key string, value interface{}) { | ||
bm[key] = value | ||
} | ||
|
||
// 获取参数 | ||
func (bm BodyMap) Get(key string) string { | ||
if bm == nil { | ||
return null | ||
} | ||
var ( | ||
value interface{} | ||
ok bool | ||
v string | ||
) | ||
if value, ok = bm[key]; !ok { | ||
return null | ||
} | ||
if v, ok = value.(string); ok { | ||
return v | ||
} | ||
return convertToString(value) | ||
} | ||
|
||
func convertToString(v interface{}) (str string) { | ||
if v == nil { | ||
return null | ||
} | ||
var ( | ||
bs []byte | ||
err error | ||
) | ||
if bs, err = json.Marshal(v); err != nil { | ||
return null | ||
} | ||
str = string(bs) | ||
return | ||
} | ||
|
||
// 删除参数 | ||
func (bm BodyMap) Remove(key string) { | ||
delete(bm, key) | ||
} | ||
|
||
type xmlMapEntry struct { | ||
XMLName xml.Name | ||
Value string `xml:",chardata"` | ||
} | ||
|
||
func (bm BodyMap) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) { | ||
if len(bm) == 0 { | ||
return nil | ||
} | ||
var ( | ||
value string | ||
vKind reflect.Kind | ||
) | ||
if err = e.EncodeToken(start); err != nil { | ||
return | ||
} | ||
for k, v := range bm { | ||
vKind = reflect.ValueOf(v).Kind() | ||
switch vKind { | ||
case reflect.String: | ||
value = v.(string) | ||
case reflect.Int: | ||
value = Int2String(v.(int)) | ||
case reflect.Int64: | ||
value = Int642String(v.(int64)) | ||
case reflect.Float32: | ||
value = Float32ToString(v.(float32)) | ||
case reflect.Float64: | ||
value = Float64ToString(v.(float64)) | ||
default: | ||
value = "" | ||
} | ||
e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: value}) | ||
} | ||
return e.EncodeToken(start.End()) | ||
} | ||
|
||
func (bm *BodyMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) { | ||
for { | ||
var e xmlMapEntry | ||
err = d.Decode(&e) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return | ||
} | ||
bm.Set(e.XMLName.Local, e.Value) | ||
} | ||
return | ||
} | ||
|
||
// ("bar=baz&foo=quux") sorted by key. | ||
func (bm BodyMap) EncodeWeChatSignParams(apiKey string) string { | ||
var ( | ||
buf strings.Builder | ||
keyList []string | ||
) | ||
for k := range bm { | ||
keyList = append(keyList, k) | ||
} | ||
sort.Strings(keyList) | ||
for _, k := range keyList { | ||
if v := bm.Get(k); v != null { | ||
buf.WriteString(k) | ||
buf.WriteByte('=') | ||
buf.WriteString(v) | ||
buf.WriteByte('&') | ||
} | ||
} | ||
buf.WriteString("key") | ||
buf.WriteByte('=') | ||
buf.WriteString(apiKey) | ||
return buf.String() | ||
} | ||
|
||
// ("bar=baz&foo=quux") sorted by key. | ||
func (bm BodyMap) EncodeAliPaySignParams() string { | ||
var ( | ||
buf strings.Builder | ||
keyList []string | ||
) | ||
keyList = make([]string, 0, len(bm)) | ||
for k := range bm { | ||
keyList = append(keyList, k) | ||
} | ||
sort.Strings(keyList) | ||
for _, k := range keyList { | ||
if v := bm.Get(k); v != null { | ||
buf.WriteString(k) | ||
buf.WriteByte('=') | ||
buf.WriteString(v) | ||
buf.WriteByte('&') | ||
} | ||
} | ||
return buf.String()[:buf.Len()-1] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
module github.com/iGoogle-ink/gopay | ||
|
||
go 1.13 | ||
|
||
require ( | ||
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 // indirect | ||
github.com/parnurzeal/gorequest v0.2.16 | ||
github.com/pkg/errors v0.8.1 // indirect | ||
github.com/smartystreets/goconvey v1.6.4 // indirect | ||
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 // indirect | ||
moul.io/http2curl v1.0.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +0,0 @@ | ||
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484 h1:pEtiCjIXx3RvGjlUJuCNxNOw0MNblyR9Wi+vJGBFh+8= | ||
github.com/elazarl/goproxy v0.0.0-20191011121108-aa519ddbe484/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= | ||
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM= | ||
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8= | ||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= | ||
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= | ||
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= | ||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= | ||
github.com/parnurzeal/gorequest v0.2.16 h1:T/5x+/4BT+nj+3eSknXmCTnEVGSzFzPGdpqmUVVZXHQ= | ||
github.com/parnurzeal/gorequest v0.2.16/go.mod h1:3Kh2QUMJoqw3icWAecsyzkpY7UzRfDhbRdTjtNwNiUE= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc= | ||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= | ||
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= | ||
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= | ||
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5 h1:bHNaocaoJxYBo5cw41UyTMLjYlb8wPY7+WFrnklbHOM= | ||
golang.org/x/net v0.0.0-20191109021931-daa7c04131f5/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= | ||
moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8= | ||
moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE= | ||
Oops, something went wrong.