Skip to content

Commit

Permalink
增加手机号码新的获取接口
Browse files Browse the repository at this point in the history
  • Loading branch information
royalmorty committed Dec 26, 2021
1 parent c793df4 commit 999adeb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
39 changes: 39 additions & 0 deletions phonenumber/get_phone_number.go
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions phonenumber/phonenumber.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions weapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit 999adeb

Please sign in to comment.