WeChatInfoService.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package com.ygj.yuemum.service.global;
  2. import com.alibaba.fastjson.JSON;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.ygj.yuemum.component.Constant;
  5. import com.ygj.yuemum.dao.global.MiniAppInfoDao;
  6. import com.ygj.yuemum.dao.global.WeChatInfoDao;
  7. import com.ygj.yuemum.domain.customer.CustomerBooking;
  8. import com.ygj.yuemum.domain.global.MiniAppInfo;
  9. import com.ygj.yuemum.domain.global.TemplateData;
  10. import com.ygj.yuemum.domain.global.WeChatInfo;
  11. import com.ygj.yuemum.domain.global.WechatTemplate;
  12. import com.ygj.yuemum.domain.wxmini.WXUser;
  13. import com.ygj.yuemum.service.wxmini.WXUserService;
  14. import org.apache.commons.codec.binary.Base64;
  15. import org.apache.http.HttpStatus;
  16. import org.apache.http.client.methods.CloseableHttpResponse;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.entity.ContentType;
  19. import org.apache.http.entity.StringEntity;
  20. import org.apache.http.impl.client.CloseableHttpClient;
  21. import org.apache.http.impl.client.HttpClients;
  22. import org.apache.http.util.EntityUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.stereotype.Service;
  25. import javax.crypto.BadPaddingException;
  26. import javax.crypto.Cipher;
  27. import javax.crypto.IllegalBlockSizeException;
  28. import javax.crypto.NoSuchPaddingException;
  29. import javax.crypto.spec.IvParameterSpec;
  30. import javax.crypto.spec.SecretKeySpec;
  31. import java.io.BufferedReader;
  32. import java.io.IOException;
  33. import java.io.InputStreamReader;
  34. import java.io.UnsupportedEncodingException;
  35. import java.net.URL;
  36. import java.net.URLConnection;
  37. import java.security.*;
  38. import java.security.spec.InvalidParameterSpecException;
  39. import java.text.SimpleDateFormat;
  40. import java.util.*;
  41. @Service
  42. public class WeChatInfoService {
  43. @Autowired
  44. private WeChatInfoDao weChatInfoDao;
  45. @Autowired
  46. private MiniAppInfoDao miniAppInfoDao;
  47. @Autowired
  48. private WXUserService wxUserService;
  49. public String APPID = Constant.APPID;
  50. public String APPSECRET = Constant.APPSECRET;
  51. public String MINIAPPID = Constant.MINIAPPID;
  52. public String MINIAPPSECRET = Constant.MINIAPPSECRET;
  53. public String GRANT_TYPE = Constant.GRANT_TYPE;
  54. public String JSAPI_TICKET_TYPE = Constant.JSAPI_TICKET_TYPE;
  55. public String ACCESS_TOKEN_URL = Constant.ACCESS_TOKEN_URL;
  56. public String JSAPI_TICKET_URL = Constant.JSAPI_TICKET_URL;
  57. public String JSCODE2SESSION = Constant.JSCODE2SESSION;
  58. public String SENDMESSAGEURL = Constant.SENDMESSAGEURL;
  59. public String OrderTemplateid = Constant.ORDERTEMPLATEID;
  60. public String BookingTemplateid = Constant.BOOKINGTEMPLATEID;
  61. public String ExtractTemplateid = Constant.EXTRACTTEMPLATEID;
  62. public String WPOPENID = Constant.WPOPENID;
  63. private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
  64. String access_Token;
  65. String expires_in;
  66. String jsapi_Ticket;
  67. String noncestr;
  68. String timeStamp;
  69. String signaTure;
  70. String session_key;
  71. String openid;
  72. public int WXSendExtractMessage(String wue_applysum, String wue_phone, String wue_bank_card) throws IOException {
  73. ObjectMapper mapper = new ObjectMapper();
  74. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  75. // String resultToken = sendGet("http://yuesuo.yueguanjia.com/modoo-1/getWxPublicSender");
  76. // Map mapToken = JSON.parseObject(resultToken);
  77. // String openid = String.valueOf(mapToken.get("wp_openid"));
  78. Map<String,TemplateData> mapdata = new HashMap<>();
  79. WechatTemplate wechatTemplate = new WechatTemplate();
  80. wechatTemplate.setTemplate_id(ExtractTemplateid);
  81. wechatTemplate.setTouser(WPOPENID);
  82. // 封装模板数据
  83. TemplateData first = new TemplateData();
  84. first.setValue("介绍人发送提现申请!");
  85. first.setColor("#173177");
  86. mapdata.put("first", first);
  87. TemplateData keyword1 = new TemplateData();
  88. keyword1.setValue(wue_applysum);
  89. keyword1.setColor("#fc6961");
  90. mapdata.put("keyword1", keyword1);
  91. TemplateData keyword2 = new TemplateData();
  92. keyword2.setValue(sdf.format(new Date()));
  93. keyword2.setColor("#173177");
  94. mapdata.put("keyword2", keyword2);
  95. TemplateData keyword5 = new TemplateData();
  96. keyword5.setValue("手机号:" +wue_phone+ ",银行卡号:" +wue_bank_card+"。请尽快处理!");
  97. keyword5.setColor("#173177");
  98. mapdata.put("remark", keyword5);
  99. wechatTemplate.setData(mapdata);
  100. String toString = mapper.writeValueAsString(wechatTemplate);
  101. String result = "";
  102. CloseableHttpClient httpClient = HttpClients.createDefault();
  103. WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
  104. HttpPost httpPost = new HttpPost(SENDMESSAGEURL+weChatInfo.getAccess_token());
  105. StringEntity stringEntity = new StringEntity(toString, ContentType.APPLICATION_JSON);
  106. stringEntity.setContentEncoding("utf-8");
  107. httpPost.setEntity(stringEntity);
  108. CloseableHttpResponse response = httpClient.execute(httpPost);
  109. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  110. result = EntityUtils.toString(response.getEntity(), "utf-8");
  111. } else {
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. public int WXSendOrderMessage(String orderno, String total, String date) throws IOException {
  117. ObjectMapper mapper = new ObjectMapper();
  118. // String resultToken = sendGet("http://yuesuo.yueguanjia.com/modoo-1/getWxPublicSender");
  119. // Map mapToken = JSON.parseObject(resultToken);
  120. // String openid = String.valueOf(mapToken.get("wp_openid"));
  121. Map<String,TemplateData> mapdata = new HashMap<>();
  122. WechatTemplate wechatTemplate = new WechatTemplate();
  123. wechatTemplate.setTemplate_id(OrderTemplateid);
  124. wechatTemplate.setTouser(WPOPENID);
  125. // 封装模板数据
  126. TemplateData first = new TemplateData();
  127. first.setValue("用户设备租赁单在线支付成功!");
  128. first.setColor("#173177");
  129. mapdata.put("first", first);
  130. TemplateData keyword1 = new TemplateData();
  131. keyword1.setValue(orderno);
  132. keyword1.setColor("#fc6961");
  133. mapdata.put("keyword1", keyword1);
  134. TemplateData keyword2 = new TemplateData();
  135. keyword2.setValue(total);
  136. keyword2.setColor("#173177");
  137. mapdata.put("keyword2", keyword2);
  138. TemplateData keyword3 = new TemplateData();
  139. keyword3.setValue(date);
  140. keyword3.setColor("#173177");
  141. mapdata.put("keyword3", keyword3);
  142. TemplateData keyword5 = new TemplateData();
  143. keyword5.setValue("请尽快完善订单及时发货");
  144. keyword5.setColor("#173177");
  145. mapdata.put("remark", keyword5);
  146. wechatTemplate.setData(mapdata);
  147. String toString = mapper.writeValueAsString(wechatTemplate);
  148. String result = "";
  149. CloseableHttpClient httpClient = HttpClients.createDefault();
  150. WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
  151. HttpPost httpPost = new HttpPost(SENDMESSAGEURL+weChatInfo.getAccess_token());
  152. StringEntity stringEntity = new StringEntity(toString, ContentType.APPLICATION_JSON);
  153. stringEntity.setContentEncoding("utf-8");
  154. httpPost.setEntity(stringEntity);
  155. CloseableHttpResponse response = httpClient.execute(httpPost);
  156. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  157. result = EntityUtils.toString(response.getEntity(), "utf-8");
  158. } else {
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. public int WXSendYSOrderMessage(String orderno, String total, String date,String type) throws IOException {
  164. ObjectMapper mapper = new ObjectMapper();
  165. // String resultToken = sendGet("http://yuesuo.yueguanjia.com/modoo-1/getWxPublicSender");
  166. // Map mapToken = JSON.parseObject(resultToken);
  167. // String openid = String.valueOf(mapToken.get("wp_openid"));
  168. Map<String,TemplateData> mapdata = new HashMap<>();
  169. WechatTemplate wechatTemplate = new WechatTemplate();
  170. wechatTemplate.setTemplate_id(OrderTemplateid);
  171. wechatTemplate.setTouser(WPOPENID);
  172. // 封装模板数据
  173. TemplateData first = new TemplateData();
  174. if (type.equals("2")) {
  175. first.setValue("用户支付订单首付款成功!");
  176. first.setColor("#173177");
  177. } else if (type.equals("3")) {
  178. first.setValue("用户支付订单尾款成功!");
  179. first.setColor("#173177");
  180. }
  181. mapdata.put("first", first);
  182. TemplateData keyword1 = new TemplateData();
  183. keyword1.setValue(orderno);
  184. keyword1.setColor("#fc6961");
  185. mapdata.put("keyword1", keyword1);
  186. TemplateData keyword2 = new TemplateData();
  187. keyword2.setValue(total);
  188. keyword2.setColor("#173177");
  189. mapdata.put("keyword2", keyword2);
  190. TemplateData keyword3 = new TemplateData();
  191. keyword3.setValue(date);
  192. keyword3.setColor("#173177");
  193. mapdata.put("keyword3", keyword3);
  194. TemplateData keyword5 = new TemplateData();
  195. keyword5.setValue("请尽快安排顾问跟进!");
  196. keyword5.setColor("#173177");
  197. mapdata.put("remark", keyword5);
  198. wechatTemplate.setData(mapdata);
  199. String toString = mapper.writeValueAsString(wechatTemplate);
  200. String result = "";
  201. CloseableHttpClient httpClient = HttpClients.createDefault();
  202. WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
  203. HttpPost httpPost = new HttpPost(SENDMESSAGEURL+weChatInfo.getAccess_token());
  204. StringEntity stringEntity = new StringEntity(toString, ContentType.APPLICATION_JSON);
  205. stringEntity.setContentEncoding("utf-8");
  206. httpPost.setEntity(stringEntity);
  207. CloseableHttpResponse response = httpClient.execute(httpPost);
  208. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  209. result = EntityUtils.toString(response.getEntity(), "utf-8");
  210. } else {
  211. return 0;
  212. }
  213. return 1;
  214. }
  215. public int WXSendBookingMessage(CustomerBooking customerBooking) throws IOException {
  216. ObjectMapper mapper = new ObjectMapper();
  217. // String resultToken = sendGet("http://yuesuo.yueguanjia.com/modoo-1/getWxPublicSender");
  218. // Map mapToken = JSON.parseObject(resultToken);
  219. // String openid = String.valueOf(mapToken.get("wp_openid"));
  220. WechatTemplate wechatTemplate = new WechatTemplate();
  221. wechatTemplate.setTemplate_id(BookingTemplateid);
  222. wechatTemplate.setTouser(WPOPENID);
  223. Map<String,TemplateData> mapdata = new HashMap<>();
  224. // 封装模板数据
  225. TemplateData first = new TemplateData();
  226. if(customerBooking.getBk_type().equals(4) && customerBooking.getBk_introducer_phone() != null && customerBooking.getBk_introducer_phone() != ""){
  227. first.setValue("顾问预约信息!介绍人手机:"+customerBooking.getBk_introducer_phone()+",介绍人顾问:"+customerBooking.getBk_introducer_consultant()+"!");
  228. first.setColor("#173177");
  229. } else if(customerBooking.getBk_type().equals(1) && customerBooking.getBk_introducer_phone() != null && customerBooking.getBk_introducer_phone() != "") {
  230. first.setValue("套餐预约信息!介绍人手机:"+customerBooking.getBk_introducer_phone()+",介绍人顾问:"+customerBooking.getBk_introducer_consultant()+"!");
  231. first.setColor("#173177");
  232. } else {
  233. first.setValue("预约信息!");
  234. first.setColor("#173177");
  235. }
  236. mapdata.put("first", first);
  237. TemplateData keyword2 = new TemplateData();
  238. keyword2.setValue(customerBooking.getBk_phone());
  239. keyword2.setColor("#fc6961");
  240. mapdata.put("keyword2", keyword2);
  241. TemplateData keyword3 = new TemplateData();
  242. keyword3.setValue(customerBooking.getBk_date());
  243. keyword3.setColor("#173177");
  244. mapdata.put("keyword3", keyword3);
  245. if(customerBooking.getBk_type().equals(1)){
  246. TemplateData keyword4 = new TemplateData();
  247. keyword4.setValue("套餐预约,套餐名称:"+customerBooking.getBk_desc());
  248. keyword4.setColor("#173177");
  249. mapdata.put("keyword4", keyword4);
  250. }
  251. if(customerBooking.getBk_type().equals(4)){
  252. TemplateData keyword4 = new TemplateData();
  253. keyword4.setValue("顾问预约,"+"预产期:"+customerBooking.getBk_duedate());
  254. keyword4.setColor("#173177");
  255. mapdata.put("keyword4", keyword4);
  256. }
  257. if(customerBooking.getBk_type().equals(5)){
  258. TemplateData keyword4 = new TemplateData();
  259. keyword4.setValue("设备返还预约,租赁单编号:"+customerBooking.getBk_desc());
  260. keyword4.setColor("#173177");
  261. mapdata.put("keyword4", keyword4);
  262. }
  263. TemplateData keyword5 = new TemplateData();
  264. keyword5.setValue("请尽快安排跟进!");
  265. keyword5.setColor("#173177");
  266. mapdata.put("remark", keyword5);
  267. wechatTemplate.setData(mapdata);
  268. String toString = mapper.writeValueAsString(wechatTemplate);
  269. String result = "";
  270. CloseableHttpClient httpClient = HttpClients.createDefault();
  271. WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
  272. HttpPost httpPost = new HttpPost(SENDMESSAGEURL+weChatInfo.getAccess_token());
  273. StringEntity stringEntity = new StringEntity(toString, ContentType.APPLICATION_JSON);
  274. stringEntity.setContentEncoding("utf-8");
  275. httpPost.setEntity(stringEntity);
  276. CloseableHttpResponse response = httpClient.execute(httpPost);
  277. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  278. result = EntityUtils.toString(response.getEntity(), "utf-8");
  279. } else {
  280. return 0;
  281. }
  282. return 1;
  283. }
  284. public int SaveWeChatInfo() {
  285. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  286. try {
  287. //access_token
  288. String resultToken = sendGet(ACCESS_TOKEN_URL + "?grant_type=" + GRANT_TYPE + "&appid=" + APPID + "&secret=" + APPSECRET);
  289. Map mapToken = JSON.parseObject(resultToken);
  290. access_Token = String.valueOf(mapToken.get("access_token"));
  291. expires_in = String.valueOf(mapToken.get("expires_in"));
  292. //get jsApi_ticket
  293. String resultJsapi = sendGet(JSAPI_TICKET_URL + "?access_token=" + access_Token + "&type=" + JSAPI_TICKET_TYPE);
  294. Map mapJsapi = JSON.parseObject(resultJsapi);
  295. jsapi_Ticket = String.valueOf(mapJsapi.get("ticket"));
  296. //保存到数据库中
  297. WeChatInfo weChatInfo = new WeChatInfo();
  298. weChatInfo.setAccess_token(access_Token);
  299. weChatInfo.setJsapi_ticket(jsapi_Ticket);
  300. weChatInfo.setUpdate_time(dateFormat.format(new Date()));
  301. weChatInfoDao.updateWeChatInfo(weChatInfo);
  302. } catch (Exception ex) {
  303. ex.printStackTrace();
  304. return 0;
  305. }
  306. return 1;
  307. }
  308. public int SaveMiniAppInfo() {
  309. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  310. try {
  311. //access_token
  312. String resultToken = sendGet(ACCESS_TOKEN_URL + "?grant_type=" + GRANT_TYPE + "&appid=" + MINIAPPID + "&secret=" + MINIAPPSECRET);
  313. Map mapToken = JSON.parseObject(resultToken);
  314. access_Token = String.valueOf(mapToken.get("access_token"));
  315. expires_in = String.valueOf(mapToken.get("expires_in"));
  316. //保存到数据库中
  317. MiniAppInfo miniAppInfo = new MiniAppInfo();
  318. miniAppInfo.setAccess_token(access_Token);
  319. miniAppInfo.setJsapi_ticket(jsapi_Ticket);
  320. miniAppInfo.setUpdate_time(dateFormat.format(new Date()));
  321. miniAppInfoDao.updateMiniAppInfo(miniAppInfo);
  322. } catch (Exception ex) {
  323. ex.printStackTrace();
  324. return 0;
  325. }
  326. return 1;
  327. }
  328. public Map<String, String> getWXOpenid(String js_code) {
  329. Map<String, String> result = new HashMap<>();
  330. String resultToken = sendGet(JSCODE2SESSION + "?grant_type=authorization_code" + "&appid=" + MINIAPPID + "&secret=" + MINIAPPSECRET + "&js_code=" + js_code);
  331. Map mapToken = JSON.parseObject(resultToken);
  332. session_key = String.valueOf(mapToken.get("session_key"));
  333. openid = String.valueOf(mapToken.get("openid"));
  334. //save database
  335. //检查用户是否已经注册,注册返回手机号等信息,没注册返回openid作为用户名
  336. //新用户 insert ,没有手机返回openid 有手机返回手机号等信息
  337. WXUser ckeckwxUser = wxUserService.getWXUser(openid);
  338. if(ckeckwxUser != null) {
  339. result.put("openid", openid);
  340. result.put("type","2");
  341. result.put("nickName",ckeckwxUser.getXu_name());
  342. result.put("phone",ckeckwxUser.getXu_phone());
  343. } else {
  344. WXUser wxUser = new WXUser();
  345. wxUser.setXu_openid(openid);
  346. wxUser.setXu_sessionkey(session_key);
  347. UUID id=UUID.randomUUID();
  348. String[] idd=id.toString().split("-");
  349. String lsid = "YSLS"+idd[1];
  350. wxUser.setXu_name(lsid);
  351. wxUserService.insertWXUser(wxUser);
  352. result.put("openid", openid);
  353. result.put("type","1");
  354. result.put("nickName",lsid);
  355. }
  356. result.put("session_key", session_key);
  357. return result;
  358. }
  359. public Map<String, String> getWeChatInfo(String url) {
  360. WeChatInfo weChatInfo = weChatInfoDao.getWeChatInfo();
  361. Map<String, String> result = new HashMap<>();
  362. String timestamp = getTimestamp();
  363. String noncestr = getNoncestr();
  364. String signature = SHA1(jointString(weChatInfo.getJsapi_ticket(), noncestr, timestamp, url));
  365. result.put("timestamp", timestamp);
  366. result.put("nonceStr", noncestr);
  367. result.put("signature", signature);
  368. result.put("appid", APPID);
  369. result.put("jsapi_ticket", weChatInfo.getJsapi_ticket());
  370. return result;
  371. }
  372. //-----------------------------------
  373. public static String jointString(String jsapi_Ticket, String noncestr, String timestamp, String url) {
  374. String sb = new String();
  375. sb += "jsapi_ticket=" + jsapi_Ticket + "&";
  376. sb += "noncestr=" + noncestr + "&";
  377. sb += "timestamp=" + timestamp + "&";
  378. sb += "url=" + url;
  379. return sb;
  380. }
  381. public static String SHA1(String str) {
  382. try {
  383. MessageDigest digest = java.security.MessageDigest
  384. .getInstance("SHA-1"); //如果是SHA加密只需要将"SHA-1"改成"SHA"即可
  385. digest.update(str.getBytes());
  386. byte messageDigest[] = digest.digest();
  387. // Create Hex String
  388. StringBuffer hexStr = new StringBuffer();
  389. // 字节数组转换为 十六进制 数
  390. for (int i = 0; i < messageDigest.length; i++) {
  391. String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
  392. if (shaHex.length() < 2) {
  393. hexStr.append(0);
  394. }
  395. hexStr.append(shaHex);
  396. }
  397. return hexStr.toString();
  398. } catch (NoSuchAlgorithmException e) {
  399. e.printStackTrace();
  400. }
  401. return null;
  402. }
  403. public static String getTimestamp() {
  404. return Long.toString(System.currentTimeMillis() / 1000);
  405. }
  406. public static String getNoncestr() {
  407. return UUID.randomUUID().toString();
  408. }
  409. public static String sendGet(String url) {
  410. String result = "";
  411. StringBuilder jsonStr = new StringBuilder();
  412. BufferedReader in = null;
  413. try {
  414. String urlNameString = url;
  415. URL realUrl = new URL(urlNameString);
  416. // 打开和URL之间的连接
  417. URLConnection connection = realUrl.openConnection();
  418. // 设置通用的请求属性
  419. connection.setRequestProperty("accept", "*/*");
  420. connection.setRequestProperty("connection", "Keep-Alive");
  421. connection.setRequestProperty("user-agent",
  422. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  423. // 建立实际的连接
  424. connection.connect();
  425. // 获取所有响应头字段
  426. Map<String, List<String>> map = connection.getHeaderFields();
  427. //ConstantUtil.UTF_CODE 编码格式
  428. InputStreamReader reader = new InputStreamReader(connection.getInputStream());
  429. char[] buff = new char[1024];
  430. int length = 0;
  431. while ((length = reader.read(buff)) != -1) {
  432. result = new String(buff, 0, length);
  433. jsonStr.append(result);
  434. }
  435. } catch (Exception e) {
  436. e.printStackTrace();
  437. }
  438. // 使用finally块来关闭输入流
  439. finally {
  440. try {
  441. if (in != null) {
  442. in.close();
  443. }
  444. } catch (Exception e2) {
  445. e2.printStackTrace();
  446. }
  447. }
  448. return jsonStr.toString();
  449. }
  450. public String getWxDecrypt(String encrypdata,String ivdata, String openid) {
  451. WXUser wxUser = wxUserService.getWXUser(openid);
  452. String str="";
  453. try {
  454. str = decrypt(encrypdata,wxUser.getXu_sessionkey(),ivdata,"UTF-8");
  455. } catch (Exception e) {
  456. e.printStackTrace();
  457. }
  458. return str;
  459. }
  460. public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
  461. // initialize();
  462. //被加密的数据
  463. byte[] dataByte = Base64.decodeBase64(data);
  464. //加密秘钥
  465. byte[] keyByte = Base64.decodeBase64(key);
  466. //偏移量
  467. byte[] ivByte = Base64.decodeBase64(iv);
  468. try {
  469. Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  470. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
  471. SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
  472. AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
  473. parameters.init(new IvParameterSpec(ivByte));
  474. cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
  475. byte[] resultByte = cipher.doFinal(dataByte);
  476. if (null != resultByte && resultByte.length > 0) {
  477. String result = new String(resultByte, encodingFormat);
  478. return result;
  479. }
  480. return null;
  481. } catch (NoSuchAlgorithmException e) {
  482. e.printStackTrace();
  483. } catch (NoSuchPaddingException e) {
  484. e.printStackTrace();
  485. } catch (InvalidParameterSpecException e) {
  486. e.printStackTrace();
  487. } catch (InvalidKeyException e) {
  488. e.printStackTrace();
  489. } catch (InvalidAlgorithmParameterException e) {
  490. e.printStackTrace();
  491. } catch (IllegalBlockSizeException e) {
  492. e.printStackTrace();
  493. } catch (BadPaddingException e) {
  494. e.printStackTrace();
  495. } catch (UnsupportedEncodingException e) {
  496. e.printStackTrace();
  497. }
  498. return null;
  499. }
  500. }