package com.xxx.framework.xxx.common;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxx.framework.xxx.client.WeChatClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
@Component
public class WechatUtil {
private static Logger logger = LoggerFactory.getLogger(WechatUtil.class);
@Resource
private WeChatClient weChatClient;
private static WechatUtil wechatUtil;
@PostConstruct
public void init() {
wechatUtil = this;
wechatUtil.weChatClient = this.weChatClient;
}
public static JSONObject getSessionKeyOrOpenId(String code) {
try {
String clientResult = wechatUtil.weChatClient.getSessionKeyOrOpenId(WxConstants.appid, WxConstants.secret, code, "authorization_code");
JSONObject jsonObject = JSON.parseObject(clientResult);
return jsonObject;
} catch (Exception e) {
logger.error("Get sessionKey or openId exception: {}", e.getMessage());
throw e;
}
}
}