一.小程序端

1.index.wxml
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>
<button bindtap="sessionkey">获取sessionkey</button>
2.index.js
//index.js
//获取应用实例
const app = getApp()
Page({
data: {
sessionkey:''
},
getPhoneNumber (e) {
wx.request({
url: 'http://localhost:8080/getPhone',
data:{
vi:e.detail.iv,
sessionKey:this.data.sessionkey,
enData:e.detail.encryptedData
},
method:"get",
success:function(data){
console.log(data.data)
}
})
},
sessionkey(){
wx.login({
complete: (res) => {
var that=this;
wx.request({
url: 'http://localhost:8080/getCode',
data:{code: res.code},
method:'get',
success(data){
that.setData({
sessionkey:data.data
})
}
})
}
})
}
})
二.java后端
1.WxHttpUtils.java
package com.http.utils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class WxHttpUtils {
private static int socketTimeout = 10000;// 连接超时时间,默认10秒
private static int connectTimeout = 30000;// 传输超时时间,默认30秒
private static RequestConfig requestConfig;// 请求器的配置
private static CloseableHttpClient httpClient= HttpClients.custom().build();// HTTP请求器
/**
* @author huangfei
* get 请求
* @param url
* @param param
* @return String
*/
public static String sendGet(String url, String param) {
String result ="";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.connect();
Map<String, List<String>> map = connection.getHeaderFields();
for (String key : map.keySet()) {
}
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result+"==========");
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
2.WechatDecryptDataUtil.java工具类
package com.http.utils;/*
* @author allen
* @version v1.0
*/
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
public class WechatDecryptDataUtil {
private static final String KEY_ALGORITHM = "AES";
private static final String ALGORITHM_STR = "AES/CBC/PKCS7Padding";
private static Key key;
private static Cipher cipher;
/**
* 获取电话字符串
* @param encryptDataB64
* @param sessionKeyB64
* @param ivB64
* @return
*/
public static String decryptData(String encryptDataB64, String sessionKeyB64, String ivB64) {
return new String(decryptOfDiyIV(Base64.decode(encryptDataB64), Base64.decode(sessionKeyB64), Base64.decode(ivB64)));
}
/**
* 初始化数据
* @param keyBytes
*/
private static void init(byte[] keyBytes) {
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
Security.addProvider(new BouncyCastleProvider());
key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
try {
cipher = Cipher.getInstance(ALGORITHM_STR, "BC");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解密方法
*
* @param encryptedData 要解密的字符串
* @param keyBytes 解密密钥
* @param ivs 自定义对称解密算法初始向量 iv
* @return 解密后的字节数组
*/
private static byte[] decryptOfDiyIV(byte[] encryptedData, byte[] keyBytes, byte[] ivs) {
byte[] encryptedText = null;
init(keyBytes);
try {
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(ivs));
encryptedText = cipher.doFinal(encryptedData);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedText;
}
}
3.Controller
@RequestMapping("/getCode")
public String getCode(String code){
String params = "appid=" + WxPayConstant.APPID + "&secret=" + WxPayConstant.APPSECRET + "&js_code=" + code + "&grant_type=authorization_code";
String ss = WxHttpUtils.sendGet(WxPayConstant.WXAPPURL, params);
JSONObject SessionKeyOpenId = JSONObject.fromObject(ss);
String openid = SessionKeyOpenId.getString("session_key");
return openid;
}
@RequestMapping("/getPhone")
public String getPhone(String vi,String sessionKey,String enData){
String s = WechatDecryptDataUtil.decryptData(enData, sessionKey, vi);
JSONObject jsonObject = JSONObject.fromObject(s);
String data = jsonObject.getString("phoneNumber");
return data;
}