微信小程序获取手机号流程附代码-java

微信小程序获取手机号流程附代码-java

一、前端请求发送 携带 code 到后台换取 openid

微信小程序前端

wx.login({

      success(res) {
        if (res.code) {
          console.log(res.code)
          wx.request({
            url: 'http://localhost/index/users/code2seesion',
            method: 'GET',
            header: {},
            success(res) {
              console.log(res.data.openid);
              console.log(res.data.session_key);
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    })

后端逻辑
微信小程序获取手机号流程附代码-java_第1张图片
备注:这里前端把用户res.code返回到后端交互微信服务器,换取用户的openId,session_key参数

、前端获取到后端交互微信拿到的用户的openid以及session_key之后,存在前端,等待用户点击授权手机号


点击之后执行的前端代码

getPhoneNumber: function (e) {
    var that = this;
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: 'http://localhost/getUserTel',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          sessionKey: that.data.session_key,
        },
        method: "post",
        success: function (res) {
          console.log(res);
        }
      })
    }
  },

后端带着请求回来的三个参数进行解密,解密代码如下

public JSONObject getPhoneNumber(PhontVO phontVO){
        String encryptedData = phontVO.getEncryptedData();
        String iv = phontVO.getIv();
        String session_key = phontVO.getSessionKey();
        // 被加密的数据
        byte[] dataByte = Base64.decode(encryptedData);
        // 加密秘钥
        byte[] keyByte = Base64.decode(session_key);
        // 偏移量
        byte[] ivByte = Base64.decode(iv);
        try {
            int base = 16;
            if (keyByte.length % base != 0) {
                int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
                byte[] temp = new byte[groups * base];
                Arrays.fill(temp, (byte) 0);
                System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
                keyByte = temp;
            }
            // 初始化
            Security.addProvider(new BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));
            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, "UTF-8");
                return JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

最后返回

{
    "phoneNumber": "13580006666",
    "purePhoneNumber": "13580006666",
    "countryCode": "86",
    "watermark":
    {
        "appid":"APPID",
        "timestamp": TIMESTAMP
    }
}

你可能感兴趣的:(微信小程序,获取用户手机号,java,小程序,java)