|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.caimei365.wechat.dao.WeChatDao;
|
|
import com.caimei365.wechat.dao.WeChatDao;
|
|
import com.caimei365.wechat.entity.WeChatConstant;
|
|
import com.caimei365.wechat.entity.WeChatConstant;
|
|
|
|
+import com.caimei365.wechat.entity.WeChatUser;
|
|
import com.caimei365.wechat.entity.WechatArticleDetail;
|
|
import com.caimei365.wechat.entity.WechatArticleDetail;
|
|
import com.caimei365.wechat.entity.WechatReply;
|
|
import com.caimei365.wechat.entity.WechatReply;
|
|
import com.caimei365.wechat.service.WechatServerService;
|
|
import com.caimei365.wechat.service.WechatServerService;
|
|
@@ -34,6 +35,8 @@ import java.util.Map;
|
|
@Service
|
|
@Service
|
|
public class WechatServerServiceImpl implements WechatServerService {
|
|
public class WechatServerServiceImpl implements WechatServerService {
|
|
|
|
|
|
|
|
+ @Value("${wechat.apiUrl}")
|
|
|
|
+ private String wechatApiUrl;
|
|
@Value("${caimei.wwwDomain}")
|
|
@Value("${caimei.wwwDomain}")
|
|
private String wwwDomain;
|
|
private String wwwDomain;
|
|
@Value("${caimei.coreDomain}")
|
|
@Value("${caimei.coreDomain}")
|
|
@@ -236,6 +239,37 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
public String handleSubscribeMsg(Map<String, String> requestMap) {
|
|
public String handleSubscribeMsg(Map<String, String> requestMap) {
|
|
// 用户openid
|
|
// 用户openid
|
|
String openid = requestMap.get(WeChatConstant.FROM_USER_NAME);
|
|
String openid = requestMap.get(WeChatConstant.FROM_USER_NAME);
|
|
|
|
+ String unionId = null;
|
|
|
|
+ WeChatUser user = weChatDao.getWeChatUserByOpenid(openid);
|
|
|
|
+ if (null == user || !StringUtils.hasLength(user.getUnionId())){
|
|
|
|
+ try {
|
|
|
|
+ // 获取 access_token
|
|
|
|
+ String url = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + caimeiAppid + "&secret=" + caimeiSecret;
|
|
|
|
+ String result1 = HttpUtil.httpRequest(url, "GET", null);
|
|
|
|
+ JSONObject parse = JSONObject.parseObject(result1, JSONObject.class);
|
|
|
|
+ String token = parse.getString("access_token");
|
|
|
|
+ // 获取用户unionId
|
|
|
|
+ String unionIdUrl = wechatApiUrl + "/cgi-bin/user/info?access_token="+ token + "&openid=" + openid + "&lang=zh_CN";
|
|
|
|
+ String result2 = HttpUtil.httpRequest(unionIdUrl, "GET", null);
|
|
|
|
+ log.info("openid:" + openid + ",获取用户unionId:" + result2);
|
|
|
|
+ JSONObject parse2 = JSONObject.parseObject(result2, JSONObject.class);
|
|
|
|
+ unionId = parse2.getString("unionid");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("获取unionId失败:", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 保存关注用户
|
|
|
|
+ if (null == user) {
|
|
|
|
+ user = new WeChatUser();
|
|
|
|
+ user.setOpenid(openid);
|
|
|
|
+ user.setUnionId(unionId);
|
|
|
|
+ weChatDao.insertWeChatUser(user);
|
|
|
|
+ } else {
|
|
|
|
+ if (!StringUtils.hasLength(user.getUnionId())) {
|
|
|
|
+ user.setUnionId(unionId);
|
|
|
|
+ weChatDao.updateWeChatUser(user);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
// 微信号公众号Id
|
|
// 微信号公众号Id
|
|
String wxType = requestMap.get(WeChatConstant.TO_USER_NAME);
|
|
String wxType = requestMap.get(WeChatConstant.TO_USER_NAME);
|
|
log.info(">>>>>>>>>>处理接收的订阅关注事件,openid:" + openid + ",wxType:" + wxType);
|
|
log.info(">>>>>>>>>>处理接收的订阅关注事件,openid:" + openid + ",wxType:" + wxType);
|