blog-weixin/src/main/java/com/zeekling/solo/wechat/service/WeChatMenuServiceImpl.java

74 lines
1.9 KiB
Java
Raw Normal View History

2020-03-21 04:54:29 +00:00
package com.zeekling.solo.wechat.service;
2020-03-21 10:29:30 +00:00
import com.zeekling.solo.wechat.conf.WeChatConfigure;
import com.zeekling.solo.wechat.util.FileUtils;
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;
2020-03-21 04:54:29 +00:00
import org.springframework.stereotype.Service;
2020-03-21 10:29:30 +00:00
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
2020-03-21 04:54:29 +00:00
/**
* @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0
2020-03-21 10:29:30 +00:00
* @apiNote 微信菜单相关处理
2020-03-21 04:54:29 +00:00
* @since 2020-03-21
*/
@Service
public class WeChatMenuServiceImpl implements WeChatMenuService {
2020-03-21 10:29:30 +00:00
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 "{}";
2020-10-08 13:06:48 +00:00
}
System.out.println(menuInfo);
2020-03-21 10:29:30 +00:00
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;
}
2020-03-21 04:54:29 +00:00
}