package com.yanzhu.system.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信配置
 */
@Configuration
public class WeChatConfig {

    @Autowired
    private WeChatProperties weChatProperties;

    private static final Logger log = LoggerFactory.getLogger(WeChatConfig.class);

    /**
     * 公众号
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        log.info("weChatProperties.mpAppId...{}",weChatProperties.getMpAppId());
        log.info("weChatProperties.mpAppSecret...{}",weChatProperties.getMpAppSecret());
        config.setAppId(weChatProperties.getMpAppId());
        config.setSecret(weChatProperties.getMpAppSecret());

        WxMpServiceImpl wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(config);

        return wxMpService;
    }

    /**
     * 小程序
     * @return
     */
    @Bean
    public WxMaService wxMaService() {
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        log.info("weChatProperties.maAppId...{}",weChatProperties.getMaAppId());
        log.info("weChatProperties.maAppSecret...{}",weChatProperties.getMaAppSecret());
        wxMaConfig.setAppid(weChatProperties.getMaAppId());
        wxMaConfig.setSecret(weChatProperties.getMaAppSecret());

        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaConfig);

        return wxMaService;
    }
}
