package com.slodon.b2b2c.model.member.advich;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.net.url.UrlQuery;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slodon.b2b2c.core.config.DomainUrlUtil;
import com.slodon.b2b2c.core.constant.*;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.util.*;
import com.slodon.b2b2c.dto.GpUserInfoDto;
import com.slodon.b2b2c.dto.HttpApiResult;
import com.slodon.b2b2c.dto.OauthTokenDto;
import com.slodon.b2b2c.member.example.MemberExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.model.member.MemberModel;
import com.slodon.b2b2c.model.seller.VendorModel;
import com.slodon.b2b2c.seller.dto.VendorAddDTO;
import com.slodon.b2b2c.seller.dto.VendorRegisterDTO;
import com.slodon.b2b2c.seller.example.VendorExample;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.member.GpSystemVO;
import com.slodon.b2b2c.vo.member.SsoMemberOauthVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.config.NamespaceUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author cyan
 */
@Component
@Slf4j
public class OneClickLoginModel {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private MemberModel memberModel;

    @Resource
    private VendorModel vendorModel;

    @Resource
    private RedisTemplate<String, Object> objectRedisTemplate;

    /**
     * 检测接口url状态
     *
     * @param urlStr
     * @return
     */
    public String getApiUrlStatus(String urlStr) {
        String urlStatus = "";
        try {
            HttpResponse httpResponse = HttpRequest.post(urlStr).execute();
            log.info("******* httpResponse.getStatus() ****** :{}", httpResponse.getStatus());
            urlStatus = String.valueOf(httpResponse.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getApiUrlStatus Exception：", e);
            log.error("e.getMessage();:{}", e.getMessage());
            urlStatus = e.getMessage();
        }
        return urlStatus;
    }

    /**
     * 链接绿道系统检测用户信息是否存在
     *
     * @param username
     * @param password
     * @param source
     * @param webSite
     */
    public GpUserInfoDto checkGpSystemUserIsExist(HttpServletRequest request, String username, String password, String source, String webSite) {
        log.info("****** 开始链接绿道系统检测用户信息是否存在。username:{},password:{},source:{},webSite:{}, ******", username, password, source, webSite);
        GpUserInfoDto gpUserInfoDto = null;
        String gpUserUrl = DomainUrlUtil.GP_SYSTEM_USER_NAME_PASSWORD_CHECK_API_URL;
        if (!"200".equals(getApiUrlStatus(gpUserUrl))) {
            return gpUserInfoDto;
        }
        //构造参数
        ModelMap modelMap = new ModelMap();
        modelMap.put("username", username);
        modelMap.put("password", password);
        modelMap.put("source", source);
        if("PC".equals(source)){
            modelMap.put("webSite", webSite);
        }
        String result = HttpUtil.post(gpUserUrl, modelMap);
        log.info("绿道系统返回信息：" + result);
//        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("code");
        HttpApiResult httpApiResult = JSONObject.parseObject(result, HttpApiResult.class);
        if (httpApiResult.getCode() == 200) {
            gpUserInfoDto = JSONObject.parseObject(JSON.toJSONString(httpApiResult.getData()), GpUserInfoDto.class);
            gpUserInfoDto.setPassword(password);
            if("PC".equals(source)){
                gpUserInfoDto.setWebSite(webSite);
            }
            saveOrUpdatePcAndSellerUserInfo(request, source, gpUserInfoDto);
        }
        return gpUserInfoDto;
    }

    /**
     * 保存或更新会员信息
     *
     * @param gpUserInfoDto
     */
    public Member addOrUpdateMemberInfo(HttpServletRequest request, GpUserInfoDto gpUserInfoDto, GpSystemVO gpSystemVO) {
        log.info("***** 保存绿道系统用户信息-->addOrUpdateMemberInfo ==> gpUserInfoDto:{}*****", JSON.toJSONString(gpUserInfoDto));
        Member member = new Member();
        member.setMemberName(gpUserInfoDto.getUserName());
        member.setMemberNickName(gpUserInfoDto.getNickName());
        member.setMemberTrueName(gpUserInfoDto.getUserName());
        member.setMemberMobile(gpUserInfoDto.getUserPhone());
        member.setMemberEmail(gpUserInfoDto.getUserEmail());
        member.setGpUid(gpUserInfoDto.getUserId());
        member.setWebSite(gpUserInfoDto.getWebSite());

        //查询会员信息
        MemberExample memberExample = new MemberExample();
        memberExample.setGpUid(gpUserInfoDto.getUserId());
        List<Member> memberList = memberModel.getMemberList(memberExample, null);
        if (CollectionUtil.isEmpty(memberList)) {
            log.info("***** 保存绿道系统返回的用户信息 *****");
            if (StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                member.setLoginPwd(Md5.getMd5String(DomainUrlUtil.GP_SYSTEM_SSO_PASSWORD));
                member.setGpToken(Md5.getMd5String(UUID.randomUUID().toString()));
            } else {
                member.setLoginPwd(Md5.getMd5String(gpUserInfoDto.getPassword()));
            }
            member.setPayPwd("");
            member.setLastLoginIp(WebUtil.getRealIp(request));
            member.setLoginNumber(0);
            member.setRegisterTime(new Date());
            member.setLastLoginTime(new Date());
            member.setRegisterChannel(MemberConst.MEMBER_FROM_PC);
            member.setIsMobileVerify(MemberConst.IS_MOBILE_VERIFY_1);
            member.setIsEmailActive(MemberConst.IS_EMAIL_ACTIVE_1);
            memberModel.saveMember(member);
        } else {
            log.info("***** 更新绿道系统返回的用户信息 *****");
            Member memberInfo = memberList.get(0);
            if (gpSystemVO == null) {
                member.setLastLoginIp(WebUtil.getRealIp(request));
                member.setMemberId(memberInfo.getMemberId());
                member.setUpdateTime(new Date());
                if (!StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                    member.setLoginPwd(Md5.getMd5String(gpUserInfoDto.getPassword()));
                }
                memberModel.updateMember(member);
            } else {
                if (!StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                    member.setLoginPwd(Md5.getMd5String(gpUserInfoDto.getPassword()));
                }
                if (StringUtil.isEmpty(gpSystemVO.getToken())) {
                    member.setMemberId(memberInfo.getMemberId());
                    if (StringUtil.isEmpty(memberInfo.getGpToken())) {
                        member.setGpToken(Md5.getMd5String(UUID.randomUUID().toString()));
                    } else {
                        member.setGpToken(memberInfo.getGpToken());
                    }
                    memberModel.updateMember(member);
                } else {
                    if (memberInfo.getGpToken().equals(gpSystemVO.getToken())) {
                        member.setLastLoginIp(WebUtil.getRealIp(request));
                        member.setMemberId(memberInfo.getMemberId());
                        member.setUpdateTime(new Date());
                        memberModel.updateMember(member);
                    }
                }
            }
        }
        return member;
    }

    /**
     * 保存或更新店铺账号信息
     *
     * @param gpUserInfoDto
     */
    public Vendor addOrUpdateVendorInfo(HttpServletRequest request, GpUserInfoDto gpUserInfoDto, GpSystemVO gpSystemVO) {
        log.info("***** 保存或更新店铺账号信息-->addOrUpdateVendorInfo ===> gpUserInfoDto:{} *****", JSON.toJSONString(gpUserInfoDto));
        Vendor vendor = new Vendor();

        //查询店主信息
        VendorExample vendorExample = new VendorExample();
        vendorExample.setGpUid(gpUserInfoDto.getUserId());
        List<Vendor> vendors = vendorModel.getVendorList(vendorExample, null);
        if (CollectionUtil.isEmpty(vendors)) {
            log.info("***** 保存绿道系统返回的用户信息 *****");
            // 注册业务逻辑
            VendorRegisterDTO vendorRegisterDTO = new VendorRegisterDTO();
            vendorRegisterDTO.setVendorName(gpUserInfoDto.getUserName());
            if (StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                vendorRegisterDTO.setVendorPassword(DomainUrlUtil.GP_SYSTEM_SSO_PASSWORD);
                vendorRegisterDTO.setGpToken(Md5.getMd5String(UUID.randomUUID().toString()));
            } else {
                vendorRegisterDTO.setVendorPassword(gpUserInfoDto.getPassword());
            }
            vendorRegisterDTO.setVendorMobile(gpUserInfoDto.getUserPhone());
            vendorRegisterDTO.setVendorEmail(gpUserInfoDto.getUserEmail());
            vendorRegisterDTO.setGpUid(gpUserInfoDto.getUserId());

            //获取登陆ip
            String ip = WebUtil.getRealIp(request);
            vendorRegisterDTO.setIp(ip);
            try {
                vendorModel.registerVendor(vendorRegisterDTO);
                VendorExample vendorExample2 = new VendorExample();
                vendorExample2.setGpUid(gpUserInfoDto.getUserId());
                List<Vendor> saveVendorsInfo = vendorModel.getVendorList(vendorExample2, null);
                if (CollectionUtil.isNotEmpty(saveVendorsInfo)) {
                    vendor.setVendorId(saveVendorsInfo.get(0).getVendorId());
                    vendor.setGpToken(saveVendorsInfo.get(0).getGpToken());
                }
            } catch (Exception e) {
                e.printStackTrace();
                vendor.setStatusMsg(e.getMessage());
            }
        } else {
            log.info("***** 更新绿道系统返回的用户信息 *****");
            Vendor vendorInfo = vendors.get(0);
            if (gpSystemVO == null) {
                Vendor vendorUpdate = new Vendor();
                vendorUpdate.setVendorId(vendorInfo.getVendorId());
                vendorUpdate.setVendorName(gpUserInfoDto.getUserName());
                if (!StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                    vendorUpdate.setVendorPassword(Md5.getMd5String(gpUserInfoDto.getPassword()));
                }
                vendorUpdate.setVendorMobile(gpUserInfoDto.getUserPhone());
                vendorUpdate.setVendorEmail(gpUserInfoDto.getUserEmail());
                vendorUpdate.setLatestLoginTime(new Date());
                vendorUpdate.setLatestLoginIp(WebUtil.getRealIp(request));
                vendorUpdate.setGpUid(gpUserInfoDto.getUserId());
                vendorModel.updateVendor(vendorUpdate);
            } else {
                if (StringUtil.isEmpty(gpSystemVO.getToken())) {
                    vendor.setVendorId(vendorInfo.getVendorId());
                    if (StringUtil.isEmpty(vendorInfo.getGpToken())) {
                        vendor.setGpToken(Md5.getMd5String(UUID.randomUUID().toString()));
                    } else {
                        vendor.setGpToken(vendorInfo.getGpToken());
                    }
                    vendorModel.updateVendor(vendor);
                } else {
                    if (vendorInfo.getGpToken().equals(gpSystemVO.getToken())) {
                        Vendor vendorUpdate = new Vendor();
                        vendorUpdate.setVendorId(vendorInfo.getVendorId());
                        vendorUpdate.setVendorName(gpUserInfoDto.getUserName());
                        vendorUpdate.setVendorMobile(gpUserInfoDto.getUserPhone());
                        vendorUpdate.setVendorEmail(gpUserInfoDto.getUserEmail());
                        vendorUpdate.setLatestLoginTime(new Date());
                        vendorUpdate.setLatestLoginIp(WebUtil.getRealIp(request));
                        vendorUpdate.setGpUid(gpUserInfoDto.getUserId());
                        vendorModel.updateVendor(vendorUpdate);

                        vendor.setVendorId(vendorInfo.getVendorId());
                        vendor.setGpToken(vendorInfo.getGpToken());
                    }
                }
            }

        }
        return vendor;
    }

    /**
     * 根据用户名和UId检测绿道系统用户uid是否存在
     *
     * @param request
     * @param gpSystemVO
     * @return
     */
    public GpUserInfoDto checkGpSystemUserIsExistByUserNameAndUid(HttpServletRequest request, GpSystemVO gpSystemVO) {
        log.info("****** 开始链接绿道系统根据用户名和UId，检测用户信息是否存在。gpSystemVO:{} ******", JSON.toJSONString(gpSystemVO));
        GpUserInfoDto gpUserInfoDto = null;
        String gpUserUrl = DomainUrlUtil.GP_SYSTEM_USER_INFO_API_URL;
        if (!String.valueOf(ResponseConst.STATE_SUCCESS).equals(getApiUrlStatus(gpUserUrl))) {
            return gpUserInfoDto;
        }
        try {
            String gpParam = AESUtil.encrypt(JSON.toJSONString(gpSystemVO), DomainUrlUtil.GP_SYSTEM_CUSTOMER_SSO_ASE_KEY);
            log.info("gpParam:{}", gpParam);

            //构造参数
            ModelMap modelMap = new ModelMap();
            modelMap.put("param", gpParam);
            String result = HttpUtil.post(gpUserUrl, modelMap);
            log.info("绿道系统返回信息：" + result);
            HttpApiResult httpApiResult = JSONObject.parseObject(result, HttpApiResult.class);
            if (httpApiResult.getCode() == 200) {
                gpUserInfoDto = JSONObject.parseObject(JSON.toJSONString(httpApiResult.getData()), GpUserInfoDto.class);
                if ("PC".equals(gpSystemVO.getSource())) {
                    Member member = addOrUpdateMemberInfo(request, gpUserInfoDto, gpSystemVO);
                    if (member.getMemberId() != null) {
                        Member memberInfo = memberModel.getMemberByMemberId(member.getMemberId());
                        gpUserInfoDto.setB2bUid(memberInfo.getMemberId());
                        gpUserInfoDto.setPassword(memberInfo.getGpToken());
                    }
                }

                if ("SELLER".equals(gpSystemVO.getSource())) {
                    Vendor vendor = addOrUpdateVendorInfo(request, gpUserInfoDto, gpSystemVO);
                    if (!StringUtil.isEmpty(vendor.getStatusMsg())) {
                        gpUserInfoDto.setStatusMsg(vendor.getStatusMsg());
                    } else {
                        if (vendor.getVendorId() != null) {
                            Vendor vendorInfo = vendorModel.getVendorByVendorId(vendor.getVendorId());
                            gpUserInfoDto.setVendorId(vendorInfo.getVendorId());
                            gpUserInfoDto.setPassword(vendorInfo.getGpToken());
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            gpUserInfoDto = null;
        }
        return gpUserInfoDto;
    }

    /**
     * 保存或更新用户信息
     *
     * @param request
     * @param source
     * @param gpUserInfoDto
     */
    public void saveOrUpdatePcAndSellerUserInfo(HttpServletRequest request, String source, GpUserInfoDto gpUserInfoDto) {
        if ("PC".equals(source)) {
            addOrUpdateMemberInfo(request, gpUserInfoDto, null);
        }

        if ("SELLER".equals(source)) {
            addOrUpdateVendorInfo(request, gpUserInfoDto, null);
        }
    }

    /**
     * 获取PC商城token
     *
     * @param gpSystemVO
     * @return
     */
    public OauthTokenDto getOauthPcToken(HttpServletRequest request, GpSystemVO gpSystemVO) {
        String key = "";
        OauthTokenDto oauthTokenDto = new OauthTokenDto();
        GpUserInfoDto gpUserInfoDto = checkGpSystemUserIsExistByUserNameAndUid(request, gpSystemVO);
        if (gpUserInfoDto != null) {
            if (gpUserInfoDto.getB2bUid() != null && !StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                // 登录验证
                MemberExample memberExample = new MemberExample();
                memberExample.setMemberId(gpUserInfoDto.getB2bUid());
                memberExample.setGpToken(gpUserInfoDto.getPassword());
                List<Member> memberList = memberModel.getMemberList(memberExample, null);
                log.info("getOauthToken == >memberList:{}", JSON.toJSONString(memberList));
                if (CollectionUtil.isNotEmpty(memberList)) {
                    Member member = memberList.get(0);
                    AssertUtil.isTrue(!member.getState().equals(MemberConst.STATE_1), "会员账户被禁用，请联系平台客服！");
                    //定义一个uuid，用做存储redis的key，并且设置到token中
                    key = Md5.getMd5String(UUID.randomUUID().toString());
                    log.info("key:{}", key);
                    oauthTokenDto.setToken(member.getGpToken());
                    oauthTokenDto.setState(1);
                    SsoMemberOauthVO ssoMemberOauthVO = new SsoMemberOauthVO();
                    ssoMemberOauthVO.setMemberId(member.getMemberId() + "");
                    ssoMemberOauthVO.setToken(oauthTokenDto.getToken());
                    String oauthKey = RedisConst.SLD_PC_OAUTH_USER_KEY + key;
                    long expireTime = RedisConst.OAUTH_SSO_LINK_EXPIRE_TIME;
                    stringRedisTemplate.opsForValue().set(oauthKey, JSON.toJSONString(ssoMemberOauthVO), expireTime, TimeUnit.SECONDS);
                } else {
                    oauthTokenDto.setState(NumConstant.MINUSTHREE);
                }
            } else {
                oauthTokenDto.setState(NumConstant.MINUSTWO);
            }
        } else {
            oauthTokenDto.setState(NumConstant.MINUSONE);
        }
        oauthTokenDto.setKey(key);
        return oauthTokenDto;
    }

    /**
     * 根据oauthId获取缓存内容
     *
     * @param oauthId
     * @return
     */
    public String getOauthPcTokenInfoByOauthId(String oauthId) {
        String oauthPcKey = RedisConst.SLD_PC_OAUTH_USER_KEY + oauthId;
        log.info("oauthPcKey:{}", oauthPcKey);
        return stringRedisTemplate.opsForValue().get(oauthPcKey);
    }

    /**
     * 创建pc token
     *
     * @param member
     * @return
     */
    public JsonResult createPcToken(Member member) {
        log.info("******* createToken ***** ===> member.getMemberName():{},member.getLoginPwd():{}", member.getMemberName(), member.getLoginPwd());
        //登陆
        //设置请求头
        Map<String, String> headers = new HashMap<>(16);
        headers.put("Authorization", "Basic ZnJvbnQ6ZnJvbnQ=");
        headers.put("name", null);

        //设置请求参数
        UrlQuery urlQuery = new UrlQuery();
        urlQuery.add("loginType", 1);
        urlQuery.add("username", member.getMemberName());
        urlQuery.add("password", member.getLoginPwd());
        urlQuery.add("ssoType", 1);

        URI uri;
        try {
            uri = new URI(DomainUrlUtil.SLD_PC_URL + "/api/v3/frontLogin/oauth/token");
        } catch (URISyntaxException e) {
            log.error("登录地址有误", e);
            throw new MallException("登录失败");
        }
        UrlBuilder urlBuilder = UrlBuilder.of(uri, Charset.defaultCharset()).setQuery(urlQuery);

        HttpResponse execute = new HttpRequest(urlBuilder).method(Method.POST).addHeaders(headers).execute();
        return JSONObject.parseObject(execute.body(), JsonResult.class);
    }

    /**
     * 创建seller token
     *
     * @param gpSystemVO
     * @return
     */
    public OauthTokenDto getOauthSellerToken(HttpServletRequest request, GpSystemVO gpSystemVO) {
        String key = "";
        OauthTokenDto oauthTokenDto = new OauthTokenDto();
        GpUserInfoDto gpUserInfoDto = checkGpSystemUserIsExistByUserNameAndUid(request, gpSystemVO);
        if (gpUserInfoDto != null) {
            if (!StringUtil.isEmpty(gpUserInfoDto.getStatusMsg())) {
                oauthTokenDto.setState(NumConstant.MINUSFOUR);
                oauthTokenDto.setStatusMsg(gpUserInfoDto.getStatusMsg());
            } else {
                if (gpUserInfoDto.getVendorId() != null && !StringUtil.isEmpty(gpUserInfoDto.getPassword())) {
                    // 登录验证
                    VendorExample vendorExample = new VendorExample();
                    vendorExample.setVendorId(gpUserInfoDto.getVendorId());
                    vendorExample.setGpToken(gpUserInfoDto.getPassword());
                    List<Vendor> vendors = vendorModel.getVendorList(vendorExample, null);
                    log.info("getOauthSellerToken ==> vendors:{}", JSON.toJSONString(vendors));
                    if (CollectionUtil.isNotEmpty(vendors)) {
                        Vendor vendor = vendors.get(0);
                        AssertUtil.isTrue(vendor.getIsAllowLogin() != VendorConst.IS_ALLOW_LOGIN, "账号不可用");
                        //定义一个uuid，用做存储redis的key，并且设置到token中
                        key = Md5.getMd5String(UUID.randomUUID().toString());
                        log.info("key:{}", key);
                        oauthTokenDto.setToken(vendor.getGpToken());
                        oauthTokenDto.setState(1);
                        SsoMemberOauthVO ssoMemberOauthVO = new SsoMemberOauthVO();
                        ssoMemberOauthVO.setVendorId(vendor.getVendorId() + "");
                        ssoMemberOauthVO.setToken(oauthTokenDto.getToken());
                        String oauthSellerKey = RedisConst.SLD_SELLER_OAUTH_USER_KEY + key;
                        long expireTime = RedisConst.OAUTH_SSO_LINK_EXPIRE_TIME;
                        stringRedisTemplate.opsForValue().set(oauthSellerKey, JSON.toJSONString(ssoMemberOauthVO), expireTime, TimeUnit.SECONDS);
                    } else {
                        oauthTokenDto.setState(NumConstant.MINUSTHREE);
                    }
                } else {
                    oauthTokenDto.setState(NumConstant.MINUSTWO);
                }
            }
        } else {
            oauthTokenDto.setState(NumConstant.MINUSONE);
        }
        oauthTokenDto.setKey(key);
        return oauthTokenDto;
    }

    /**
     * 根据oauthId 获取缓存信息
     *
     * @param oauthId
     * @return
     */
    public String getOauthSellerTokenInfoByOauthId(String oauthId) {
        String oauthSellerKey = RedisConst.SLD_SELLER_OAUTH_USER_KEY + oauthId;
        log.info("oauthSellerKey:{}", oauthSellerKey);
        return stringRedisTemplate.opsForValue().get(oauthSellerKey);
    }

    /**
     * 创建Seller token
     *
     * @param vendor
     * @return
     */
    public JsonResult createSellerToken(Vendor vendor) {
        log.info("******* createSellerToken ***** ===> vendor.getVendorName():{},vendor.getVendorPassword():{}", vendor.getVendorName(), vendor.getVendorPassword());
        //登陆
        //设置请求头
        Map<String, String> headers = new HashMap<>(16);
        headers.put("Authorization", "Basic c2VsbGVyOnNlbGxlcg==");
        headers.put("name", null);

        UrlQuery urlQuery = new UrlQuery();
        urlQuery.add("username", vendor.getVendorName());
        urlQuery.add("password", vendor.getVendorPassword());
        urlQuery.add("ssoType", 1);

        URI uri;
        try {
            uri = new URI(DomainUrlUtil.SLD_SELLER_URL + "/api/v3/sellerLogin/oauth/token");
        } catch (URISyntaxException e) {
            log.error("登录地址有误", e);
            throw new MallException("登录失败");
        }

        UrlBuilder urlBuilder = UrlBuilder.of(uri, Charset.defaultCharset()).setQuery(urlQuery);
        HttpResponse execute = new HttpRequest(urlBuilder).method(Method.POST).addHeaders(headers).execute();
        return JSONObject.parseObject(execute.body(), JsonResult.class);
    }

}
