package cn.axzo.msg.center.wx.notices.miniapp;

import cn.axzo.msg.center.wx.notices.compont.WxMaProcessor;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 微信小程序消息处理器
 *
 * @author cn
 * @version 1.0
 * @description
 * @date 2023/5/25 16:12
 */
@Service
@RequiredArgsConstructor
public class WxMiniMessageHandler {
    @Resource
    private WxMaProcessor wxProcessor;


    public WxMiniMessageHandler switchConfig(String appid, String secret) {
        WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
        wxMaConfig.setAppid(appid);
        wxMaConfig.setSecret(secret);
//        wxMaConfig.setAccessToken(token);
        //修改过过期时间为5分钟
//        wxMaConfig.setExpiresTime(System.currentTimeMillis() + 300000);
        wxProcessor.setWxMaConfig(wxMaConfig);
        wxProcessor.switchoverTo(appid);
        return this;
    }


    public void sendSubscribeMsg(WxMaSubscribeMessage msg) throws WxErrorException {
        wxProcessor.getMsgService().sendSubscribeMsg(msg);
    }
}
