微信小程序授权手机号和解析手机号

1.wxml

<button class="warrant-btn ml-30 fl" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">微信授权</button>

2.js

getPhoneNumber: function (e) {
    var that = this
    //给定义参数赋值
    var ency = e.detail.encryptedData;
    var iv = e.detail.iv;
    var errMsg = e.detail.errMsg
    if (iv == null || ency == null) {
      wx.showToast({
        title: "授权失败,请重新授权!",
        icon: 'none',
      });
      return false
    }
    //调用手机号授权事件
    that.zhuc(ency, iv);
  },
  //调用手机号授权
  zhuc(ency, iv) {
    //从缓存中获取sessionkey
    var that = this//必须这么写不然返回参数接收不到
    var sessionKey = app.data.loginSessionKey;
    //判断登录状态
    wx.checkSession({
      success: function () {
        wx.request({
          url: urlList.getPhoneNumberUrl,
          data: {
            encryptedData: ency,//必须传
            iv: iv,//必须传
            sessionKey: sessionKey//必须传
          },
          method: "POST",
          header: { 'content-type': 'application/x-www-form-urlencoded' },
          success: (res) => {
            if (res.data.code === '201') {
             	console.log("解密成功");
             	//给全局变量赋值
             	app.data.studentPhone = res.data.data.model;//学员手机号
            }else{
              wx.showToast({
                title: '请求异常',
                icon: 'none',
                duration: 1000,
                mask: true
              })
            }
          }, 
          fail: function (err) {
            wx.showToast({
              title: '请求失败',
              icon: 'none',
              duration: 1000,
              mask: true
            })
          }
        });
      },
      fail: function () {//如果失败,就重新登录,并且重新获取手机号
      }
    })
  },

3.java后台

解析电话号码

引入架包

import org.bouncycastle.util.encoders.Base64;
import java.security.AlgorithmParameters;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.alibaba.fastjson.JSONObject;
**方法**
 public JSONObject getPhoneNumber(String session_key, String encryptedData, String iv) {
        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;
    }

4.pom.xml
//解密依赖包20190611

  <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.59</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

你可能感兴趣的:(微信小程序)