|
@@ -1,18 +1,28 @@
|
|
package com.miaxis.wx.service.impl;
|
|
package com.miaxis.wx.service.impl;
|
|
|
|
|
|
|
|
+import cn.hutool.json.XML;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.miaxis.common.constant.Constants;
|
|
import com.miaxis.common.constant.Constants;
|
|
-import com.miaxis.common.core.redis.RedisCache;
|
|
|
|
|
|
+import com.miaxis.common.sign.VerifyUtil;
|
|
import com.miaxis.common.utils.StringUtils;
|
|
import com.miaxis.common.utils.StringUtils;
|
|
|
|
+import com.miaxis.common.utils.wx.MessageUtil;
|
|
|
|
+import com.miaxis.feign.service.IWxMpService;
|
|
import com.miaxis.feign.service.IWxSendService;
|
|
import com.miaxis.feign.service.IWxSendService;
|
|
|
|
+import com.miaxis.wx.domain.WxMenu;
|
|
|
|
+import com.miaxis.wx.mapper.WxMenuMapper;
|
|
import com.miaxis.wx.service.IWxGzhService;
|
|
import com.miaxis.wx.service.IWxGzhService;
|
|
|
|
+import com.qcloud.cos.COSClient;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -28,7 +38,13 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
|
|
|
private final RedisTemplate redisTemplate;
|
|
private final RedisTemplate redisTemplate;
|
|
|
|
|
|
- private final IWxSendService wxSendService;
|
|
|
|
|
|
+ private final IWxSendService wxSendService;
|
|
|
|
+
|
|
|
|
+ private final WxMenuMapper wxMenuMapper;
|
|
|
|
+
|
|
|
|
+ private final COSClient cosClient;
|
|
|
|
+
|
|
|
|
+ private final IWxMpService wxMpService;
|
|
|
|
|
|
@Value("${app.appid}")
|
|
@Value("${app.appid}")
|
|
private String appid;
|
|
private String appid;
|
|
@@ -37,6 +53,10 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
private String secret;
|
|
private String secret;
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取微信公众号token
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
@Override
|
|
@Override
|
|
public String getGzhToken() {
|
|
public String getGzhToken() {
|
|
String token = "";
|
|
String token = "";
|
|
@@ -58,4 +78,152 @@ public class WxGzhServiceImpl implements IWxGzhService {
|
|
|
|
|
|
return token;
|
|
return token;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询微信菜单数据
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public JSONObject getWxMenuJson() {
|
|
|
|
+
|
|
|
|
+ JSONObject menuJson = new JSONObject();
|
|
|
|
+ //获取父类
|
|
|
|
+ List<WxMenu> menuList = wxMenuMapper.selectByMap(new HashMap<String,Object>(){{
|
|
|
|
+ put("parent_id",0);
|
|
|
|
+ }});
|
|
|
|
+ JSONArray button=new JSONArray();
|
|
|
|
+ for(WxMenu menu : menuList){
|
|
|
|
+ if(!VerifyUtil.verifyString(menu.getType())){
|
|
|
|
+ JSONObject childButton = new JSONObject();
|
|
|
|
+ childButton.put("name", menu.getMenuName());
|
|
|
|
+ //父类
|
|
|
|
+ JSONArray sub_button = new JSONArray();
|
|
|
|
+ List<WxMenu> childMenuList = wxMenuMapper.selectByMap(new HashMap<String,Object>(){{
|
|
|
|
+ put("parent_id",menu.getId());
|
|
|
|
+ }});
|
|
|
|
+
|
|
|
|
+ for (WxMenu childMenu : childMenuList) {
|
|
|
|
+ sub_button.add(getChildMenuJson(childMenu));
|
|
|
|
+ }
|
|
|
|
+ childButton.put("sub_button", sub_button);
|
|
|
|
+ button.add(childButton);
|
|
|
|
+ }else{
|
|
|
|
+ button.add(getChildMenuJson(menu));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ menuJson.put("button", button);
|
|
|
|
+ return menuJson;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private JSONObject getChildMenuJson(WxMenu menu){
|
|
|
|
+ JSONObject menuJson = new JSONObject();
|
|
|
|
+ menuJson.put("type", menu.getType());
|
|
|
|
+ menuJson.put("name", menu.getMenuName());
|
|
|
|
+ if(menu.getType().equals("view")){
|
|
|
|
+ menuJson.put("url", menu.getUrl());
|
|
|
|
+ }else if(menu.getType().equals("click")){
|
|
|
|
+ menuJson.put("key", menu.getClickKey());
|
|
|
|
+ }else if(menu.getType().equals("miniprogram")){
|
|
|
|
+ menuJson.put("url", menu.getUrl());
|
|
|
|
+ menuJson.put("key", menu.getClickKey());
|
|
|
|
+ menuJson.put("appid", menu.getAppid());
|
|
|
|
+ menuJson.put("pagepath", menu.getPagePath());
|
|
|
|
+ }
|
|
|
|
+ return menuJson;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 微信推送消息处理
|
|
|
|
+ * @param request
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String handlePublicMsg(HttpServletRequest request) {
|
|
|
|
+ try {
|
|
|
|
+ log.info("1....");
|
|
|
|
+ cn.hutool.json.JSONObject decryptMap = XML.toJSONObject(IOUtils.toString(request.getInputStream()));
|
|
|
|
+ log.info("2-----decryptMap------"+decryptMap);
|
|
|
|
+
|
|
|
|
+ //开发者微信号
|
|
|
|
+ String toUserName = decryptMap.getStr("ToUserName");
|
|
|
|
+ //发送方帐号(一个OpenID)
|
|
|
|
+ String fromUserName = decryptMap.getStr("FromUserName");
|
|
|
|
+ // 区分消息类型
|
|
|
|
+ String msgType = decryptMap.getStr("MsgType");
|
|
|
|
+ //回复消息(xml字符串)
|
|
|
|
+ String message = "";
|
|
|
|
+
|
|
|
|
+ // 普通消息
|
|
|
|
+ if (MessageUtil.MESSAGE_TEXT.equals(msgType)) { // 文本消息
|
|
|
|
+ log.info("2.1...");
|
|
|
|
+ String content = decryptMap.getStr("Content");
|
|
|
|
+ if (content.startsWith("你好")){
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, "你好");
|
|
|
|
+ }else if (content.startsWith("傻逼")){
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, "你才是煞笔");
|
|
|
|
+ }else {
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, "文本消息-默认回复信息");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_IMAGE.equals(msgType)) { // 图片消息
|
|
|
|
+ log.info("2.2...");
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, "抱歉,暂时无法识别图片信息!");
|
|
|
|
+ }else if (MessageUtil.MESSAGE_EVENT.equals(msgType)) { // 事件消息
|
|
|
|
+ log.info("3....");
|
|
|
|
+ // 区分事件推送
|
|
|
|
+ String event = decryptMap.getStr("Event");
|
|
|
|
+ if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)) { // 关注事件 或 扫描二维码关注事件
|
|
|
|
+ log.info("3.1...");
|
|
|
|
+ String content = "";
|
|
|
|
+
|
|
|
|
+ //存在Ticket为扫码关注
|
|
|
|
+ if (org.apache.commons.lang3.StringUtils.isNotEmpty(decryptMap.getStr("Ticket"))){
|
|
|
|
+ log.info("3.1.2..");
|
|
|
|
+ content = "扫描二维码关注事件";
|
|
|
|
+ //根据Ticket 推送绑定信息到上级用户
|
|
|
|
+ //从缓存中获取Ticket用户信息
|
|
|
|
+ }else {
|
|
|
|
+ content = "关注事件";
|
|
|
|
+ }
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, content);
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_UNSUBSCRIBE.equals(event)) { // 取消订阅事件
|
|
|
|
+ // todo 处理取消订阅事件
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_SCAN.equals(event)) { // 已关注扫描二维码事件
|
|
|
|
+ log.info("3.2...");
|
|
|
|
+ message = MessageUtil.initText(fromUserName, toUserName, "已关注扫描二维码事件");
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_LOCATION.equals(event)) { // 上报地理位置事件
|
|
|
|
+ // todo 处理上报地理位置事件
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_CLICK.equals(event)) { // 点击菜单拉取消息时的事件推送事件
|
|
|
|
+ //判断事件KEY值,与自定义菜单接口中KEY值对应
|
|
|
|
+ if ("generateTicket".equals(decryptMap.get("EventKey"))){ //获取分销二维码
|
|
|
|
+ message = MessageUtil.initNews(fromUserName,
|
|
|
|
+ toUserName,
|
|
|
|
+ "图文消息信息",
|
|
|
|
+ "title",
|
|
|
|
+ "描述",
|
|
|
|
+ "https://twzd-1305573081.cos.ap-shanghai.myqcloud.com/twzd-test/testTicket/2021/10/25/1635154145629.png",
|
|
|
|
+ "www.baidu.com"
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ } else if (MessageUtil.MESSAGE_VIEW.equals(event)) { // 点击菜单跳转链接时的事件推送
|
|
|
|
+ // todo 处理点击菜单跳转链接时的事件推送
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return message;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("处理微信公众号请求信息,失败", e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
}
|
|
}
|