Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
wechat-client-sdk
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
go
wechat-client-sdk
Commits
2eb7aa7e
Commit
2eb7aa7e
authored
Oct 14, 2022
by
yemin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no message
parent
4bbfcb3d
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
101 additions
and
28 deletions
+101
-28
ResponseMiniProgram.go
core/response/ResponseMiniProgram.go
+2
-2
HttpUtil.go
httpclient/HttpUtil.go
+3
-3
client.go
miniprogram/auth/client.go
+32
-9
responseCode2Session.go
miniprogram/auth/response/responseCode2Session.go
+0
-13
client.go
miniprogram/phonenumber/client.go
+48
-0
client_test.go
test/client_test.go
+16
-1
No files found.
core/response/ResponseMiniProgram.go
View file @
2eb7aa7e
package
response
package
response
type
ResponseMiniProgram
struct
{
type
MiniProgramResponse
struct
{
ErrCode
int
`json:"errcode"`
//错误码
ErrCode
int
`json:"errcode"`
//错误码
0成功,其他失败
ErrMSG
string
`json:"errmsg,omitempty"`
//错误信息
ErrMSG
string
`json:"errmsg,omitempty"`
//错误信息
}
}
httpclient/HttpUtil.go
View file @
2eb7aa7e
...
@@ -61,11 +61,11 @@ func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
...
@@ -61,11 +61,11 @@ func GetWithHeader(url string, header *map[string]string) ([]byte, error) {
// url 请求地址
// url 请求地址
// header header参数
// header header参数
// badyParam body参数
// badyParam body参数
func
Post
(
url
string
,
header
*
map
[
string
]
string
,
b
adyParam
*
interface
{})
([]
byte
,
error
)
{
func
Post
(
url
string
,
header
*
map
[
string
]
string
,
b
odyParam
interface
{})
([]
byte
,
error
)
{
//处理body参数
//处理body参数
var
bt
[]
byte
var
bt
[]
byte
if
b
a
dyParam
!=
nil
{
if
b
o
dyParam
!=
nil
{
b
,
err
:=
json
.
Marshal
(
*
ba
dyParam
)
b
,
err
:=
json
.
Marshal
(
bo
dyParam
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
...
miniprogram/auth/client.go
View file @
2eb7aa7e
...
@@ -3,22 +3,30 @@ package auth
...
@@ -3,22 +3,30 @@ package auth
import
(
import
(
"encoding/json"
"encoding/json"
"fmt"
"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/httpclient"
"git.168cad.top/go/wechat-client-sdk/miniprogram/auth/response"
)
)
type
Client
struct
{
// NewAccessToken 获取接口调用凭据。
// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getAccessToken.html
}
func
NewAccessToken
(
appId
string
,
secret
string
)
(
*
AccessTokenResponse
,
error
)
{
result
:=
&
AccessTokenResponse
{}
func
NewClient
()
*
Client
{
var
url
=
fmt
.
Sprintf
(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
,
appId
,
secret
)
return
&
Client
{}
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 登录凭证校验。
// 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
(
comp
*
Client
)
Code2Session
(
code
string
,
appId
string
,
secret
string
)
(
*
response
.
ResponseCode2Session
,
error
)
{
func
Code2Session
(
code
string
,
appId
string
,
secret
string
)
(
*
Code2SessionResponse
,
error
)
{
result
:=
&
response
.
ResponseCode2Session
{}
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"
,
appId
,
secret
,
code
)
r
,
err
:=
httpclient
.
Get
(
url
)
r
,
err
:=
httpclient
.
Get
(
url
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -30,3 +38,17 @@ func (comp *Client) Code2Session(code string, appId string, secret string) (*res
...
@@ -30,3 +38,17 @@ func (comp *Client) Code2Session(code string, appId string, secret string) (*res
}
}
return
result
,
nil
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
miniprogram/auth/response/responseCode2Session.go
deleted
100644 → 0
View file @
4bbfcb3d
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
}
miniprogram/phonenumber/client.go
0 → 100644
View file @
2eb7aa7e
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
test/client_test.go
View file @
2eb7aa7e
...
@@ -3,12 +3,26 @@ package test
...
@@ -3,12 +3,26 @@ package test
import
(
import
(
"fmt"
"fmt"
"git.168cad.top/go/wechat-client-sdk/miniprogram/auth"
"git.168cad.top/go/wechat-client-sdk/miniprogram/auth"
"git.168cad.top/go/wechat-client-sdk/miniprogram/phonenumber"
"log"
"log"
"testing"
"testing"
)
)
func
TestCode2Session
(
t
*
testing
.
T
)
{
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
(
err
)
log
.
Println
(
fmt
.
Sprintf
(
"%+v
\n
"
,
*
r
))
log
.
Println
(
fmt
.
Sprintf
(
"%+v
\n
"
,
*
r
))
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment