Commit 6bb13dfc by yemin

no message

parent 032776f0
...@@ -8,16 +8,11 @@ import ( ...@@ -8,16 +8,11 @@ import (
) )
// HttpClient 本项目中使用的http客户端 // HttpClient 本项目中使用的http客户端
var HttpClient *http.Client var httpClient *http.Client = &http.Client{}
func init() {
HttpClient = &http.Client{}
//HttpClient.Timeout = config.Conf.Http.Client.Timeout * time.Millisecond //设置请求超时时间
}
// Get get方法的http请求,返回原始字符串 // Get get方法的http请求,返回原始字符串
func Get(url string) ([]byte, error) { func Get(url string) ([]byte, error) {
rep, err := HttpClient.Get(url) rep, err := httpClient.Get(url)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -43,7 +38,7 @@ func GetWithHeader(url string, header *map[string]string) ([]byte, error) { ...@@ -43,7 +38,7 @@ func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
} }
} }
//发送请求 //发送请求
rep, err := HttpClient.Do(req) rep, err := httpClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
...@@ -84,7 +79,7 @@ func Post(url string, header *map[string]string, bodyParam interface{}) ([]byte, ...@@ -84,7 +79,7 @@ func Post(url string, header *map[string]string, bodyParam interface{}) ([]byte,
} }
} }
//发送请求 //发送请求
rep, err := HttpClient.Do(req) rep, err := httpClient.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
package main
func main() {
}
package smapro package minipro
import ( import (
"encoding/json" "encoding/json"
...@@ -9,9 +9,9 @@ import ( ...@@ -9,9 +9,9 @@ import (
// NewAccessToken 获取接口调用凭据。 // NewAccessToken 获取接口调用凭据。
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
func NewAccessToken(appId string, secret string) (*AccessTokenResponse,error) { func (c *MiniClient) NewAccessToken() (*AccessTokenResponse,error) {
result := &AccessTokenResponse{} result := &AccessTokenResponse{}
var url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",appId, secret) var url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",c.appId, c.secret)
r,err := httpclient.Get(url) r,err := httpclient.Get(url)
if err != nil{ if err != nil{
return nil, err return nil, err
...@@ -25,9 +25,9 @@ func NewAccessToken(appId string, secret string) (*AccessTokenResponse,error) { ...@@ -25,9 +25,9 @@ func NewAccessToken(appId string, secret string) (*AccessTokenResponse,error) {
// Code2Session 登录凭证校验。 // Code2Session 登录凭证校验。
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
func Code2Session(code string, appId string, secret string) (*Code2SessionResponse, error) { func (c *MiniClient) Code2Session(code string) (*Code2SessionResponse, error) {
result := &Code2SessionResponse{} result := &Code2SessionResponse{}
var url = fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, secret, code) var url = fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", c.appId, c.secret, code)
r,err := httpclient.Get(url) r,err := httpclient.Get(url)
if err != nil{ if err != nil{
return nil, err return nil, err
......
package minipro
type MiniClient struct {
appId string
secret string
}
// NewMiniClient 返回MiniClient
func NewMiniClient(appId string, secret string) *MiniClient {
c := MiniClient{appId,secret}
return &c
}
\ No newline at end of file
package smapro package minipro
import ( import (
"encoding/json" "encoding/json"
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
// GetUserPhoneNumber 换取用户手机号 // GetUserPhoneNumber 换取用户手机号
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html // https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
func GetUserPhoneNumber(code string, accessToken string) (*PhoneNumberResponse, error) { func (c *MiniClient) GetUserPhoneNumber(code string, accessToken string) (*PhoneNumberResponse, error) {
result := &PhoneNumberResponse{} result := &PhoneNumberResponse{}
url := fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s", accessToken) url := fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s", accessToken)
bodyParam := phoneNumberParam{code} bodyParam := phoneNumberParam{code}
......
...@@ -2,26 +2,26 @@ package test ...@@ -2,26 +2,26 @@ package test
import ( import (
"fmt" "fmt"
"git.168cad.top/go/wechat-client-sdk/smapro" "git.168cad.top/go/wechat-client-sdk/minipro"
"log" "log"
"testing" "testing"
) )
func TestCode2Session(t *testing.T) { func TestCode2Session(t *testing.T) {
r,err:=smapro.Code2Session("033DEr0w36huoZ2Ga22w3wXeTR1DEr0g","","") r,err:= minipro.Code2Session("033DEr0w36huoZ2Ga22w3wXeTR1DEr0g","","")
log.Println(err) log.Println(err)
log.Println(fmt.Sprintf("%+v\n", *r)) log.Println(fmt.Sprintf("%+v\n", *r))
} }
func TestNewAccessToken(t *testing.T) { func TestNewAccessToken(t *testing.T) {
r,err := smapro.NewAccessToken("","") r,err := minipro.NewAccessToken("","")
log.Println(err) log.Println(err)
log.Println(fmt.Sprintf("%+v\n", *r)) log.Println(fmt.Sprintf("%+v\n", *r))
} }
func TestGetUserPhoneNumber(t *testing.T) { func TestGetUserPhoneNumber(t *testing.T) {
accessToken := "61_uqRWPGxxveyZnFoqCsddx9uY5w6uTZtHckodV8sS80OKtEBKJHM1udwD8ImlOa8UXnO_o3A4Qi3cwNtBvoC_fXSFnmXzJaD1cw-dMwaRGoz7XxTLhxvAeA_Bou4TWqPAmXnmmTknNwe9mU3_PVPiAGAZFZ" accessToken := "61_uqRWPGxxveyZnFoqCsddx9uY5w6uTZtHckodV8sS80OKtEBKJHM1udwD8ImlOa8UXnO_o3A4Qi3cwNtBvoC_fXSFnmXzJaD1cw-dMwaRGoz7XxTLhxvAeA_Bou4TWqPAmXnmmTknNwe9mU3_PVPiAGAZFZ"
r,err := smapro.GetUserPhoneNumber("fa46d5412a1c7053de2f930a043fa92b4d253eb83ce4e23d0fe0304bef65ded1", accessToken) r,err := minipro.GetUserPhoneNumber("fa46d5412a1c7053de2f930a043fa92b4d253eb83ce4e23d0fe0304bef65ded1", accessToken)
log.Println(err) log.Println(err)
log.Println(fmt.Sprintf("%+v\n", *r)) log.Println(fmt.Sprintf("%+v\n", *r))
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment