|
@@ -11,6 +11,7 @@ import com.caimei365.wechat.service.WechatServerService;
|
|
|
import com.caimei365.wechat.utils.HttpUtil;
|
|
|
import com.caimei365.wechat.utils.MessageUtil;
|
|
|
import com.caimei365.wechat.utils.SignUtil;
|
|
|
+import com.caimei365.wechat.utils.WechatType;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -88,7 +89,9 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
* </xml>
|
|
|
*/
|
|
|
@Override
|
|
|
- public String handleTextMsg(Map<String, String> requestMap) {
|
|
|
+ public String handleTextMsg(String type, Map<String, String> requestMap) {
|
|
|
+ WechatType wechatType = WechatType.initWechatType(type);
|
|
|
+
|
|
|
// 根据接收的关键字回复
|
|
|
String keyword = requestMap.get(WeChatConstant.CONTENT);
|
|
|
// 用户openid
|
|
@@ -97,7 +100,7 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
String wxType = requestMap.get(WeChatConstant.TO_USER_NAME);
|
|
|
log.info(">>>>>>>>>>处理接收的文本消息,keyword:" + keyword + ",openid:" + openid + ",wxType:" + wxType);
|
|
|
// 根据口令获取用户openid
|
|
|
- if("caimei:get_openid".equals(keyword)) {
|
|
|
+ if ("caimei:get_openid".equals(keyword)) {
|
|
|
return MessageUtil.setTextXml(openid, wxType, openid);
|
|
|
}
|
|
|
// 根据keyword匹配数据库回复配置
|
|
@@ -109,16 +112,19 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
return replyXml;
|
|
|
}
|
|
|
}
|
|
|
+ if ("2".equals(wechatType.getTYPE())) {
|
|
|
+ return MessageUtil.setTextXml(openid, wxType, "小颜已经收到您的消息,会尽快回复哦!");
|
|
|
+ }
|
|
|
log.info(">>>>>>>>>>模糊搜索:" + keyword);
|
|
|
// 搜索商品: https://core.caimei365.com/commodity/search/query/product?keyword=
|
|
|
WechatArticleDetail article = null;
|
|
|
try {
|
|
|
- String uri = coreDomain + "/commodity/search/query/product?keyword="+URLEncoder.encode(keyword, "UTF-8" );
|
|
|
+ String uri = coreDomain + "/commodity/search/query/product?keyword=" + URLEncoder.encode(keyword, "UTF-8");
|
|
|
String jsonResult = HttpUtil.sendGet(uri);
|
|
|
- if(StringUtils.hasLength(jsonResult)){
|
|
|
+ if (StringUtils.hasLength(jsonResult)) {
|
|
|
JSONObject jsonObject = JSON.parseObject(jsonResult);
|
|
|
String data = jsonObject.getString("data");
|
|
|
- if(StringUtils.hasLength(data)){
|
|
|
+ if (StringUtils.hasLength(data)) {
|
|
|
JSONObject parse = JSONObject.parseObject(data);
|
|
|
if(parse != null){
|
|
|
Integer total = parse.getInteger("total");
|
|
@@ -130,7 +136,7 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
try {
|
|
|
url = URLEncoder.encode(url, "UTF-8");
|
|
|
} catch (UnsupportedEncodingException ignored) {}
|
|
|
- article.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + caimeiAppid + "&redirect_uri=" + url +"&response_type=code&scope=snsapi_base&state=reLogin#wechat_redirect");
|
|
|
+ article.setUrl("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wechatType.getAPPID() + "&redirect_uri=" + url + "&response_type=code&scope=snsapi_base&state=reLogin#wechat_redirect");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -236,22 +242,23 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
* </xml>
|
|
|
*/
|
|
|
@Override
|
|
|
- public String handleSubscribeMsg(Map<String, String> requestMap) {
|
|
|
+ public String handleSubscribeMsg(String type, Map<String, String> requestMap) {
|
|
|
// 用户openid
|
|
|
String openid = requestMap.get(WeChatConstant.FROM_USER_NAME);
|
|
|
+ WechatType wechatType = WechatType.initWechatType(type);
|
|
|
String unionId = null;
|
|
|
- WeChatUser user = weChatDao.getWeChatUserByOpenid(openid);
|
|
|
- if (null == user || !StringUtils.hasLength(user.getUnionId())){
|
|
|
+ WeChatUser user = weChatDao.getWeChatUserByOpenid(wechatType.getTYPE(), openid, null);
|
|
|
+ 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 url = wechatApiUrl + "/cgi-bin/token?grant_type=client_credential&appid=" + wechatType.getAPPID() + "&secret=" + wechatType.getAPPSECRET();
|
|
|
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 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);
|
|
|
+ log.info("openid:" + openid + ",获取用户unionId:" + result2 + ",access_token:" + token);
|
|
|
JSONObject parse2 = JSONObject.parseObject(result2, JSONObject.class);
|
|
|
unionId = parse2.getString("unionid");
|
|
|
} catch (Exception e) {
|
|
@@ -259,14 +266,23 @@ public class WechatServerServiceImpl implements WechatServerService {
|
|
|
}
|
|
|
}
|
|
|
// 保存关注用户
|
|
|
- if (null == user) {
|
|
|
- user = new WeChatUser();
|
|
|
- user.setOpenid(openid);
|
|
|
- user.setUnionId(unionId);
|
|
|
- weChatDao.insertWeChatUser(user);
|
|
|
- } else {
|
|
|
- if (!StringUtils.hasLength(user.getUnionId())) {
|
|
|
+ if (null != unionId && (null == user || null == user.getUnionId())) {
|
|
|
+ user = weChatDao.getWeChatUserByOpenid(null, null, unionId);
|
|
|
+ if (null == user) {
|
|
|
+ user = new WeChatUser();
|
|
|
+ if ("2".equals(wechatType.getTYPE())) {
|
|
|
+ user.setHeheOpenid(openid);
|
|
|
+ } else {
|
|
|
+ user.setOpenid(openid);
|
|
|
+ }
|
|
|
user.setUnionId(unionId);
|
|
|
+ weChatDao.insertWeChatUser(user);
|
|
|
+ } else {
|
|
|
+ if ("2".equals(wechatType.getTYPE())) {
|
|
|
+ user.setHeheOpenid(openid);
|
|
|
+ } else {
|
|
|
+ user.setOpenid(openid);
|
|
|
+ }
|
|
|
weChatDao.updateWeChatUser(user);
|
|
|
}
|
|
|
}
|