package com.ruoyi.weixin.service;

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.ruoyi.common.exception.ServiceException;
import com.ruoyi.weixin.config.WxMaConfiguration;
import com.ruoyi.weixin.domain.WxUserDto;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author wc
 */
@Slf4j
@Service
public class WxUserService {

    /**
     * 微信app id
     */
    @Value("${wx.miniapp.configs[0].appid}")
    private String appid;

    /**
     * 获取openid
     *
     * @param wxUserDto
     * @return
     */
    public WxUserDto getOpenId(WxUserDto wxUserDto) {
        if (StringUtils.isBlank(wxUserDto.getCode())) {
            return null;
        }
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(wxUserDto.getCode());
//            log.info(session.getSessionKey());
//            log.info(session.getOpenid());
            //TODO 可以增加自己的逻辑，关联业务相关数据
            wxUserDto.setSessionKey(session.getSessionKey());
            wxUserDto.setOpenId(session.getOpenid());
            return wxUserDto;
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 获取openid
     *
     * @param code
     * @return
     */
    public String getOpenIdByCode(String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            return session.getOpenid();
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

    /**
     * 获取用户信息接口
     *
     * @param dto
     * @return
     */
    public WxMaUserInfo getInfo(WxUserDto dto) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(dto.getSessionKey(), dto.getRawData(), dto.getSignature())) {
            throw new ServiceException("user check failed");
        }

        // 解密用户信息
        return wxService.getUserService().getUserInfo(dto.getSessionKey(), dto.getEncryptedData(), dto.getIv());
    }

    /**
     * 获取用户手机号
     *
     * @param dto
     * @return
     */
    public WxMaPhoneNumberInfo getPhone(WxUserDto dto) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(dto.getSessionKey(), dto.getRawData(), dto.getSignature())) {
            throw new ServiceException("user check failed");
        }

        // 解密用户信息
        return wxService.getUserService().getPhoneNoInfo(dto.getSessionKey(), dto.getEncryptedData(), dto.getIv());
    }

    /**
     * 通过Code获取用户手机号
     *
     * @param code
     * @return
     */
    public WxMaPhoneNumberInfo getPhoneByCode(String code) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        WxMaPhoneNumberInfo newPhoneNoInfo = null;
        try {
            newPhoneNoInfo = wxService.getUserService().getNewPhoneNoInfo(code);
        } catch (WxErrorException e) {
            throw new ServiceException(e.getMessage());
        }

        return newPhoneNoInfo;
    }
}
