SenInfoCheckUtil.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. package com.sqx.modules.utils;
  2. import cn.hutool.core.util.StrUtil;
  3. import cn.hutool.http.HttpUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.google.common.collect.Maps;
  7. import com.sqx.common.constant.RedisKey;
  8. import com.sqx.common.utils.RedisUtils;
  9. import com.sqx.modules.common.entity.CommonInfo;
  10. import com.sqx.modules.common.service.CommonInfoService;
  11. import icu.xuyijie.secureapi.cipher.CipherAlgorithmEnum;
  12. import icu.xuyijie.secureapi.cipher.CipherUtils;
  13. import icu.xuyijie.secureapi.model.SecureApiPropertiesConfig;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.io.IOUtils;
  16. import org.apache.commons.lang3.StringUtils;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.http.ResponseEntity;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.web.client.RestTemplate;
  24. import javax.imageio.ImageIO;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.awt.image.BufferedImage;
  27. import java.io.InputStream;
  28. import java.io.OutputStreamWriter;
  29. import java.io.PrintWriter;
  30. import java.net.URL;
  31. import java.net.URLConnection;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. @Component
  36. @Slf4j
  37. public class SenInfoCheckUtil {
  38. private static Logger logger = LoggerFactory.getLogger(SenInfoCheckUtil.class);
  39. // 这里使用静态,让 service 属于类
  40. private static CommonInfoService commonInfoService;
  41. private static RedisUtils redisUtils;
  42. private static boolean GET_MP_TOKEN_FROM_TEMP;
  43. private static SecureApiPropertiesConfig SECUREAPIPROPERTIESCONFIG;
  44. // 注入的时候,给类的 service 注入
  45. @Autowired
  46. public void setWxChatContentService(CommonInfoService commonInfoService, RedisUtils redisUtils, @Value("${mp.temp:false}") boolean isTemp, SecureApiPropertiesConfig secureApiPropertiesConfig) {
  47. SenInfoCheckUtil.commonInfoService = commonInfoService;
  48. SenInfoCheckUtil.redisUtils = redisUtils;
  49. SenInfoCheckUtil.GET_MP_TOKEN_FROM_TEMP = isTemp;
  50. SenInfoCheckUtil.SECUREAPIPROPERTIESCONFIG = secureApiPropertiesConfig;
  51. }
  52. /**
  53. * 获取Token 小程序
  54. * @return AccessToken
  55. */
  56. public static String getMpToken(){
  57. String token = (String) redisUtils.get(RedisKey.MP_TOKEN_CACHE_KEY);
  58. if (StrUtil.isBlank(token)) {
  59. if (!GET_MP_TOKEN_FROM_TEMP) {
  60. token = getMpAccessToken();
  61. } else {
  62. try {
  63. token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
  64. } catch (Exception e) {}
  65. }
  66. redisUtils.set(RedisKey.MP_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
  67. }
  68. return token;
  69. }
  70. /**
  71. * 获取Token 小程序
  72. * @return AccessToken
  73. */
  74. public static String getRiderMpToken() {
  75. String token = (String) redisUtils.get(RedisKey.MP_OF_RIDER_TOKEN_CACHE_KEY);
  76. if (StrUtil.isBlank(token)) {
  77. if (!GET_MP_TOKEN_FROM_TEMP) {
  78. token = getRiderMpAccessToken();
  79. } else {
  80. try {
  81. token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
  82. } catch (Exception e) {}
  83. }
  84. redisUtils.set(RedisKey.MP_OF_RIDER_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
  85. }
  86. return token;
  87. }
  88. public static String getShopMpToken() {
  89. String token = (String) redisUtils.get(RedisKey.MP_OF_SHOP_TOKEN_CACHE_KEY);
  90. if (StrUtil.isBlank(token)) {
  91. if (!GET_MP_TOKEN_FROM_TEMP) {
  92. token = getShopMpAccessToken();
  93. } else {
  94. try {
  95. token = doDecrypt(HttpUtil.get("https://mxys.chuanghai-tech.com/sqx_fast/app/wxPay/temp/token/1"));
  96. } catch (Exception e) {}
  97. }
  98. redisUtils.set(RedisKey.MP_OF_SHOP_TOKEN_CACHE_KEY, token, RedisUtils.HOUR_ONE_EXPIRE);
  99. }
  100. return token;
  101. }
  102. private static String doDecrypt(String text) {
  103. CipherUtils cipherUtils = new CipherUtils(CipherAlgorithmEnum.AES_CBC_PKCS5);
  104. String key = SECUREAPIPROPERTIESCONFIG.getKey();
  105. String iv = SECUREAPIPROPERTIESCONFIG.getIv();
  106. return cipherUtils.decrypt(text, key, iv);
  107. }
  108. public static void getImg(String relation,String goodsId,String type, String page,HttpServletResponse response){
  109. String mpToken = getMpToken();
  110. //获取二维码数据
  111. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpToken;
  112. Map<String,Object> map = Maps.newHashMap();
  113. map.put("scene",relation+"&"+goodsId+"&"+type);
  114. String value = commonInfoService.findOne(105).getValue();
  115. if("是".equals(value)){
  116. map.put("page",page);
  117. }
  118. map.put("width", 280);
  119. String jsonString = JSON.toJSONString(map);
  120. InputStream inputStream = sendPostBackStream(url, jsonString);
  121. //生成二维码图片
  122. response.setContentType("image/png");
  123. try{
  124. BufferedImage bi = ImageIO.read(inputStream);
  125. ImageIO.write(bi, "JPG", response.getOutputStream());
  126. inputStream.close();
  127. }catch (Exception e){
  128. e.printStackTrace();
  129. }
  130. }
  131. /**
  132. * 获取二维码图片
  133. */
  134. public static void getPoster(String invitationCode, HttpServletResponse response){
  135. String mpToken = getMpToken();
  136. //获取二维码数据
  137. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpToken;
  138. Map<String,Object> map = Maps.newHashMap();
  139. map.put("scene",invitationCode);
  140. map.put("width", 280);
  141. String jsonString = JSON.toJSONString(map);
  142. InputStream inputStream = sendPostBackStream(url, jsonString);
  143. //生成二维码图片
  144. response.setContentType("image/png");
  145. try{
  146. BufferedImage bi = ImageIO.read(inputStream);
  147. ImageIO.write(bi, "JPG", response.getOutputStream());
  148. inputStream.close();
  149. }catch (Exception e){
  150. logger.error(e.getMessage());
  151. }
  152. }
  153. /**
  154. * 获取二维码图片
  155. */
  156. public static void getShopQr(String shopId, HttpServletResponse response){
  157. String mpToken = getMpToken();
  158. //获取二维码数据
  159. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpToken;
  160. Map<String,Object> map = Maps.newHashMap();
  161. map.put("scene",shopId);
  162. CommonInfo one = commonInfoService.findOne(261);
  163. if("是".equals(one.getValue())){
  164. map.put("page","pages/index/index");
  165. }
  166. map.put("width", 280);
  167. String jsonString = JSON.toJSONString(map);
  168. InputStream inputStream = sendPostBackStream(url, jsonString);
  169. //生成二维码图片
  170. response.setContentType("image/png");
  171. try{
  172. BufferedImage bi = ImageIO.read(inputStream);
  173. ImageIO.write(bi, "JPG", response.getOutputStream());
  174. inputStream.close();
  175. }catch (Exception e){
  176. logger.error(e.getMessage());
  177. }
  178. }
  179. /*public static void getShopQr(String shopId, HttpServletResponse response){
  180. String mpToken = getMpToken();
  181. //获取二维码数据
  182. String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+mpToken;
  183. Map<String,Object> map = Maps.newHashMap();
  184. CommonInfo one = commonInfoService.findOne(261);
  185. // if("是".equals(one.getValue())){
  186. map.put("page","/pages/index/index?shopId="+shopId);
  187. // }
  188. // map.put("scene",shopId);
  189. map.put("width", 280);
  190. String jsonString = JSON.toJSONString(map);
  191. InputStream inputStream = sendPostBackStream(url, jsonString);
  192. //生成二维码图片
  193. response.setContentType("image/png");
  194. try{
  195. BufferedImage bi = ImageIO.read(inputStream);
  196. ImageIO.write(bi, "JPG", response.getOutputStream());
  197. inputStream.close();
  198. }catch (Exception e){
  199. logger.error(e.getMessage());
  200. }
  201. }*/
  202. private static InputStream sendPostBackStream(String url, String param) {
  203. PrintWriter out = null;
  204. try {
  205. URL realUrl = new URL(url);
  206. // 打开和URL之间的连接
  207. URLConnection conn = realUrl.openConnection();
  208. // 设置通用的请求属性
  209. conn.setRequestProperty("accept", "*/*");
  210. conn.setRequestProperty("connection", "Keep-Alive");
  211. conn.setRequestProperty("user-agent",
  212. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  213. conn.setDoOutput(true);
  214. conn.setDoInput(true);
  215. //解决乱码问题
  216. OutputStreamWriter outWriter =new OutputStreamWriter(conn.getOutputStream(), "utf-8");
  217. out =new PrintWriter(outWriter);
  218. // 发送请求参数
  219. if(StringUtils.isNotBlank(param)) {
  220. out.print(param);
  221. }
  222. // flush输出流的缓冲
  223. out.flush();
  224. return conn.getInputStream();
  225. } catch (Exception e) {
  226. logger.error("发送 POST 请求出现异常!"+e);
  227. } finally{
  228. IOUtils.closeQuietly(out);
  229. }
  230. return null;
  231. }
  232. /**
  233. * 获取access_token
  234. * 每个两个小时自动刷新AcessTocken
  235. */
  236. // @Scheduled(cron = "0 0 */2 * * ?")
  237. public static String getMpAccessToken(){
  238. String appid = commonInfoService.findOne(45).getValue();
  239. String secret = commonInfoService.findOne(46).getValue();
  240. String jsonResult = HttpClientUtil.doPost("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  241. JSONObject parseObject = JSON.parseObject(jsonResult);
  242. logger.info("=========accessTokenOut========="+parseObject.toJSONString());
  243. String errcode = parseObject.getString("errcode");
  244. String accessToken = parseObject.getString("access_token");
  245. String expiresIn = parseObject.getString("expires_in");
  246. return accessToken;
  247. }
  248. public static String getRiderMpAccessToken(){
  249. String appid = commonInfoService.findOne(248).getValue();
  250. String secret = commonInfoService.findOne(249).getValue();
  251. String jsonResult = HttpClientUtil.doPost("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  252. JSONObject parseObject = JSON.parseObject(jsonResult);
  253. logger.info("=========accessTokenOut========="+parseObject.toJSONString());
  254. String errcode = parseObject.getString("errcode");
  255. String accessToken = parseObject.getString("access_token");
  256. String expiresIn = parseObject.getString("expires_in");
  257. return accessToken;
  258. }
  259. public static String getShopMpAccessToken(){
  260. String appid = commonInfoService.findOne(305).getValue();
  261. String secret = commonInfoService.findOne(306).getValue();
  262. String jsonResult = HttpClientUtil.doPost("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  263. JSONObject parseObject = JSON.parseObject(jsonResult);
  264. logger.info("=========accessTokenOut========="+parseObject.toJSONString());
  265. String errcode = parseObject.getString("errcode");
  266. String accessToken = parseObject.getString("access_token");
  267. String expiresIn = parseObject.getString("expires_in");
  268. return accessToken;
  269. }
  270. /**
  271. * 小程序消息推送
  272. * @param wxId 用户openId
  273. * @param templateId 模板id
  274. * @param msgList 消息内容集合
  275. * @param type 类型
  276. */
  277. public static void sendMsg(String wxId, String templateId, List<String> msgList, Integer type){
  278. String mpToken = getMpToken();
  279. RestTemplate restTemplate = new RestTemplate();
  280. String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" +mpToken;
  281. //拼接推送的模版
  282. WxMssVo wxMssVo = new WxMssVo();
  283. wxMssVo.setTouser(wxId);//用户的openid(要发送给那个用户,通常这里应该动态传进来的)
  284. wxMssVo.setTemplate_id(templateId);//订阅消息模板id
  285. // wxMssVo.setPage("pages/index/index");
  286. wxMssVo.setData(getParam(type,msgList));
  287. ResponseEntity<String> responseEntity =
  288. restTemplate.postForEntity(url, wxMssVo, String.class);
  289. String body = responseEntity.getBody();
  290. //System.err.println(body);
  291. log.info("小程序消息通知返回值");
  292. log.info("小程序消息通知返回值:"+ body);
  293. }
  294. public static void sendRiderMsg(String wxId, String templateId, List<String> msgList, Integer type) {
  295. String mpToken = getRiderMpToken();
  296. RestTemplate restTemplate = new RestTemplate();
  297. String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" +mpToken;
  298. //拼接推送的模版
  299. WxMssVo wxMssVo = new WxMssVo();
  300. wxMssVo.setTouser(wxId);//用户的openid(要发送给那个用户,通常这里应该动态传进来的)
  301. wxMssVo.setTemplate_id(templateId);//订阅消息模板id
  302. // wxMssVo.setPage("pages/index/index");
  303. wxMssVo.setData(getParam(type,msgList));
  304. ResponseEntity<String> responseEntity =
  305. restTemplate.postForEntity(url, wxMssVo, String.class);
  306. String body = responseEntity.getBody();
  307. //System.err.println(body);
  308. log.info("小程序消息通知返回值");
  309. log.info("小程序消息通知返回值:"+ body);
  310. }
  311. public static void sendShopMsg(String wxId, String templateId, List<String> msgList, Integer type) {
  312. String mpToken = getShopMpToken();
  313. RestTemplate restTemplate = new RestTemplate();
  314. String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" +mpToken;
  315. //拼接推送的模版
  316. WxMssVo wxMssVo = new WxMssVo();
  317. wxMssVo.setTouser(wxId);//用户的openid(要发送给那个用户,通常这里应该动态传进来的)
  318. wxMssVo.setTemplate_id(templateId);//订阅消息模板id
  319. // wxMssVo.setPage("pages/index/index");
  320. wxMssVo.setData(getParam(type,msgList));
  321. ResponseEntity<String> responseEntity =
  322. restTemplate.postForEntity(url, wxMssVo, String.class);
  323. String body = responseEntity.getBody();
  324. //System.err.println(body);
  325. log.info("小程序消息通知返回值");
  326. log.info("小程序消息通知返回值:"+ body);
  327. }
  328. /**
  329. * 订阅消息模板
  330. * @param type 类型 1 用户端订单状态通知 2骑手端订单完成通知 3 用户端订单取消通知
  331. * 4 骑手端接单成功通知 5 6 骑手端订单完成 7骑手端新订单通知 8商家端新订单通知 9商家端订单变更通知
  332. * 10 账户余额充值成功通知
  333. * @param msgList 对应值
  334. * @return
  335. */
  336. public static Map<String, TemplateParam> getParam(Integer type, List<String> msgList){
  337. if(type==1){
  338. Map<String, TemplateParam> paras = new HashMap<>();
  339. //订单状态
  340. paras.put("phrase3",new TemplateParam( msgList.get(0)));
  341. //订单号
  342. paras.put("character_string1",new TemplateParam( msgList.get(1)));
  343. //服务门店
  344. paras.put("thing17",new TemplateParam( msgList.get(2)));
  345. //订单时间
  346. paras.put("time12",new TemplateParam( msgList.get(3)));
  347. return paras;
  348. }else if(type==2) {
  349. Map<String, TemplateParam> paras = new HashMap<>();
  350. //订单标题
  351. paras.put("thing1", new TemplateParam(msgList.get(0)));
  352. //下单时间
  353. paras.put("time7", new TemplateParam(msgList.get(1)));
  354. //订单状态
  355. paras.put("phrase2", new TemplateParam(msgList.get(2)));
  356. //完成时间
  357. paras.put("time3", new TemplateParam(msgList.get(3)));
  358. return paras;
  359. }else if(type==3) {
  360. Map<String, TemplateParam> paras = new HashMap<>();
  361. //订单号
  362. paras.put("character_string1", new TemplateParam(msgList.get(0)));
  363. //取消原因
  364. paras.put("thing2", new TemplateParam(msgList.get(1)));
  365. //退款金额
  366. paras.put("amount14", new TemplateParam(msgList.get(2)));
  367. //取消时间
  368. paras.put("time5", new TemplateParam(msgList.get(3)));
  369. return paras;
  370. }else if(type==4) {
  371. Map<String, TemplateParam> paras = new HashMap<>();
  372. //服务项目
  373. paras.put("thing9", new TemplateParam(msgList.get(0)));
  374. //接单人员
  375. paras.put("thing6", new TemplateParam(msgList.get(1)));
  376. //接单时间
  377. paras.put("time7", new TemplateParam(msgList.get(2)));
  378. //备注
  379. paras.put("thing4", new TemplateParam(msgList.get(3)));
  380. return paras;
  381. }else if(type==5) {
  382. Map<String, TemplateParam> paras = new HashMap<>(3);
  383. //订单名称
  384. paras.put("character_string1", new TemplateParam(msgList.get(0)));
  385. //下单时间
  386. paras.put("phrase3", new TemplateParam(msgList.get(1)));
  387. //订单状态
  388. paras.put("thing8", new TemplateParam(msgList.get(2)));
  389. //完成时间
  390. paras.put("time11", new TemplateParam(msgList.get(3)));
  391. return paras;
  392. }else if(type==6) {
  393. Map<String, TemplateParam> paras = new HashMap<>(3);
  394. //订单名称
  395. paras.put("thing1", new TemplateParam(msgList.get(0)));
  396. //下单时间
  397. paras.put("amount13", new TemplateParam(msgList.get(1)));
  398. //订单状态
  399. paras.put("thing11", new TemplateParam(msgList.get(2)));
  400. //完成时间
  401. paras.put("date4", new TemplateParam(msgList.get(3)));
  402. return paras;
  403. }else if(type==7) {
  404. Map<String, TemplateParam> paras = new HashMap<>(3);
  405. paras.put("character_string12", new TemplateParam(msgList.get(0)));
  406. paras.put("amount13", new TemplateParam(msgList.get(1)));
  407. paras.put("date4", new TemplateParam(msgList.get(2)));
  408. paras.put("thing5", new TemplateParam(msgList.get(3)));
  409. return paras;
  410. }else if(type==8) {
  411. Map<String, TemplateParam> paras = new HashMap<>(3);
  412. paras.put("number5", new TemplateParam(msgList.get(0)));
  413. paras.put("amount3", new TemplateParam(msgList.get(1)));
  414. paras.put("thing6", new TemplateParam(msgList.get(2)));
  415. return paras;
  416. }else if(type==9){
  417. Map<String, TemplateParam> paras = new HashMap<>(3);
  418. paras.put("character_string2", new TemplateParam(msgList.get(0)));
  419. paras.put("phrase5", new TemplateParam(msgList.get(1)));
  420. paras.put("time3", new TemplateParam(msgList.get(2)));
  421. return paras;
  422. } else if (type == 10) {
  423. Map<String, TemplateParam> paras = new HashMap<>(3);
  424. paras.put("thing1", new TemplateParam(msgList.get(0)));
  425. paras.put("amount2", new TemplateParam(msgList.get(1)));
  426. paras.put("time3", new TemplateParam(msgList.get(2)));
  427. return paras;
  428. }
  429. return null;
  430. }
  431. }