All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.zopen.wechat.mp.task.WechatMpTask Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
package com.zopen.wechat.mp.task;

import com.zcj.util.UtilString;
import com.zopen.ato.properties.WechatMpProperties;
import com.zopen.wechat.mp.service.WechatMpAccessTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component("atoWechatTask")
@EnableScheduling
public class WechatMpTask {

    private static final Logger logger = LoggerFactory.getLogger(WechatMpTask.class);

    @Autowired
    private WechatMpAccessTokenService wechatMpAccessTokenService;
    @Autowired
    private WechatMpProperties wechatMpProperties;

    @Scheduled(fixedRate = 1 * 60 * 60 * 1000)
    private void refreshAccessToken() {
        logger.info("开始刷新所有 access_token");
        for (WechatInfo wechatInfo : WechatInfo.getOpenIdInfoMap().values()) {
            try {
                wechatMpAccessTokenService.init(wechatInfo.getAppId(), wechatInfo.getAppSecret());
            } catch (Exception e) {
                logger.warn("刷新 access_token 失败,app_id[{}],错误原因[{}]", wechatInfo.getAppId(), e.getMessage());
            }
        }
        String appId = wechatMpProperties.getAppId();
        String appSecret = wechatMpProperties.getAppSecret();
        if (UtilString.isNotBlank(appId) && UtilString.isNotBlank(appSecret)) {
            if (WechatInfo.getOpenIdInfoMap().get(appId) == null) {
                wechatMpAccessTokenService.init(appId, appSecret);
            }
        }
        logger.info("刷新所有 access_token 结束");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy