|
@@ -1,205 +0,0 @@
|
|
|
-package org.springblade.school.controller;
|
|
|
|
|
-
|
|
|
|
|
-import cn.hutool.http.HttpUtil;
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
-import lombok.AllArgsConstructor;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
|
-import org.springblade.system.user.entity.WxUnionIdOpenId;
|
|
|
|
|
-import org.springblade.system.user.feign.IWxUnionIdOpenIdClient;
|
|
|
|
|
-import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
|
|
-import org.springblade.school.util.WXPayXmlUtil;
|
|
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
-import org.w3c.dom.Node;
|
|
|
|
|
-import org.w3c.dom.NodeList;
|
|
|
|
|
-
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
-import javax.xml.parsers.DocumentBuilder;
|
|
|
|
|
-import java.io.BufferedReader;
|
|
|
|
|
-import java.io.ByteArrayInputStream;
|
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
-import java.util.*;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * 与微信产生交换的控制器
|
|
|
|
|
- *
|
|
|
|
|
- * @author BladeX
|
|
|
|
|
- */
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-@RestController
|
|
|
|
|
-@RequestMapping("/wechatTest")
|
|
|
|
|
-@AllArgsConstructor
|
|
|
|
|
-public class WxTest {
|
|
|
|
|
-
|
|
|
|
|
- private final IWxUnionIdOpenIdClient wxUnionIdOpenIdClient;
|
|
|
|
|
-
|
|
|
|
|
- //公众号的appId以及secret
|
|
|
|
|
- private static final String appId = "wxf077390a6ec17f23";
|
|
|
|
|
- private static final String appSecret = "50e84930675a0c06057d45a6d64ec548";
|
|
|
|
|
-
|
|
|
|
|
- @PostMapping("/scanCodeCallback")
|
|
|
|
|
- public void scanCodeCallback(HttpServletRequest request, HttpServletResponse response){
|
|
|
|
|
- log.info("======关注回调======");
|
|
|
|
|
-
|
|
|
|
|
- Map<String, String> dataMap = analysis(request);
|
|
|
|
|
- log.info("返回数据======》" + dataMap);
|
|
|
|
|
- log.info("返回数据======》" + dataMap.toString());
|
|
|
|
|
- String openId = dataMap.get("FromUserName");//openId
|
|
|
|
|
- String Event = dataMap.get("Event");//关注或取消关注
|
|
|
|
|
-
|
|
|
|
|
- if ("subscribe".equals(Event)) {//关注
|
|
|
|
|
- //获得公众号token
|
|
|
|
|
- Map<String,String> tokenMap = this.getToken();
|
|
|
|
|
- log.info("token======>"+tokenMap.get("accessToken"));
|
|
|
|
|
- //根据公众号openid获得unionId
|
|
|
|
|
- Map<String, String > userMap = this.getUserInfo(tokenMap.get("accessToken"),openId);
|
|
|
|
|
- String unionId = userMap.get("unionid");
|
|
|
|
|
- log.info("unionId======>"+userMap.get("unionid"));
|
|
|
|
|
-
|
|
|
|
|
- if (ObjectUtil.isNotEmpty(unionId)){
|
|
|
|
|
- //根据unionId查询数据库是否存在
|
|
|
|
|
- WxUnionIdOpenId wxUnionIdOpenId = wxUnionIdOpenIdClient.getWxUnionId(unionId);
|
|
|
|
|
- if (ObjectUtil.isEmpty(wxUnionIdOpenId)){//不存在 新增
|
|
|
|
|
- WxUnionIdOpenId item = new WxUnionIdOpenId();
|
|
|
|
|
- item.setOpenId(openId);
|
|
|
|
|
- item.setUnionId(unionId);
|
|
|
|
|
- wxUnionIdOpenIdClient.insertWxUO(item);
|
|
|
|
|
- }else {//存在 则更新
|
|
|
|
|
- wxUnionIdOpenId.setOpenId(openId);
|
|
|
|
|
- wxUnionIdOpenIdClient.updateWxUO(wxUnionIdOpenId);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获得公众号token
|
|
|
|
|
- */
|
|
|
|
|
- public Map<String, String> getToken() {
|
|
|
|
|
-
|
|
|
|
|
- Map<String,String> tokenMap = new HashMap<>();
|
|
|
|
|
- String requestUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
|
|
|
|
|
- try {
|
|
|
|
|
- String response = HttpUtil.get(requestUrl);
|
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
|
|
- if (ObjectUtil.isEmpty(jsonObject.get("errcode"))){
|
|
|
|
|
- String accessToken = String.valueOf(jsonObject.get("access_token"));
|
|
|
|
|
- if (ObjectUtil.isEmpty(accessToken)){
|
|
|
|
|
- throw new RuntimeException("获取token出现异常");
|
|
|
|
|
- }else {
|
|
|
|
|
- tokenMap.put("accessToken", accessToken);
|
|
|
|
|
- }
|
|
|
|
|
- }else if (jsonObject.getString("errcode").equals("-1")) {
|
|
|
|
|
- throw new RuntimeException("系统繁忙,此时请开发者稍候再试");
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new RuntimeException("不合法的telCode(telCode不存在、已过期或者使用过)");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.info("获取token出现异常");
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return tokenMap;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 获得用户信息
|
|
|
|
|
- */
|
|
|
|
|
- public Map<String, String > getUserInfo(String accessToken,String openId){
|
|
|
|
|
- Map<String,String> userMap = new HashMap<>();
|
|
|
|
|
- String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
|
|
|
|
|
- try {
|
|
|
|
|
- String response = HttpUtil.get(requestUrl);
|
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(response);
|
|
|
|
|
- if (ObjectUtil.isEmpty(jsonObject.get("errcode"))){
|
|
|
|
|
- String unionid = String.valueOf(jsonObject.get("unionid"));
|
|
|
|
|
- if (ObjectUtil.isEmpty(unionid)){
|
|
|
|
|
- throw new RuntimeException("获取unionid出现异常");
|
|
|
|
|
- }else {
|
|
|
|
|
- userMap.put("openid", openId);
|
|
|
|
|
- userMap.put("unionid", unionid);
|
|
|
|
|
- }
|
|
|
|
|
- }else {
|
|
|
|
|
- throw new RuntimeException("用户获取失败"+jsonObject.get("errmsg"));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.info("获取unionid出现异常");
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return userMap;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public Map<String, String> analysis(HttpServletRequest request) {
|
|
|
|
|
- try {
|
|
|
|
|
- BufferedReader reader = null;
|
|
|
|
|
- String line = "";
|
|
|
|
|
- String xmlString = null;
|
|
|
|
|
- reader = request.getReader();
|
|
|
|
|
- StringBuffer inputString = new StringBuffer();
|
|
|
|
|
-
|
|
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
|
|
- inputString.append(line);
|
|
|
|
|
- }
|
|
|
|
|
- xmlString = inputString.toString();
|
|
|
|
|
- Map<String, String> dataMap = xmlToMap(xmlString);
|
|
|
|
|
- return dataMap;
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * XML格式字符串转换为Map
|
|
|
|
|
- *
|
|
|
|
|
- * @param strXML XML字符串
|
|
|
|
|
- * @return XML数据转换后的Map
|
|
|
|
|
- * @throws Exception
|
|
|
|
|
- */
|
|
|
|
|
- public static Map<String, String> xmlToMap(String strXML) throws Exception {
|
|
|
|
|
- try {
|
|
|
|
|
- Map<String, String> data = new HashMap<String, String>();
|
|
|
|
|
- DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
|
|
|
|
|
- InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
|
|
|
|
|
- org.w3c.dom.Document doc = documentBuilder.parse(stream);
|
|
|
|
|
- doc.getDocumentElement().normalize();
|
|
|
|
|
- NodeList nodeList = doc.getDocumentElement().getChildNodes();
|
|
|
|
|
- for (int idx = 0; idx < nodeList.getLength(); ++idx) {
|
|
|
|
|
- Node node = nodeList.item(idx);
|
|
|
|
|
- if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
|
|
|
- org.w3c.dom.Element element = (org.w3c.dom.Element) node;
|
|
|
|
|
- data.put(element.getNodeName(), element.getTextContent());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- try {
|
|
|
|
|
- stream.close();
|
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
|
- // do nothing
|
|
|
|
|
- }
|
|
|
|
|
- return data;
|
|
|
|
|
- } catch (Exception ex) {
|
|
|
|
|
- getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
|
|
|
|
|
- throw ex;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 日志
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- public static Logger getLogger() {
|
|
|
|
|
- Logger logger = LoggerFactory.getLogger("wxpay java sdk");
|
|
|
|
|
- return logger;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|