package com.stylefeng.guns.modular.system.util;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import org.apache.commons.codec.binary.Base64;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.client.RestTemplate;
|
|
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.UnsupportedEncodingException;
|
import java.security.InvalidAlgorithmParameterException;
|
import java.security.InvalidKeyException;
|
import java.security.NoSuchAlgorithmException;
|
import java.security.spec.AlgorithmParameterSpec;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* 微信工具类
|
*/
|
@Component
|
public class WeChatUtil {
|
|
@Value("${wx.appletsAppid}")
|
private String wxAppletsAppid;
|
|
@Value("${wx.appletsAppSecret}")
|
private String wxAppletsAppSecret;
|
|
@Autowired
|
private RestTemplate restTemplate;
|
|
|
/**
|
* 小程序使用jscode获取openid
|
* @param jscode
|
* @return
|
*/
|
public Map<String, String> code2Session(String jscode){
|
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxAppletsAppid + "&secret=" + wxAppletsAppSecret
|
+ "&js_code=" + jscode + "&grant_type=authorization_code";
|
String forObject = restTemplate.getForObject(url, String.class);
|
JSONObject jsonObject = JSON.parseObject(forObject);
|
int errcode = jsonObject.getIntValue("errcode");
|
Map<String, String> map = new HashMap<>();
|
if(errcode == 0){//成功
|
map.put("openid", jsonObject.getString("openid"));
|
map.put("sessionKey", jsonObject.getString("session_key"));
|
map.put("unionid", jsonObject.getString("unionid"));
|
return map;
|
}
|
if(errcode == -1){//系统繁忙,此时请开发者稍候再试
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 40029){//code 无效
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
if(errcode == 45011){//频率限制,每个用户每分钟100次
|
map.put("msg", jsonObject.getString("errmsg"));
|
return map;
|
}
|
return null;
|
}
|
/**
|
* AES解密
|
*
|
* @param data //密文,被加密的数据
|
* @param key //秘钥
|
* @param iv //偏移量
|
* @return
|
* @throws Exception
|
*/
|
public static String decrypt1(String data, String key,String iv){
|
//被加密的数据
|
byte[] dataByte = Base64.decodeBase64(data);
|
//加密秘钥
|
byte[] keyByte = Base64.decodeBase64(key);
|
//偏移量
|
byte[] ivByte = Base64.decodeBase64(iv);
|
try {
|
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivByte);
|
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
|
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
|
return new String(cipher.doFinal(dataByte),"UTF-8");
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
} catch (NoSuchPaddingException e) {
|
e.printStackTrace();
|
} catch (InvalidKeyException e) {
|
e.printStackTrace();
|
} catch (InvalidAlgorithmParameterException e) {
|
e.printStackTrace();
|
} catch (IllegalBlockSizeException e) {
|
e.printStackTrace();
|
} catch (BadPaddingException e) {
|
e.printStackTrace();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
}
|