package com.ruoyi.front.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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;


/**
 * 微信小程序配置类
 */
@Slf4j
@Configuration
@ConditionalOnBean(WxMaProperties.class)
@RequiredArgsConstructor
public class WxMaConfig {
    private final WxMaProperties properties;

    @PostConstruct
    public void init() {
        log.info("WX miniapp service   initialized  successfully");
    }

    @Bean
    public WxMaService wxMaService() {
        WxMaService maService = new WxMaServiceImpl();
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(properties.getAppId());
        config.setSecret(properties.getSecret());
        config.setToken(properties.getToken());
        config.setAesKey(properties.getAesKey());
        config.setMsgDataFormat(properties.getMsgDataFormat());
        maService.setWxMaConfig(config);
        return maService;
    }

}

