package com.ywt.alipaympapi.service;

import com.alibaba.fastjson.JSON;
import com.ptc.board.citymsg.sdk.UserAccountApi;
import com.ptc.board.citymsg.sdk.response.UserAccountInfo;
import com.ywt.alipaympapi.core.utils.Checker;
import com.ywt.alipaympapi.models.Constants;
import com.ywt.alipaympapi.models.auth.AlipayMpCfg;
import com.ywt.alipaympapi.models.auth.GetAlipayUserInfoReq;
import com.ywt.alipaympapi.models.auth.GetAlipayUserInfoResp;
import com.ywt.alipaympapi.models.enums.TerminalEnum;
import com.ywt.core.exception.AppMessageException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author Walker
 * Created on 2022/9/1
 */
@Slf4j
@Service
public class AuthService {

    /**
     * 换取支付宝用户信息
     */
    public GetAlipayUserInfoResp getAlipayUserInfo(GetAlipayUserInfoReq req) throws AppMessageException {
        GetAlipayUserInfoResp resp = new GetAlipayUserInfoResp();
        try {
            UserAccountApi api = new UserAccountApi("https", "ab-saas-api.alipay-eco.com", null, req.getSaasSecurityKey());
            UserAccountInfo userAccountInfo = api.queryUserInfo(req.getAppId(), req.getTicket());
            log.info("AuthService#getAlipayUserInfo(req={} ): 获取到的用户信息：{}", JSON.toJSONString(req),
                    JSON.toJSONString(userAccountInfo));
            resp.setAlipayUid(userAccountInfo.getAlipayUid());
            resp.setAvatar(userAccountInfo.getAvatar());
            resp.setRealName(userAccountInfo.getFullName());
            resp.setNickName(userAccountInfo.getNickName());
            resp.setMobile(userAccountInfo.getPhone());
            resp.setAge(userAccountInfo.getAge());
            resp.setIdCardNo(userAccountInfo.getIdcardNo());
        } catch (Exception e) {
            log.error("AuthService#getAlipayUserInfo(req={} ):\n {}", req, e.getMessage(), e);
            throw new AppMessageException(e.getMessage());
        }
        return resp;
    }


}
