自定义菜单处理
This commit is contained in:
parent
fd673ed1e6
commit
fe5f36186d
@ -5,15 +5,12 @@ import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
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.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author zeekling [lingzhaohui@zeekling.cn]
|
||||
@ -26,18 +23,18 @@ import java.util.stream.Collectors;
|
||||
public class WeChatBeans {
|
||||
|
||||
@Resource
|
||||
private WeXinConfigure weXinConfigure;
|
||||
private WeChatConfigure weChatConfigure;
|
||||
|
||||
@Bean
|
||||
public WxMpService wxMpService() {
|
||||
WxMpService service = new WxMpServiceImpl();
|
||||
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
|
||||
configStorage.setAppId(weXinConfigure.getAppId());
|
||||
configStorage.setSecret(weXinConfigure.getSecret());
|
||||
configStorage.setToken(weXinConfigure.getToken());
|
||||
configStorage.setAesKey(weXinConfigure.getAesKey());
|
||||
configStorage.setAppId(weChatConfigure.getAppId());
|
||||
configStorage.setSecret(weChatConfigure.getSecret());
|
||||
configStorage.setToken(weChatConfigure.getToken());
|
||||
configStorage.setAesKey(weChatConfigure.getAesKey());
|
||||
Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>();
|
||||
configStorageMap.put(weXinConfigure.getAppId(), configStorage);
|
||||
configStorageMap.put(weChatConfigure.getAppId(), configStorage);
|
||||
service.setMultiConfigStorages(configStorageMap);
|
||||
return service;
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ import org.springframework.stereotype.Component;
|
||||
* @since 2020-03-18
|
||||
*/
|
||||
@Component
|
||||
public class WeXinConfigure {
|
||||
public class WeChatConfigure {
|
||||
|
||||
private String appId;
|
||||
|
@ -1,7 +1,8 @@
|
||||
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.service.WeChatMenuService;
|
||||
import com.zeekling.solo.wechat.verify.SHA1;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import org.slf4j.Logger;
|
||||
@ -23,14 +24,14 @@ public class WeXinController {
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Resource
|
||||
private WeXinConfigure wechatConfig;
|
||||
private WeChatConfigure weChatConfigure;
|
||||
|
||||
@Resource
|
||||
private WxMpService wxMpService;
|
||||
private WeChatMenuService weChatMenuService;
|
||||
|
||||
@RequestMapping(value = "/validate")
|
||||
public String validate(Validate validate) {
|
||||
String token = wechatConfig.getToken();
|
||||
String token = weChatConfigure.getToken();
|
||||
String signature = SHA1.getSHA1(token, validate.getTimestamp(), validate.getNonce());
|
||||
// 3.字符串校验
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,14 @@ package com.zeekling.solo.wechat.service;
|
||||
*/
|
||||
public interface WeChatMenuService {
|
||||
|
||||
/**
|
||||
* 根据配置文件menu.json文件创建对应的菜单
|
||||
*
|
||||
* @return 创建菜单是否成功
|
||||
*/
|
||||
String create();
|
||||
|
||||
String get();
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,16 +1,78 @@
|
||||
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.util.StringUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author zeekling [lingzhaohui@zeekling.cn]
|
||||
* @version 1.0
|
||||
* @apiNote
|
||||
* @apiNote 微信菜单相关处理
|
||||
* @since 2020-03-21
|
||||
*/
|
||||
@Service
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public final class FileUtils {
|
||||
* @param filePath 文件地址
|
||||
* @return 文件内容
|
||||
*/
|
||||
public String readFile(String filePath) {
|
||||
public static String readFile(String filePath) {
|
||||
StrBuilder sb = new StrBuilder();
|
||||
byte[] buffer = new byte[1024];
|
||||
try {
|
||||
@ -49,7 +49,7 @@ public final class FileUtils {
|
||||
* @param filePath 文件路径
|
||||
* @return 转换后的json对象
|
||||
*/
|
||||
public JSONObject readJsonFile(String filePath) {
|
||||
public static JSONObject readJsonFile(String filePath) {
|
||||
String str = readFile(filePath);
|
||||
|
||||
try {
|
||||
@ -57,7 +57,7 @@ public final class FileUtils {
|
||||
} catch (Exception e) {
|
||||
LOG.warn("str to json error,str:{}, error:{}", str, e);
|
||||
}
|
||||
return null;
|
||||
return new JSONObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user