在小程序開發中,特別是一些電商平台,需要獲取用戶的手機號碼.
前端是無法直接獲取用戶手機號碼,前端需要用戶點擊同意授權後,把獲取到用戶的code傳給後端.後端收到code後,要先獲取一下token(獲取token的代碼,下次發).具體代碼如下(我是用golang原生寫的).
//獲取微信手機號碼func GetPhoneNumber(wxCode string) interface{} { //獲取用戶的token 明天發獲取token的代碼 accessToken := GetAccessToken() httpData := make(map[string]interface{}) httpData["code"] = wxCode httpDataCode, _ := json.Marshal(httpData) httpUrl := "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" accessToken client := &http.Client{} req, _ := http.NewRequest("POST", httpUrl, bytes.NewReader(httpDataCode)) req.Header.Add("content-type", "application/json") res, _ := client.Do(req) defer func() { _ = res.Body.Close() }() body, _ := ioutil.ReadAll(res.Body) fmt.Print(string(body)) var bodyData map[string]interface{} _ = json.Unmarshal(body, &bodyData) return bodyData["phone_info"].(interface{}).(map[string]interface{})["purePhoneNumber"]}注意!!! "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" accessToken token一定要拚接到url後麵,至於為啥,微信規定的哈.
本文到此結束,希望對大家有所幫助呢。
“第五套標準”IPO,僅剩5家排隊!兩家“0收入”!
獻禮70華誕:理想全員唱響《我和我的祖國》
自然陽光煥膚水潤時光麵膜 今天煥亮上市!
2024年百強房企銷售總額逾4.35萬億元 一線城市成交占比繼續提升
維持50年後,俄烏停止對歐天然氣供應,一個時代終結了
地方國資出手!增量資金即將入市
第二次互換便利操作開啟 備選機構範圍進一步擴大
金木扮靚京津冀產業大會 收獲來賓高度認可
AI戀愛應用 的生意經
蘋果開年迎降價:買iPhone 16 Pro最高立減800元