123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package com.zoniot.ccrc.service;
- import com.alibaba.fastjson.JSONObject;
- import com.zoniot.ccrc.commom.exception.ServiceException;
- import com.zoniot.ccrc.commom.utils.HttpRequest;
- import lombok.extern.slf4j.Slf4j;
- import org.bouncycastle.jce.provider.BouncyCastleProvider;
- import org.bouncycastle.util.encoders.Base64;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import javax.crypto.BadPaddingException;
- import javax.crypto.Cipher;
- import javax.crypto.IllegalBlockSizeException;
- import javax.crypto.NoSuchPaddingException;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.math.BigInteger;
- import java.security.*;
- import java.security.spec.InvalidParameterSpecException;
- import java.util.Arrays;
- import java.util.List;
- @Slf4j
- @Service
- public class WechatService {
- //水查查小程序
- private static String APP_SECRET="a01d7f75f15e1a6b7fa28a317f97ef1b";
- private static String APP_ID="wx27f831675081e293" ;
- public static String getSessionkey(String code) {
- JSONObject oppidObj=getWechatAuthInfo(code);
- Integer errcode = (Integer) oppidObj.get("errcode");
- if(errcode == null){
- String openid = (String) oppidObj.get("openid");
- String session_key = (String) oppidObj.get("session_key");
- log.info("session_key:"+session_key);
- return session_key;
- }else {
- throw new ServiceException(-900,"获取session_key错误");
- }
- }
- private static JSONObject getWechatAuthInfo(String code){
- String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret="
- + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
- String reusult = null;
- try {
- reusult = HttpRequest.doGet(url);
- log.info(reusult);
- } catch (IOException e) {
- e.printStackTrace();
- }
- JSONObject oppidObj = JSONObject.parseObject(reusult);
- return oppidObj;
- }
- public static String getOpenid(String code,String customerId) {
- JSONObject oppidObj=getWechatAuthInfo(code);
- Integer errcode = (Integer) oppidObj.get("errcode");
- if(errcode == null){
- String openid = (String) oppidObj.get("openid");
- log.info("openid:"+openid);
- return openid;
- }else {
- throw new ServiceException(-900,"获取openid错误");
- }
- //return "ojYws5KUylI0muJdONaGaAUAJ_Xc";
- }
- public static String getInfo(String encryptedData, String iv, String sessionKey) {
- // 被加密的数据
- byte[] dataByte = Base64.decode(encryptedData);
- // 加密秘钥
- byte[] keyByte = Base64.decode(sessionKey);
- // 偏移量
- byte[] ivByte = Base64.decode(iv);
- try {
- // 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
- 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/PKCS7Padding","BC");
- 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");
- log.info("info:"+result);
- return JSONObject.parseObject(result).getString("purePhoneNumber");
- }
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- } catch (InvalidParameterSpecException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (InvalidAlgorithmParameterException e) {
- e.printStackTrace();
- } catch (NoSuchProviderException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
|