微信小程序獲取手機號的完整實例(Java后臺實現(xiàn))
小程序端:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
獲取手機號碼:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html
獲取token:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
現(xiàn)在的獲取手機號碼變得很簡單,不需要像之前那樣去根據(jù)偏移量解析密文了,現(xiàn)在直接使用code去微信后臺換取號碼即可,這里簡單記錄一下。
小程序
首先小程序端很簡單,直接調用API獲取code即可,然后將code作為參數(shù)傳遞給接口。
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
Page({ getPhoneNumber (e) { console.log(e.detail.code) } })
參數(shù) | 類型 | 說明 | 最低版本 |
---|---|---|---|
code | String | 動態(tài)令牌??赏ㄟ^動態(tài)令牌換取用戶手機號。使用方法詳情 phonenumber.getPhoneNumber 接口 |
后端接口
開發(fā)接口的時候需要注意以下幾點:
- 首先需要獲取一個access_token
- access_token是直接跟在url后面的,不需要作為參數(shù)處理
- code參數(shù)是json格式的
調用地址如下:
POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
請求參數(shù):
屬性 | 類型 | 默認值 | 必填 | 說明 |
---|---|---|---|---|
access_token / cloudbase_access_token | string | 是 | 接口調用憑證 | |
code | string | 是 | 手機號獲取憑證 |
了解了這些之后,我們就可以直接編寫我們的接口了。
@Autowired private RestTemplate restTemplate; @PostMapping("/wx/getPhone") public R getPhone(@RequestParam(value = "code", required = false) String code) { // 獲取token String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", WXConstant.APPID, WXConstant.SECRET); JSONObject token = JSON.parseObject(HttpUtil.get(token_url)); // 使用前端code獲取手機號碼 參數(shù)為json格式 String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + token.getString("access_token"); Map<String, String> paramMap = new HashMap<>(); paramMap.put("code", code); HttpHeaders headers = new HttpHeaders(); HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers); System.out.println(httpEntity); ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class); return R.ok().message("獲取手機號碼成功.").data(response.getBody()); }
這里獲取token的時候我是直接使用Hutool工具包提供的工具類開發(fā)的,大家可以自行引入,
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.16</version> </dependency>
然后,獲取手機號碼這里我是采用RestTemplate來調用的,相關配置文件如下:
@Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory() { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(5000);//ms factory.setConnectTimeout(15000);//ms return factory; } }
總結
到此這篇關于微信小程序獲取手機號的文章就介紹到這了,更多相關微信小程序獲取手機號內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Spring Cloud 跨服務數(shù)據(jù)聚合框架
這篇文章主要介紹了詳解Spring Cloud 跨服務數(shù)據(jù)聚合框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03java eclipse 整個項目或包查找只定字符串并替換操作
這篇文章主要介紹了java eclipse 整個項目或包查找只定字符串并替換操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09