Commit 2eb7aa7e by yemin

no message

parent 4bbfcb3d
package response
type ResponseMiniProgram struct {
type MiniProgramResponse struct {
ErrCode int `json:"errcode"` //错误码
ErrCode int `json:"errcode"` //错误码 0成功,其他失败
ErrMSG string `json:"errmsg,omitempty"`//错误信息
}
......@@ -61,11 +61,11 @@ func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
// url 请求地址
// header header参数
// badyParam body参数
func Post(url string, header *map[string]string, badyParam *interface{}) ([]byte, error) {
func Post(url string, header *map[string]string, bodyParam interface{}) ([]byte, error) {
//处理body参数
var bt []byte
if badyParam != nil {
b, err := json.Marshal(*badyParam)
if bodyParam != nil {
b, err := json.Marshal(bodyParam)
if err != nil {
return nil, err
}
......
......@@ -3,22 +3,30 @@ package auth
import (
"encoding/json"
"fmt"
"git.168cad.top/go/wechat-client-sdk/core/response"
"git.168cad.top/go/wechat-client-sdk/httpclient"
"git.168cad.top/go/wechat-client-sdk/miniprogram/auth/response"
)
type Client struct {
}
func NewClient() *Client {
return &Client{}
// NewAccessToken 获取接口调用凭据。
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
func NewAccessToken(appId string, secret string) (*AccessTokenResponse,error) {
result := &AccessTokenResponse{}
var url = fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",appId, secret)
r,err := httpclient.Get(url)
if err != nil{
return nil, err
}
e := json.Unmarshal(r, result)
if err != nil{
return nil, e
}
return result, nil
}
// Code2Session 登录凭证校验。
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
func (comp *Client) Code2Session(code string, appId string, secret string) (*response.ResponseCode2Session, error) {
result := &response.ResponseCode2Session{}
func Code2Session(code string, appId string, secret string) (*Code2SessionResponse, error) {
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)
r,err := httpclient.Get(url)
if err != nil{
......@@ -30,3 +38,17 @@ func (comp *Client) Code2Session(code string, appId string, secret string) (*res
}
return result, nil
}
type Code2SessionResponse struct {
OpenID string `json:"openid"` //用户唯一标识
SessionKey string `json:"session_key"` //会话密钥
UnionID string `json:"unionid"` //用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见 UnionID 机制说明。
response.MiniProgramResponse
}
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`//获取到的凭证
ExpiresIn int32 `json:"expires_in"`//凭证有效时间,单位:秒。目前是7200秒之内的值。
response.MiniProgramResponse
}
\ No newline at end of file
package response
import (
"git.168cad.top/go/wechat-client-sdk/core/response"
)
type ResponseCode2Session struct {
OpenID string `json:"openid"` //用户唯一标识
SessionKey string `json:"session_key"` //会话密钥
UnionID string `json:"unionid"` //用户在开放平台的唯一标识符,若当前小程序已绑定到微信开放平台帐号下会返回,详见 UnionID 机制说明。
response.ResponseMiniProgram
}
package phonenumber
import (
"encoding/json"
"fmt"
"git.168cad.top/go/wechat-client-sdk/core/response"
"git.168cad.top/go/wechat-client-sdk/httpclient"
)
// GetUserPhoneNumber 换取用户手机号
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-info/phone-number/getPhoneNumber.html
func GetUserPhoneNumber(code string, accessToken string) (*PhoneNumberResponse, error) {
result := &PhoneNumberResponse{}
url := fmt.Sprintf("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s", accessToken)
bodyParam := phoneNumberParam{code}
bs,err := httpclient.Post(url,nil, bodyParam)
if err != nil {
return result, err
}
if err1 := json.Unmarshal(bs, result);err1 !=nil{
return result, err1
}
return result, nil
}
type phoneNumberParam struct {
Code string `json:"code"`
}
type PhoneNumberResponse struct {
response.MiniProgramResponse
PhoneInfo PhoneInfoResponse `json:"phone_info"`
}
type PhoneInfoResponse struct {
PhoneNumber string `json:"phoneNumber"`
PurePhoneNumber string `json:"purePhoneNumber"`
CountryCode string `json:"countryCode"`
Watermark WatermarkResponse `json:"watermark"`
}
type WatermarkResponse struct {
Timestamp int32 `json:"timestamp"`
Appid string `json:"appid"`
}
\ No newline at end of file
......@@ -3,12 +3,26 @@ package test
import (
"fmt"
"git.168cad.top/go/wechat-client-sdk/miniprogram/auth"
"git.168cad.top/go/wechat-client-sdk/miniprogram/phonenumber"
"log"
"testing"
)
func TestCode2Session(t *testing.T) {
r,err:=auth.NewClient().Code2Session("ffdsfadfdfdas","","")
r,err:=auth.Code2Session("033DEr0w36huoZ2Ga22w3wXeTR1DEr0g","","")
log.Println(err)
log.Println(fmt.Sprintf("%+v\n", *r))
}
func TestNewAccessToken(t *testing.T) {
r,err := auth.NewAccessToken("","")
log.Println(err)
log.Println(fmt.Sprintf("%+v\n", *r))
}
func TestGetUserPhoneNumber(t *testing.T) {
accessToken := "61_uqRWPGxxveyZnFoqCsddx9uY5w6uTZtHckodV8sS80OKtEBKJHM1udwD8ImlOa8UXnO_o3A4Qi3cwNtBvoC_fXSFnmXzJaD1cw-dMwaRGoz7XxTLhxvAeA_Bou4TWqPAmXnmmTknNwe9mU3_PVPiAGAZFZ"
r,err := phonenumber.GetUserPhoneNumber("fa46d5412a1c7053de2f930a043fa92b4d253eb83ce4e23d0fe0304bef65ded1", accessToken)
log.Println(err)
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