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-12-04 16:41:00 发布
本文详细介绍了一个用于微信小程序登录的工具类实现,通过调用微信官方API获取用户的session_key和openid,利用Spring框架和Fastjson进行数据处理和转换。文章深入解析了代码结构,包括依赖导入、日志记录、组件注解、资源注入等关键环节。
122

被折叠的 条评论
为什么被折叠?



