How does jfinal-weixin support official account configuration?

Time:2021-5-6

The official account of official account is spring-boot-starter-weixin, which is a spring boot version of jfinal-weixin. It has been using jfinal-weixin as development kit before, and has never tried to support the multi official account. Recently, there is a demand for support of the multi public number.

Find the source code, open it and find an interceptor. Let’s see what the interceptor does.

How does jfinal-weixin support official account configuration?

How to configure WeChat backstage to support multi official account

There are several important lines of code in prehandle. Let’s analyze them separately.

String appId = request.getParameter(weixinProperties.getAppIdKey());
//Weixinproperties class
private String appIdKey = "appId";

This line of code is important to obtain parameters, which are added when we configure the developer URL in the wechat background. The parameter name is appid.

// determine whether the official account is public, bind appId to the current thread, so as to facilitate access to ApiConfig objects in subsequent operations:
Object bean = handlerMethod.getBean();
boolean isWx = bean instanceof MsgController;
String token;
if (isWx) {
    if (StringUtils.hasText(appId)) {
        ApiConfigKit.setThreadLocalAppId(appId);
        token = ApiConfigKit.getApiConfig(appId).getToken();
    } else {
        token = ApiConfigKit.getApiConfig().getToken();
    }
} else {
    token = WxaConfigKit.getWxaConfig().getToken();
}

This code is mainly based on the appId from WeChat backstage to get the token corresponding to WeChat official account, to match the token that WeChat has sent back.

The official account is provided as follows:

How does jfinal-weixin support official account configuration?

How to support multi official account

In msgeinterceptor, you can use apiconfigkit. Getapiconfig(). Let’s open this class to see what he has done.

public static ApiConfig getApiConfig(String appId) {
    log.debug("appId: " + appId);
    ApiConfig cfg = (ApiConfig)CFG_MAP.get(appId);
    if (cfg == null) {
        Throw new IllegalStateException ("you need to call apiconfigkit. Putapiconfig (apiconfig) in advance               
             The ApiConfig object corresponding to appId is stored in, such as JFinalConfig.afterJFinalStart (), which can be used.             
             Apiconfigkit. Getapiconfig() series methods');
    } else {
        return cfg;
    }
}

According to the custom exception information, we know that if we want to use getApiConfig (String appId), we must first call putApiConfig (apiConfig) to store official account information.

@Configuration
@AllArgsConstructor
public class WeixinAppConfig implements SmartInitializingSingleton {
    private final DreamWeixinProperties weixinProperties;
    private final SpringAccessTokenCache accessTokenCache;

    @Override
    public void afterSingletonsInstantiated() {
        boolean isdev = weixinProperties.isDevMode();
        ApiConfigKit.setDevMode(isdev);
        ApiConfigKit.setAccessTokenCache(accessTokenCache);
        List<DreamWeixinProperties.ApiConfig> list = weixinProperties.getWxConfigs();
        for (DreamWeixinProperties.ApiConfig apiConfig : list) {
            ApiConfig config = new ApiConfig();
            if (StrKit.notBlank(apiConfig.getAppId())) {
                config.setAppId(apiConfig.getAppId());
            }
            if (StrKit.notBlank(apiConfig.getAppSecret())) {
                config.setAppSecret(apiConfig.getAppSecret());
            }
            if (StrKit.notBlank(apiConfig.getToken())) {
                config.setToken(apiConfig.getToken());
            }
            if (StrKit.notBlank(apiConfig.getEncodingAesKey())) {
                config.setEncodingAesKey(apiConfig.getEncodingAesKey());
            }
            config.setEncryptMessage(apiConfig.isMessageEncrypt());
            ApiConfigKit.putApiConfig(config);
        }
        //Omit applet configuration
    }
}
  • After singletons instantiated: after all singletons are initialized, the container will call back the method of the interface

The purpose of this class is to extract the WeChat official account configuration file from the configuration file after the project is launched, and put it into ApiConfigKit.

The configuration of dreamweixinproperties is as follows:

@Getter
@Setter
@ConfigurationProperties("dream.weixin")
public class DreamWeixinProperties {

    /**
     *Intercepted route, default / Weixin/*
     */
    private String urlPatterns = "/weixin/*";
    /**    
     *Development mode or not, default: false
     */
    private boolean devMode = false;
    /**
     * multi official account URL linked by default, appId
     */
    private String appIdKey = "appId";
    /**
     * multi official account configuration
     */
    private List<ApiConfig> wxConfigs = new ArrayList<>();

    @Getter
    @Setter
    public static class ApiConfig {
        private String token;
        private String appId;
        private String appSecret;
        private String encodingAesKey;
        //Message encryption or not
        private boolean messageEncrypt = false;
    }
}

Multi official account configuration

Official account official account is needed to manage public numbers on the platform. Therefore, it is much more convenient to get official account numbers from database than to obtain configuration files. We only need to implement SmartInitializingSingleton interface, and we can change public number configuration to database.

@Configuration
@AllArgsConstructor
public class WxConfig implements SmartInitializingSingleton {

    private final SpringAccessTokenCache accessTokenCache;

    @Autowired
    private WexinNoDao wexinNoDao;
    @Override
    public void afterSingletonsInstantiated() {
        ApiConfigKit.setDevMode(true);
        ApiConfigKit.setAccessTokenCache(accessTokenCache);
        /**
         *Database query
         */
        List<WeixinNoBean> list = wexinNoDao.wxNoList();
        for (WeixinNoBean apiConfig : list) {
            ApiConfig config = new ApiConfig();
            if (StrKit.notBlank(apiConfig.getAppid())) {
                config.setAppId(apiConfig.getAppid());
            }
            if (StrKit.notBlank(apiConfig.getAppKey())) {
                config.setAppSecret(apiConfig.getAppKey());
            }
            if (StrKit.notBlank(apiConfig.getToken())) {
                config.setToken(apiConfig.getToken());
            }
            if (StrKit.notBlank(apiConfig.getAeskey())) {
                config.setEncodingAesKey(apiConfig.getAeskey());
            }
            config.setEncryptMessage(false);
            ApiConfigKit.putApiConfig(config);
        }
    }
}

This way, when the project starts, we will call our custom WxConfig to complete the configuration of the official account. The database read official account configuration is more flexible than the configuration file direct reading. Therefore, I also recommend that the official account configuration be read directly from the database.

Recommended Today

[reprint] deeply understand proxy and defineproperty

it is reportedVue3.0The official version of will be released this month (August). There is still a transition period from the release to the official project, but we can’t wait until nowVue3Only when you are formally involved in the project can you learn, learn in advance, so that you can master it fasterVue3.0It’s up to you […]