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;
/**
* @ClassName WechatUtil
*/
@Component
public class WechatUtil {
private static Logger logger = LoggerFactory.getLogger(WechatUtil.class);
@Resource
private WeChatClient weChatClient;
// 维护一个本类的静态变量
private static WechatUtil wechatUtil;
// 初始化的时候,将本类中的weChatClient赋值给静态的本类变量
@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;
}
}
}
静态方法使用@Resource注入的变量
最新推荐文章于 2024-08-10 11:38:50 发布