微信小程序获取登录手机号

2023-02-20,,

小程序获取登录用户手机号

因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 <button> 组件的点击来触发。

首先,放置一个 button 按钮,将 button 的 open-type 的属性值设为 getPhoneNumber 。

当用户点击并通过之后,通过绑定的事件获取微信服务器返回过来的加密数据,再根据 session_key 和 app_id 通过后台解密就可以获取手机号啦。

说到这,就上码吧!!!

 <!--index.wxml-->
<view class="container">
<view class="userinfo">
<button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<text class="user-motto">{{motto}}</text>
</view>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
</view>

 js

  //index.js
//获取应用实例
const app = getApp() Page({
data: {
motto: 'Hello World',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function() { },
getPhoneNumber: function(e) {
console.log(e);
wx.request({
url:'http://host:port/local',//此处Ip就不暴露咯
data: {
"tel": e.detail,//微信小程序服务器返回的信息
"openId":"openId" //openId 注意此处的openId 是我们通过 code(用户登录凭证) 去后台获取到的 openId
},
method: "GET",
dataType: "json",
success: function(result) {
//无区号的手机号
console.log(result.data+"-------手机号");
}
})
}
})
package cn.byzt.controller;

import cn.byzt.service.impl.WeChatService;
import cn.byzt.util.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /************************
*author : Damon
*createTime : 2018/12/11
*微信相关操作
************************/
@Api(tags = "微信相关操作")
@RestController
@RequestMapping("/we/chat")
public class WeChatController { @Autowired
private WeChatService service; @ApiOperation("将微信获取的加密手机号解密")
@GetMapping("/decrypt/tel")
public String decryptTel(@ApiParam("微信小程序授权之后获取电话(加密字符串,json对象)") @RequestParam("tel") String tel,
@ApiParam("openid") @RequestParam("openId") String openId) {
return service.decryptTel(tel, openId);
} }
package cn.byzt.service.impl;

import cn.byzt.util.AesUtil;
import cn.byzt.util.Constants;
import cn.byzt.util.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; /************************
*author : Damon
*createTime : 2018/12/11
*微信相关操作业务实现
************************/
@Service
public class WeChatService { @Autowired
private Gson gson;
   @Autowired
   private StringRedisTemplate redisTemplate;
/** * 解密手机号(微信返回的) * * @param tel * @return */ 
  public String decryptTel(String tel, String openId) {
    Map<String, String> map = gson.fromJson(tel, Map.class);
    map = gson.fromJson(AesUtil.aes_decrypt_cbc(Base64.getDecoder().decode(map.get("encryptedData")), Base64.getDecoder().decode(redisTemplate.opsForHash().get(openId, "session_key").toString()), Base64.getDecoder().decode(map.get("iv"))), Map.class); return map.get("purePhoneNumber");
  }
}

小编温馨提示,获取微信绑定手机号时需要通过短信验证,不然是拿不到的!!

但是验证之后如何取消授权,小编还未搞明白!!还请大神指教下

完美。

微信小程序获取登录手机号的相关教程结束。

《微信小程序获取登录手机号.doc》

下载本文的Word格式文档,以方便收藏与打印。