package cn.pluss.platform.baidu.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.pluss.platform.baidu.BaiduOCRService;
import cn.pluss.platform.util.HttpUtil;
import cn.pluss.platform.util.ParametersUtil;

/**
 * @author yuchen
 * @Description
 */
@Transactional
@Service("baiduOCRService")
public class BaiduOCRServiceImpl implements BaiduOCRService {
    //https://ai.baidu.com/docs#/OCR-API-Idcard/top 此文档地址

    @Override
    public String getInfo(String url, String accessToken, String params) {
        try {
            String baiduPOST = HttpUtil.baiduPOST(url, accessToken, params);
            return baiduPOST;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
    }

    @Override
    public String getAuth() {
        String baiduApiKey = ParametersUtil.baiduAK;
        String baiduSecretKey = ParametersUtil.baiduSK;
        String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
        //固定值               //2. 官网获取的 API Key   // 3. 官网获取的 Secret Key
        String getAccessTokenUrl = authHost + "grant_type=client_credentials" + "&client_id=" + baiduApiKey + "&client_secret=" + baiduSecretKey;
        return HttpUtil.baiduGET(getAccessTokenUrl);
    }

}

