Commit a9545864 authored by shiyu's avatar shiyu

公众号

parent 624f004e
package com.wwdz.ch.wx.api;
import com.wwdz.ch.core.util.OkHttpUtil;
import com.wwdz.ch.core.util.RedisUtils;
import com.xxdxxs.utils.JsonUtils;
import com.xxdxxs.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* 微信公众号接口
*/
@Component
public class OfficialAccountApi {
private static final Logger logger = LoggerFactory.getLogger(OfficialAccountApi.class);
private final static String APPID = "wx7db8eb146a194083";
private final static String APPSECRET = "daecb3de53841fe1a787bc9849ac3406";
private final static String GET_ACCESSTOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
@Autowired
RedisUtils redisUtils;
public String getAccessToken() {
//根据商户的appid从redis中获取商户的token
String token = redisUtils.hasKey(APPID)? redisUtils.get(APPID).toString() : null;
if (StringUtils.isEmpty(token)) {
token = refreshAccessToken();
}
return token;
}
public String refreshAccessToken() {
String url = GET_ACCESSTOKEN_URL + "&appid=" + APPID + "&secret=" + APPSECRET;
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
okHttpUtil.get();
String responseStr = okHttpUtil.async();
logger.info("appid : {}, 获取token response :{}", responseStr);
if (!responseStr.contains("access_token")) {
String errorCode = JsonUtils.getValueByPath(responseStr, "errcode");
String errmsg = JsonUtils.getValueByPath(responseStr, "errmsg");
logger.error("appid : {}, 获取token出错, errorCode : {}, errmsg : {} ", APPID, errorCode, errmsg);
return null;
}
String token = JsonUtils.getValueByPath(responseStr, "access_token");
String expires_in = JsonUtils.getValueByPath(responseStr, "expires_in");
redisUtils.set(APPID, token, Long.valueOf(expires_in) - 10, TimeUnit.SECONDS);
return token;
}
}
package com.wwdz.ch.wx.api;
import com.xxdxxs.utils.XmlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/officialAccountCallback")
public class OfficialAccountController {
private static final Logger logger = LoggerFactory.getLogger(OfficialAccountController.class);
private final static String SUBSCRIBE_EVENT = "subscribe";
private final static String UNSUBSCRIBE_EVENT = "unsubscribe";
@RequestMapping(value ="/getSubscribeNotice")
public String getSubscribeNotice(@RequestBody String xmlInfo){
logger.info("微信通知回调接口原文:{}", xmlInfo);
try {
List<Map<String, String>> list = XmlUtils.fromXml(xmlInfo, "xml");
Map<String, String> contentMap = list.get(0);
String event = contentMap.get("Event");
if ("SUBSCRIBE_EVENT".equals(event) || UNSUBSCRIBE_EVENT.equals(event)) {
String userOpenId = contentMap.get("FromUserName");
String createTime = contentMap.get("CreateTime");
Date date = new Date(createTime);
}
} catch (Exception e) {
logger.error("微信通知回调接口 error : {}", e);
}
return "";
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment