package services import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/http" "hongze/hongze_mfyx/utils" "io/ioutil" http2 "net/http" "strings" ) type WxAccessToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshToken string `json:"refresh_token"` Openid string `json:"openid"` Unionid string `json:"unionid"` Scope string `json:"scope"` Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` } type WxToken struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` } func WxGetToken() (item *WxToken, err error) { requestUrl := `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s` requestUrl = fmt.Sprintf(requestUrl, utils.WxAppId, utils.WxAppSecret) result, err := http.Get(requestUrl) if err != nil { utils.FileLog.Info("WxGetToken Result:%s ", string(result)) return nil, err } err = json.Unmarshal(result, &item) return } type WxUserInfo struct { Openid string `json:"openid"` Nickname string `json:"nickname"` Sex int `json:"sex"` Language string `json:"language"` City string `json:"city"` Province string `json:"province"` Country string `json:"country"` Headimgurl string `json:"headimgurl"` SubscribeTime int `json:"subscribe_time"` Unionid string `json:"unionid"` Remark string `json:"remark"` Groupid int `json:"groupid"` SubscribeScene string `json:"subscribe_scene"` Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` SessionKey string `json:"session_key"` } type WxUserDetail struct { Unionid string Headimgurl string Nickname string } type WxuserphonenumberResp struct { Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` PhoneInfo PhoneInfo `json:"phone_info"` } type PhoneInfo struct { PhoneNumber string `json:"phoneNumber"` PurePhoneNumber string `json:"purePhoneNumber"` CountryCode string `json:"countryCode"` Watermark Watermark `json:"watermark"` } type Watermark struct { Timestamp int64 `json:"timestamp"` Appid string `json:"appid"` } func Getuserphonenumber(code string) (item *WxuserphonenumberResp, err error) { url := "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=%s" itemToken, err := WxGetToken() if err != nil { fmt.Println(err) return } url = fmt.Sprintf(url, itemToken.AccessToken) method := "POST" payload := strings.NewReader(`{ "code":"` + code + `" }`) client := &http2.Client{} req, err := http2.NewRequest(method, url, payload) if err != nil { fmt.Println(err) return } req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close() body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) return } err = json.Unmarshal(body, &item) return }