package com.liquidnet.service.sweet.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.sweet.config.WechatMaConfigure;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SweetWechatService {
    @Autowired
    WechatMaConfigure wechatMaConfigure;

    public WxMaJscode2SessionResult sessionInfo(String code, Integer anum) throws WxErrorException {
        WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
        log.info("isAccessTokenExpired：[{}] ", wxMaService.getWxMaConfig().isAccessTokenExpired());
        log.info("getAccessToken：[{}] ", wxMaService.getWxMaConfig().getAccessToken());
        return wxMaService.getUserService().getSessionInfo(code);
    }

    public WxMaUserInfo userInfo(String sessionKey, String encryptedData, String iv, Integer anum) {
        WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
        return wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
    }

    public WxMaPhoneNumberInfo phoneNumberInfo(String sessionKey, String encryptedData, String iv, Integer anum) {
        WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
        return wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
    }
}
