From 999adeb3ea735675f53092bfb0d490523f62223f Mon Sep 17 00:00:00 2001 From: Rick Date: Sun, 26 Dec 2021 20:33:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E7=A0=81=E6=96=B0=E7=9A=84=E8=8E=B7=E5=8F=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phonenumber/get_phone_number.go | 39 +++++++++++++++++++++++++++++++++ phonenumber/phonenumber.go | 19 ++++++++++++++++ weapp.go | 6 +++++ 3 files changed, 64 insertions(+) create mode 100644 phonenumber/get_phone_number.go create mode 100644 phonenumber/phonenumber.go diff --git a/phonenumber/get_phone_number.go b/phonenumber/get_phone_number.go new file mode 100644 index 0000000..b2d6c9d --- /dev/null +++ b/phonenumber/get_phone_number.go @@ -0,0 +1,39 @@ +package phonenumber + +import "github.com/medivhzhan/weapp/v3/request" + +const apiGetPhoneNumber = "/wxa/business/getuserphonenumber" + +type GetPhoneNumberRequest struct { + Code string `json:"code"` +} + +type GetPhoneNumberResponse struct { + request.CommonError + Data []struct { + PhoneNumber string `json:"phoneNumber"` // 用户绑定的手机号(国外手机号会有区号) + PurePhoneNumber string `json:"purePhoneNumber"` // 没有区号的手机号 + CountryCode string `json:"countryCode"` // 区号 + Watermark []struct { + Appid string `json:"appid"` // 小程序appid + Timestamp int64 `json:"timestamp"` // 用户获取手机号操作的时间戳 + } `json:"watermark"` // 数据水印 + } `json:"phone_info"` // 类目列表 +} + +// code换取用户手机号。 每个code只能使用一次,code的有效期为5min +func (cli *Phonenumber) GetPhoneNumber(req *GetPhoneNumberRequest) (*GetPhoneNumberResponse, error) { + + api, err := cli.conbineURI(apiGetPhoneNumber, nil, true) + if err != nil { + return nil, err + } + + res := new(GetPhoneNumberResponse) + err = cli.request.Post(api, req, res) + if err != nil { + return nil, err + } + + return res, nil +} diff --git a/phonenumber/phonenumber.go b/phonenumber/phonenumber.go new file mode 100644 index 0000000..7818d00 --- /dev/null +++ b/phonenumber/phonenumber.go @@ -0,0 +1,19 @@ +package phonenumber + +import "github.com/medivhzhan/weapp/v3/request" + +type Phonenumber struct { + request *request.Request + // 组成完整的 URL 地址 + // 默认包含 AccessToken + conbineURI func(url string, req interface{}, withToken bool) (string, error) +} + +func NewPhonenumber(request *request.Request, conbineURI func(url string, req interface{}, withToken bool) (string, error)) *Phonenumber { + sm := Phonenumber{ + request: request, + conbineURI: conbineURI, + } + + return &sm +} diff --git a/weapp.go b/weapp.go index b3d0108..3d8396b 100644 --- a/weapp.go +++ b/weapp.go @@ -12,6 +12,7 @@ import ( "github.com/medivhzhan/weapp/v3/logger" "github.com/medivhzhan/weapp/v3/ocr" "github.com/medivhzhan/weapp/v3/operation" + "github.com/medivhzhan/weapp/v3/phonenumber" "github.com/medivhzhan/weapp/v3/request" "github.com/medivhzhan/weapp/v3/search" "github.com/medivhzhan/weapp/v3/security" @@ -251,3 +252,8 @@ func (cli *Client) NewLiveBroadcast() *livebroadcast.LiveBroadcast { func (cli *Client) NewSecurity() *security.Security { return security.NewSecurity(cli.request, cli.conbineURI) } + +// 手机号 +func (cli *Client) NewPhonenumber() *phonenumber.Phonenumber { + return phonenumber.NewPhonenumber(cli.request, cli.conbineURI) +}