mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-09 15:12:26 +08:00
[feature] Format the code and improve Mini Program authorization to o… (#473)
* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session) * [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html) * upgrade json error * upgrade json error Co-authored-by: houseme <houseme@outlook.com>
This commit is contained in:
20
util/http.go
20
util/http.go
@@ -17,7 +17,7 @@ import (
|
||||
"golang.org/x/crypto/pkcs12"
|
||||
)
|
||||
|
||||
//HTTPGet get 请求
|
||||
// HTTPGet get 请求
|
||||
func HTTPGet(uri string) ([]byte, error) {
|
||||
response, err := http.Get(uri)
|
||||
if err != nil {
|
||||
@@ -31,7 +31,7 @@ func HTTPGet(uri string) ([]byte, error) {
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
//HTTPPost post 请求
|
||||
// HTTPPost post 请求
|
||||
func HTTPPost(uri string, data string) ([]byte, error) {
|
||||
body := bytes.NewBuffer([]byte(data))
|
||||
response, err := http.Post(uri, "", body)
|
||||
@@ -46,7 +46,7 @@ func HTTPPost(uri string, data string) ([]byte, error) {
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
//PostJSON post json 数据请求
|
||||
// PostJSON post json 数据请求
|
||||
func PostJSON(uri string, obj interface{}) ([]byte, error) {
|
||||
jsonBuf := new(bytes.Buffer)
|
||||
enc := json.NewEncoder(jsonBuf)
|
||||
@@ -91,7 +91,7 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
|
||||
return responseData, contentType, err
|
||||
}
|
||||
|
||||
//PostFile 上传文件
|
||||
// PostFile 上传文件
|
||||
func PostFile(fieldname, filename, uri string) ([]byte, error) {
|
||||
fields := []MultipartFormField{
|
||||
{
|
||||
@@ -103,7 +103,7 @@ func PostFile(fieldname, filename, uri string) ([]byte, error) {
|
||||
return PostMultipartForm(fields, uri)
|
||||
}
|
||||
|
||||
//MultipartFormField 保存文件或其他字段信息
|
||||
// MultipartFormField 保存文件或其他字段信息
|
||||
type MultipartFormField struct {
|
||||
IsFile bool
|
||||
Fieldname string
|
||||
@@ -111,7 +111,7 @@ type MultipartFormField struct {
|
||||
Filename string
|
||||
}
|
||||
|
||||
//PostMultipartForm 上传文件或其他多个字段
|
||||
// PostMultipartForm 上传文件或其他多个字段
|
||||
func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte, err error) {
|
||||
bodyBuf := &bytes.Buffer{}
|
||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
||||
@@ -163,7 +163,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
||||
return
|
||||
}
|
||||
|
||||
//PostXML perform a HTTP/POST request with XML body
|
||||
// PostXML perform a HTTP/POST request with XML body
|
||||
func PostXML(uri string, obj interface{}) ([]byte, error) {
|
||||
xmlData, err := xml.Marshal(obj)
|
||||
if err != nil {
|
||||
@@ -183,7 +183,7 @@ func PostXML(uri string, obj interface{}) ([]byte, error) {
|
||||
return ioutil.ReadAll(response.Body)
|
||||
}
|
||||
|
||||
//httpWithTLS CA证书
|
||||
// httpWithTLS CA证书
|
||||
func httpWithTLS(rootCa, key string) (*http.Client, error) {
|
||||
var client *http.Client
|
||||
certData, err := ioutil.ReadFile(rootCa)
|
||||
@@ -202,7 +202,7 @@ func httpWithTLS(rootCa, key string) (*http.Client, error) {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
//pkcs12ToPem 将Pkcs12转成Pem
|
||||
// pkcs12ToPem 将Pkcs12转成Pem
|
||||
func pkcs12ToPem(p12 []byte, password string) tls.Certificate {
|
||||
blocks, err := pkcs12.ToPEM(p12, password)
|
||||
defer func() {
|
||||
@@ -224,7 +224,7 @@ func pkcs12ToPem(p12 []byte, password string) tls.Certificate {
|
||||
return cert
|
||||
}
|
||||
|
||||
//PostXMLWithTLS perform a HTTP/POST request with XML body and TLS
|
||||
// PostXMLWithTLS perform a HTTP/POST request with XML body and TLS
|
||||
func PostXMLWithTLS(uri string, obj interface{}, ca, key string) ([]byte, error) {
|
||||
xmlData, err := xml.Marshal(obj)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user