package com.iformall.utils;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import com.iformall.common.FmHttpClientBuilder;
import com.iformall.domain.po.WxAppinfo;
import org.apache.commons.lang3.StringUtils;

public class MaUtil {

    static public WxMaService getWeappService(WxAppinfo appinfo) {
        WxMaInMemoryConfig config = new WxMaInMemoryConfig();
        config.setAppid(appinfo.getAppId());
        config.setSecret(appinfo.getSecret());
        config.setToken(appinfo.getToken());
        config.setAesKey(appinfo.getAesKey());
        config.setMsgDataFormat(appinfo.getMsgDataFormat());
        if (StringUtils.isNotBlank(appinfo.getAccessToken())) {
            config.setAccessToken(appinfo.getAccessToken());
            config.setExpiresTime(appinfo.getLastTokenTime().getTime()+appinfo.getExpiresIn()*1000);
        }
        config.setApacheHttpClientBuilder(FmHttpClientBuilder.get());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }
}
