赞
踩
最近在做小程序后端的时候,需要拿到手机号进行角色校验,小白也是第一次获取小程序的手机号,所以功能完毕后总结一下本次操作咯。
根据微信小程序官方文档:获取手机号 | 微信开放文档
调用的接口是getPhoneNumber
从伤处请求参数来看,我们要获取手机号,先得拿到该接口所需参数access_token。
下面看看如何拿到access_token?
看看文档
文档只有一点点啊,根本看不出来如何请求,后来看了网上的一些教程,才知道获取access_token又需要调一个接口
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s
参数:微信小程序的appId和appSecret
先根据appId,appSecret调用接口获取到access_token
//通过appid和secret来获取token String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId, appSecret); String accessToken = String.valueOf(JSON.parseObject(HttpUtil.get(tokenUrl)).get("access_token"));
然后再根据access_token,和前端生成的code来获取用户手机号就OK了。
//通过token和code来获取用户手机号 String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken + "&code=" + code; Map<String, String> paramMap = new HashMap<>(); paramMap.put("code", code); HttpHeaders headers = new HttpHeaders(); HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers); ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class); String body = String.valueOf(response.getBody());// 解析JSON字符串 log.info(body); // 定义一个正则表达式来匹配 phoneNumber Pattern pattern = Pattern.compile("phoneNumber=(\\d+)"); Matcher matcher = pattern.matcher(body); if (matcher.find()) { String phoneNumber = matcher.group(1); // 获取括号中的数字 log.info("phoneNumber:" + phoneNumber); return AjaxResult.success(phoneNumber); } else { return AjaxResult.error("手机号获取失败!"); }
注意:前端生成的code5分钟就过期了
下面附上完整的接口代码吧!!!!!!!!
由于这个controller里还有其他接口,所以这里把包和代码分开了。大概有个参考!
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson.JSON;
- import com.cvit.applet.Enum.AppletConstants;
- import com.cvit.applet.domain.StorageAccount;
- import com.cvit.applet.mapper.AccountMapper;
- import com.cvit.framework.core.domain.AjaxResult;
- import com.cvit.framework.utils.StringUtils;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.client.RestTemplate;
-
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
接口代码:
- @Value("${weixin.appId}")
- private String appId;
-
- @Value("${weixin.appSecret}")
- private String appSecret;
-
- @Autowired
- private RestTemplate restTemplate;
-
- @PostMapping("/login")
- public AjaxResult login(String code) {
- if (StringUtils.isEmpty(code)) {
- return AjaxResult.error("手机号获取凭证不能为空!");
- }
- //通过appid和secret来获取token
- String tokenUrl = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appId, appSecret);
- String accessToken = String.valueOf(JSON.parseObject(HttpUtil.get(tokenUrl)).get("access_token"));
- log.info(accessToken);
-
- //通过token和code来获取用户手机号
- String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + accessToken + "&code=" + code;
-
- Map<String, String> paramMap = new HashMap<>();
- paramMap.put("code", code);
- HttpHeaders headers = new HttpHeaders();
- HttpEntity<Map<String, String>> httpEntity = new HttpEntity<>(paramMap, headers);
- ResponseEntity<Object> response = restTemplate.postForEntity(url, httpEntity, Object.class);
- String body = String.valueOf(response.getBody());// 解析JSON字符串
- log.info(body);
- // 定义一个正则表达式来匹配 phoneNumber
- Pattern pattern = Pattern.compile("phoneNumber=(\\d+)");
- Matcher matcher = pattern.matcher(body);
- if (matcher.find()) {
- String phoneNumber = matcher.group(1); // 获取括号中的数字
- log.info("phoneNumber:" + phoneNumber);
- return AjaxResult.success(phoneNumber);
- } else {
- return AjaxResult.error("手机号获取失败!");
- }
- }
好了,大概可能估计也许就这些了,有想法的老师请多指点。。。。。。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。