微信小程序獲取登錄手機號


小程序獲取登錄用戶手機號。

因為需要用戶主動觸發才能發起獲取手機號接口,所以該功能不由 API 來調用,需用 <button> 組件的點擊來觸發。

首先,放置一個 button 按鈕,將 buttonopen-type 的屬性值設為 getPhoneNumber 

當用戶點擊並通過之后,通過綁定的事件獲取微信服務器返回過來的加密數據,再根據 session_key 和 app_id 通過后台解密就可以獲取手機號啦。

說到這,就上碼吧!!!

 

 1 <!--index.wxml-->
 2 <view class="container">
 3   <view class="userinfo">
 4     <button wx:if="{{!hasUserInfo && canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 獲取頭像昵稱 </button>
 5     <block wx:else>
 6       <image bindtap="bindViewTap" class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
 7       <text class="userinfo-nickname">{{userInfo.nickName}}</text>
 8     </block>
 9   </view>
10   <view class="usermotto">
11     <text class="user-motto">{{motto}}</text>
12   </view>
13   <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">獲取手機號</button>
14 </view>

 

 

 

 js
 1  //index.js
 2  //獲取應用實例
 3  const app = getApp()
 4  
 5  Page({
 6    data: {
 7      motto: 'Hello World',
 8      userInfo: {},
 9      hasUserInfo: false,
10      canIUse: wx.canIUse('button.open-type.getUserInfo')
11    },
12    onLoad: function() {
13      
14    },
15    getPhoneNumber: function(e) {
16      console.log(e);
17      wx.request({
18        url:'http://host:port/local',//此處Ip就不暴露咯 19        data: {
20          "tel": e.detail,//微信小程序服務器返回的信息
21          "openId":"openId" //openId  注意此處的openId 是我們通過 code(用戶登錄憑證) 去后台獲取到的 openId
22        },
23        method: "GET",
24        dataType: "json",
25        success: function(result) {
26          //無區號的手機號
27          console.log(result.data+"-------手機號");
28        }
29      })
30    }
31  })

 

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");
  }
}

小編溫馨提示,獲取微信綁定手機號時需要通過短信驗證,不然是拿不到的!!

但是驗證之后如何取消授權,小編還未搞明白!!還請大神指教下

 

完美。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM