package com.fykj.scaffold.config;

import com.fykj.scaffold.config.properties.WxCpProperties;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 微信企业号配置类
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfig {

    @Autowired
    private WxCpProperties properties;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Bean
    public WxCpService wxCpService() {
        WxCpDefaultConfigImpl config = new WxCpRedisTemplateConfigImpl(redisTemplate, "weixin-cp:");
        config.setCorpId(properties.getCorpId());
        config.setCorpSecret(properties.getCorpSecret());
        config.setToken(properties.getToken());
        config.setAesKey(properties.getAesKey());
        config.setAgentId(properties.getAgentId());
        config.setBaseApiUrl(properties.getBaseUri());
        WxCpService service = new WxCpServiceImpl();
        service.setWxCpConfigStorage(config);
        return service;
    }
}
