自定义菜单处理

This commit is contained in:
zeek 2020-03-21 18:29:30 +08:00
parent fd673ed1e6
commit fe5f36186d
7 changed files with 116 additions and 20 deletions

View File

@ -5,15 +5,12 @@ import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
/** /**
* @author zeekling [lingzhaohui@zeekling.cn] * @author zeekling [lingzhaohui@zeekling.cn]
@ -26,18 +23,18 @@ import java.util.stream.Collectors;
public class WeChatBeans { public class WeChatBeans {
@Resource @Resource
private WeXinConfigure weXinConfigure; private WeChatConfigure weChatConfigure;
@Bean @Bean
public WxMpService wxMpService() { public WxMpService wxMpService() {
WxMpService service = new WxMpServiceImpl(); WxMpService service = new WxMpServiceImpl();
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl(); WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
configStorage.setAppId(weXinConfigure.getAppId()); configStorage.setAppId(weChatConfigure.getAppId());
configStorage.setSecret(weXinConfigure.getSecret()); configStorage.setSecret(weChatConfigure.getSecret());
configStorage.setToken(weXinConfigure.getToken()); configStorage.setToken(weChatConfigure.getToken());
configStorage.setAesKey(weXinConfigure.getAesKey()); configStorage.setAesKey(weChatConfigure.getAesKey());
Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>(); Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>();
configStorageMap.put(weXinConfigure.getAppId(), configStorage); configStorageMap.put(weChatConfigure.getAppId(), configStorage);
service.setMultiConfigStorages(configStorageMap); service.setMultiConfigStorages(configStorageMap);
return service; return service;
} }

View File

@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
* @since 2020-03-18 * @since 2020-03-18
*/ */
@Component @Component
public class WeXinConfigure { public class WeChatConfigure {
private String appId; private String appId;

View File

@ -1,7 +1,8 @@
package com.zeekling.solo.wechat.controller; package com.zeekling.solo.wechat.controller;
import com.zeekling.solo.wechat.conf.WeXinConfigure; import com.zeekling.solo.wechat.conf.WeChatConfigure;
import com.zeekling.solo.wechat.entity.Validate; import com.zeekling.solo.wechat.entity.Validate;
import com.zeekling.solo.wechat.service.WeChatMenuService;
import com.zeekling.solo.wechat.verify.SHA1; import com.zeekling.solo.wechat.verify.SHA1;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -23,14 +24,14 @@ public class WeXinController {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass());
@Resource @Resource
private WeXinConfigure wechatConfig; private WeChatConfigure weChatConfigure;
@Resource @Resource
private WxMpService wxMpService; private WeChatMenuService weChatMenuService;
@RequestMapping(value = "/validate") @RequestMapping(value = "/validate")
public String validate(Validate validate) { public String validate(Validate validate) {
String token = wechatConfig.getToken(); String token = weChatConfigure.getToken();
String signature = SHA1.getSHA1(token, validate.getTimestamp(), validate.getNonce()); String signature = SHA1.getSHA1(token, validate.getTimestamp(), validate.getNonce());
// 3.字符串校验 // 3.字符串校验
if (validate.getSignature().equals(signature)) { if (validate.getSignature().equals(signature)) {
@ -43,8 +44,15 @@ public class WeXinController {
} }
} }
@RequestMapping(value = "/menu/create")
public String createMenu(){
return this.weChatMenuService.create();
}
@RequestMapping(value = "/menu/get")
public String getMenu(){
return this.weChatMenuService.get();
}
} }

View File

@ -8,6 +8,14 @@ package com.zeekling.solo.wechat.service;
*/ */
public interface WeChatMenuService { public interface WeChatMenuService {
/**
* 根据配置文件menu.json文件创建对应的菜单
*
* @return 创建菜单是否成功
*/
String create();
String get();
} }

View File

@ -1,16 +1,78 @@
package com.zeekling.solo.wechat.service; package com.zeekling.solo.wechat.service;
import com.zeekling.solo.wechat.conf.WeChatConfigure;
import com.zeekling.solo.wechat.util.FileUtils;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author zeekling [lingzhaohui@zeekling.cn] * @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0 * @version 1.0
* @apiNote * @apiNote 微信菜单相关处理
* @since 2020-03-21 * @since 2020-03-21
*/ */
@Service @Service
public class WeChatMenuServiceImpl implements WeChatMenuService { public class WeChatMenuServiceImpl implements WeChatMenuService {
private static final Logger LOG = LoggerFactory.getLogger(WeChatMenuServiceImpl.class);
@Resource
private WxMpService wxMpService;
@Resource
private WeChatConfigure weChatConfigure;
private JSONObject menuInfo = null;
@PostConstruct
private void init(){
menuInfo = FileUtils.readJsonFile(weChatConfigure.getMenuFilePath());
this.wxMpService.switchover(weChatConfigure.getAppId());
}
@Override
public String create() {
if (menuInfo == null){
return "{}";
}
try {
JSONArray buttonArray = menuInfo.getJSONArray("button");
if (buttonArray == null || buttonArray.length() == 0){
return "{}";
}
return this.wxMpService.getMenuService().menuCreate(menuInfo.toString());
} catch (JSONException | WxErrorException e) {
LOG.warn("error in create menu, error:{}", e);
}
return "{}";
}
@Override
public String get() {
try {
return this.wxMpService.getMenuService().menuGet().toJson();
} catch (WxErrorException e) {
e.printStackTrace();
}
return null;
}
} }

View File

@ -27,7 +27,7 @@ public final class FileUtils {
* @param filePath 文件地址 * @param filePath 文件地址
* @return 文件内容 * @return 文件内容
*/ */
public String readFile(String filePath) { public static String readFile(String filePath) {
StrBuilder sb = new StrBuilder(); StrBuilder sb = new StrBuilder();
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
try { try {
@ -49,7 +49,7 @@ public final class FileUtils {
* @param filePath 文件路径 * @param filePath 文件路径
* @return 转换后的json对象 * @return 转换后的json对象
*/ */
public JSONObject readJsonFile(String filePath) { public static JSONObject readJsonFile(String filePath) {
String str = readFile(filePath); String str = readFile(filePath);
try { try {
@ -57,7 +57,7 @@ public final class FileUtils {
} catch (Exception e) { } catch (Exception e) {
LOG.warn("str to json error,str:{}, error:{}", str, e); LOG.warn("str to json error,str:{}, error:{}", str, e);
} }
return null; return new JSONObject();
} }
} }

View File

@ -0,0 +1,21 @@
package com.zeekling.solo.wechat.utils;
import com.zeekling.solo.wechat.util.FileUtils;
import org.junit.Test;
/**
* @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0
* @apiNote
* @since 2020-03-21
*/
public class FileUtilsTest {
@Test
public void readJsonFile(){
String filePath = "/home/zeek/project/solo-weixin/src/main/resources/menu.json";
System.out.println(FileUtils.readJsonFile(filePath).toString());
}
}