정적 방법 은 @ Resource 가 주입 한 변 수 를 사용 합 니 다.

1306 단어 Java
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;
        }
    }
 }

좋은 웹페이지 즐겨찾기