提交 15506231 authored 作者: renjiancai's avatar renjiancai

--no commit message

上级 fce0de97
package com.zrqx.pay.bg.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.BackingStoreException;
......@@ -17,6 +17,8 @@ import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.jeewx.api.third.JwThirdAPI;
import org.jeewx.api.third.model.ApiComponentToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -43,13 +45,15 @@ import com.zrqx.pay.bg.asc.AesException;
import com.zrqx.pay.bg.asc.WXBizMsgCrypt;
import com.zrqx.pay.bg.service.AssetsService;
import com.zrqx.pay.commons.Redis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(PayRequestPath.BG + PayRequestPath.ASSETS)
@Api(description = "余额")
public class AssetsController {
private final static Logger log = LoggerFactory.getLogger(AssetsController.class);
@Autowired
private AssetsService assetsService;
@Autowired
......@@ -76,14 +80,70 @@ public class AssetsController {
public CallBack<PageInfo<AssetsRecordVo>> getRecord(AssetsRecordQueryForm form, PageParam pageParam){
return CallBack.success(assetsService.getRecord(form, pageParam));
}
//======================================测试微信登录需要===================================
private final static String COMPONENT_APPID = WeChatConfig.WECHAT_ClIENT_Id;
private final static String COMPONENT_APPSECRET = WeChatConfig.WECHAT_ClIENT_SECRET;
private final static String COMPONENT_ENCODINGAESKEY = "RDXJ9DzpjPpjSfETvAy08PDUlvZlhIibJWGP3yLuU5t";
private final static String COMPONENT_TOKEN = "23_4NeNfPBg37JfaYDZE8yDWJJ-UldUUM5tPkRnhVp1LClx-ulojqNSdIdqni3083YVUlHfY8hg8s9N7jMYQM2iTSvqcG7ihwylq9RWmNH8-BkmRGvo7qrY6ZSlgen65rx7rMurguTblWXeBuNJYFDjABASZX";
/**
* 授权事件接收
* @param request
* @param response
* @return
* @throws IOException
* @throws AesException
* @throws SAXException
* @throws ParserConfigurationException
* @throws BackingStoreException
* @throws DocumentException
*/
@ApiOperation(value = "获取授权Code", notes = "获取授权Code")
@PostMapping(value= "/vote/getCode")
public CallBack<String> getCode(HttpServletRequest request, HttpServletResponse response) throws IOException, AesException, SAXException, ParserConfigurationException, BackingStoreException, DocumentException{
processAuthorizeEvent(request);
return CallBack.success();
}
/**
* 一键授权功能
* @return
*/
@ApiOperation(value = "公众号授权引导入口", notes = "公众号授权引导入口")
@GetMapping("/auth_open")
public CallBack<String> getCode_Url() {
return CallBack.success(redis.get("Code_Url"));
}
@RequestMapping(value = "/weixin/{appid}/callback")
public void acceptMessageAndEvent(HttpServletRequest request, HttpServletResponse response) throws IOException, AesException, DocumentException {
String msgSignature = request.getParameter("msg_signature");
//LogUtil.info("第三方平台全网发布-------------{appid}/callback-----------验证开始。。。。msg_signature="+msgSignature);
if (!StringUtils.isNotBlank(msgSignature))
return;// 微信推送给第三方开放平台的消息一定是加过密的,无消息加密无法解密消息
StringBuilder sb = new StringBuilder();
BufferedReader in = request.getReader();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
String xml = sb.toString();
Document doc = DocumentHelper.parseText(xml);
Element rootElt = doc.getRootElement();
String toUserName = rootElt.elementText("ToUserName");
//微信全网测试账号
// if (StringUtils.equalsIgnoreCase(toUserName, APPID)) {
// LogUtil.info("全网发布接入检测消息反馈开始---------------APPID="+ APPID +"------------------------toUserName="+toUserName);
checkWeixinAllNetworkCheck(request,response,xml);
// }
}
/**
* 处理授权事件的推送
*
......@@ -96,7 +156,7 @@ public class AssetsController {
public void processAuthorizeEvent(HttpServletRequest request) throws IOException, AesException, BackingStoreException, DocumentException {
String encodingAesKey = "RDXJ9DzpjPpjSfETvAy08PDUlvZlhIibJWGP3yLuU5t";
String token = "23_4NeNfPBg37JfaYDZE8yDWJJ-UldUUM5tPkRnhVp1LClx-ulojqNSdIdqni3083YVUlHfY8hg8s9N7jMYQM2iTSvqcG7ihwylq9RWmNH8-BkmRGvo7qrY6ZSlgen65rx7rMurguTblWXeBuNJYFDjABASZX";
String appId="wxcb2f6d1e3f2f187d";
//String appId="wxcb2f6d1e3f2f187d";
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String msgSignature = request.getParameter("msg_signature");
......@@ -110,7 +170,7 @@ public class AssetsController {
if(StringUtils.isEmpty(xml)){
throw new BackingStoreException("第三方平台全网发布-----------------------原始 Xml为空!");
}
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, COMPONENT_APPID);
String xml1 = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
log.error("第三方平台全网发布111-----------------------解密后 Xml=" + xml1);
processAuthorizationEvent(xml1);
......@@ -128,9 +188,9 @@ public class AssetsController {
Element rootElt = doc.getRootElement();
String ticket = rootElt.elementText("ComponentVerifyTicket");
//微信公众号
String WeChat_client_id="wxcb2f6d1e3f2f187d";
String WeChat_client_secret="a6d70f9e0277b86fac2bd02cf29a2650";
String param="{\"component_appid\":\""+WeChat_client_id+"\" ,\"component_appsecret\": \""+WeChat_client_secret+"\",\"component_verify_ticket\": \""+ticket+"\"}";
//String WeChat_client_id="wxcb2f6d1e3f2f187d";
//String WeChat_client_secret="a6d70f9e0277b86fac2bd02cf29a2650";
String param="{\"component_appid\":\""+COMPONENT_APPID+"\" ,\"component_appsecret\": \""+COMPONENT_APPSECRET+"\",\"component_verify_ticket\": \""+ticket+"\"}";
String url_token="https://api.weixin.qq.com/cgi-bin/component/api_component_token";
JSONObject json=JSONArray.parseObject(param);
String result_token = HttpClientUtil.doPostW(url_token, "utf-8",json);
......@@ -138,23 +198,21 @@ public class AssetsController {
JSONObject jsonobj_WX = JSON.parseObject(result_token);
log.info("解密后的result_token++++++++++++++++++++++"+result_token);
String component_access_token=jsonobj_WX.getString("component_access_token");
String paramCode="{\"component_appid\":\""+WeChat_client_id+"\"}";
String paramCode="{\"component_appid\":\""+COMPONENT_APPID+"\"}";
String url_code="https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token="+component_access_token;
JSONObject jsonCode=JSONArray.parseObject(paramCode);
String result_Code = HttpClientUtil.doPostW(url_code, "utf-8",jsonCode);
JSONObject jsonobj_Code = JSON.parseObject(result_Code);
//{"pre_auth_code":"Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw","expires_in":600}
String code=jsonobj_Code.getString("pre_auth_code");
log.info("解密后的Code++++++++++++++++++++++"+code);
log.info("第三方平台全网发布-----------------------解密后 ComponentVerifyTicket=" + ticket);
redis.set("ComponentVerifyTicket", ticket);
redis.set("ticket", ticket);
getCode1(code);
}
public void getCode1(String ticket_value) throws IOException{
ModelAndView mav = new ModelAndView();
mav.addObject("url","https://mp.weixin.qq.com/cgi-bin/componentloginpage?"
+ "component_appid=wxcb2f6d1e3f2f187d&"
+ "component_appid=wxd53422d70d076d82&"
+ "pre_auth_code="+ticket_value+"&"
+ "redirect_uri=http://test16.zhongdianyun.com/index.html&type=3");
mav.setViewName("open_auth");
......@@ -171,11 +229,163 @@ public class AssetsController {
log.info("Code_Url1111111111++++++++++++++++++++++=" +newStr);
redis.set("Code_Url",newStr);
}
@ApiOperation(value = "公众号授权引导入口", notes = "公众号授权引导入口")
@GetMapping("/auth_open")
public CallBack<String> getCode_Url() {
return CallBack.success(redis.get("Code_Url"));
/**
* 获取授权账号信息
* @param appid
* @return
*/
/*public WeixinOpenAccountEntity getWeixinOpenAccount(String appid){
WeixinOpenAccountEntity entity = null;
List<WeixinOpenAccountEntity> ls = systemService.findByProperty(WeixinOpenAccountEntity.class, "appid", appid);
if(ls!=null && ls.size()!=0){
entity = ls.get(0);
}
return entity;
}*/
/**
* 获取授权的Appid
* @param xml
* @return
*/
public String getAuthorizerAppidFromXml(String xml) {
Document doc;
try {
doc = DocumentHelper.parseText(xml);
Element rootElt = doc.getRootElement();
String toUserName = rootElt.elementText("ToUserName");
return toUserName;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void checkWeixinAllNetworkCheck(HttpServletRequest request, HttpServletResponse response,String xml) throws DocumentException, IOException, AesException{
String nonce = request.getParameter("nonce");
String timestamp = request.getParameter("timestamp");
String msgSignature = request.getParameter("msg_signature");
WXBizMsgCrypt pc = new WXBizMsgCrypt(COMPONENT_TOKEN, COMPONENT_ENCODINGAESKEY, COMPONENT_APPID);
xml = pc.decryptMsg(msgSignature, timestamp, nonce, xml);
Document doc = DocumentHelper.parseText(xml);
Element rootElt = doc.getRootElement();
String msgType = rootElt.elementText("MsgType");
String toUserName = rootElt.elementText("ToUserName");
String fromUserName = rootElt.elementText("FromUserName");
// LogUtil.info("---全网发布接入检测--step.1-----------msgType="+msgType+"-----------------toUserName="+toUserName+"-----------------fromUserName="+fromUserName);
// LogUtil.info("---全网发布接入检测--step.2-----------xml="+xml);
if("event".equals(msgType)){
// LogUtil.info("---全网发布接入检测--step.3-----------事件消息--------");
String event = rootElt.elementText("Event");
replyEventMessage(request,response,event,toUserName,fromUserName);
}else if("text".equals(msgType)){
// LogUtil.info("---全网发布接入检测--step.3-----------文本消息--------");
String content = rootElt.elementText("Content");
processTextMessage(request,response,content,toUserName,fromUserName);
}
}
public void replyEventMessage(HttpServletRequest request, HttpServletResponse response, String event, String toUserName, String fromUserName) throws DocumentException, IOException {
String content = event + "from_callback";
// LogUtil.info("---全网发布接入检测------step.4-------事件回复消息 content="+content + " toUserName="+toUserName+" fromUserName="+fromUserName);
replyTextMessage(request,response,content,toUserName,fromUserName);
}
public void processTextMessage(HttpServletRequest request, HttpServletResponse response,String content,String toUserName, String fromUserName) throws IOException, DocumentException{
if("TESTCOMPONENT_MSG_TYPE_TEXT".equals(content)){
String returnContent = content+"_callback";
replyTextMessage(request,response,returnContent,toUserName,fromUserName);
}else if(StringUtils.startsWithIgnoreCase(content, "QUERY_AUTH_CODE")){
//output(response, "");
//接下来客服API再回复一次消息
replyApiTextMessage(request,response,content.split(":")[1],fromUserName);
}
}
public void replyApiTextMessage(HttpServletRequest request, HttpServletResponse response, String auth_code, String fromUserName) throws DocumentException, IOException {
String authorization_code = auth_code;
// 得到微信授权成功的消息后,应该立刻进行处理!!相关信息只会在首次授权的时候推送过来
System.out.println("------step.1----使用客服消息接口回复粉丝----逻辑开始-------------------------");
try {
ApiComponentToken apiComponentToken = new ApiComponentToken();
apiComponentToken.setComponent_appid(COMPONENT_APPID);
apiComponentToken.setComponent_appsecret(COMPONENT_APPSECRET);
//WeixinOpenAccountEntity entity = getWeixinOpenAccount(APPID);
apiComponentToken.setComponent_verify_ticket(redis.get("ticket"));
String component_access_token = JwThirdAPI.getAccessToken(apiComponentToken);
System.out.println("------step.2----使用客服消息接口回复粉丝------- component_access_token = "+component_access_token + "---------authorization_code = "+authorization_code);
net.sf.json.JSONObject authorizationInfoJson = JwThirdAPI.getApiQueryAuthInfo(COMPONENT_APPID, authorization_code, component_access_token);
System.out.println("------step.3----使用客服消息接口回复粉丝-------------- 获取authorizationInfoJson = "+authorizationInfoJson);
net.sf.json.JSONObject infoJson = authorizationInfoJson.getJSONObject("authorization_info");
String authorizer_access_token = infoJson.getString("authorizer_access_token");
Map<String,Object> obj = new HashMap<String,Object>();
Map<String,Object> msgMap = new HashMap<String,Object>();
String msg = auth_code + "_from_api";
msgMap.put("content", msg);
obj.put("touser", fromUserName);
obj.put("msgtype", "text");
obj.put("text", msgMap);
JwThirdAPI.sendMessage(obj, authorizer_access_token);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回复微信服务器"文本消息"
* @param request
* @param response
* @param content
* @param toUserName
* @param fromUserName
* @throws DocumentException
* @throws IOException
*/
public void replyTextMessage(HttpServletRequest request, HttpServletResponse response, String content, String toUserName, String fromUserName) throws DocumentException, IOException {
Long createTime = Calendar.getInstance().getTimeInMillis() / 1000;
StringBuffer sb = new StringBuffer();
sb.append("<xml>");
sb.append("<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>");
sb.append("<FromUserName><![CDATA["+toUserName+"]]></FromUserName>");
sb.append("<CreateTime>"+createTime+"</CreateTime>");
sb.append("<MsgType><![CDATA[text]]></MsgType>");
sb.append("<Content><![CDATA["+content+"]]></Content>");
sb.append("</xml>");
String replyMsg = sb.toString();
String returnvaleue = "";
try {
WXBizMsgCrypt pc = new WXBizMsgCrypt(COMPONENT_TOKEN, COMPONENT_ENCODINGAESKEY, COMPONENT_APPID);
returnvaleue = pc.encryptMsg(replyMsg, createTime.toString(), "easemob");
// System.out.println("------------------加密后的返回内容 returnvaleue: "+returnvaleue);
} catch (AesException e) {
e.printStackTrace();
}
output(response, returnvaleue);
}
/**
* 工具类:回复微信服务器"文本消息"
* @param response
* @param returnvaleue
*/
public void output(HttpServletResponse response,String returnvaleue){
try {
PrintWriter pw = response.getWriter();
pw.write(returnvaleue);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.zrqx.pay.bg.controller;
/**
* 获取微信公众号链接信息
* @author rjc
* @date 2019年7月24日下午5:19:34
*/
public class WeChatConfig {
/**
* 微信公众号appid
*/
//public final static String WECHAT_ClIENT_Id = "wxcb2f6d1e3f2f187d";
public final static String WECHAT_ClIENT_Id = "wxd53422d70d076d82";
/**
* 微信公众号AppSecret
*/
//public final static String WECHAT_ClIENT_SECRET="a6d70f9e0277b86fac2bd02cf29a2650";
public final static String WECHAT_ClIENT_SECRET="5c1cccaaf3daeb84303771693bee4ec3";
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论