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

74 lines
1.9 KiB
Java

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.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 javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* @author zeekling [lingzhaohui@zeekling.cn]
* @version 1.0
* @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 "{}";
}
System.out.println(menuInfo);
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;
}
}