package org.jeecg.modules.app.esign.auth;

import org.jeecg.modules.app.dto.GetPsnAuthUrlDTO;
import org.jeecg.modules.app.esign.comm.EsignHttpHelper;
import org.jeecg.modules.app.esign.comm.EsignHttpResponse;
import org.jeecg.modules.app.esign.constant.EsignDemoConfig;
import org.jeecg.modules.app.esign.enums.EsignRequestType;
import org.jeecg.modules.app.esign.exception.EsignDemoException;

import java.util.Map;

import static com.alibaba.dashscope.utils.JsonUtils.gson;

/**
 * description 认证与授权服务
 * datetime 2022年8月10日上午09:43:24
 */
public class ESignAuth extends Exception {

	private static String eSignHost= EsignDemoConfig.EsignHost;
	private static String eSignAppId=EsignDemoConfig.EsignAppId;
	private static String eSignAppSecret=EsignDemoConfig.EsignAppSecret;
	/**
	 * 获取个人认证链接
	 * @return
	 * @throws EsignDemoException
	 */
	public static EsignHttpResponse getPsnAuthUrl(GetPsnAuthUrlDTO getPsnAuthUrlDTO) throws EsignDemoException {
		String apiaddr="/v3/psn-auth-url";
		//请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
		String jsonParm = gson.toJson(getPsnAuthUrlDTO);
		//请求方法
		EsignRequestType requestType= EsignRequestType.POST;
		//生成签名鉴权方式的的header
		Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
		//发起接口请求
		return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
	}

	/**
	 * 查询个人认证授权状态
	 */
	public static EsignHttpResponse getPsnIdentityInfo(String phoneNumber) throws EsignDemoException {
		String apiaddr="/v3/persons/identity-info?psnAccount="+phoneNumber;
		//请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
		String jsonParm=null;
		//请求方法
		EsignRequestType requestType= EsignRequestType.GET;
		//生成签名鉴权方式的的header
		Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
		//发起接口请求
		return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
	}
	/**
	 * 获取企业认证链接
	 * @return
	 * @throws EsignDemoException
	 */
	public static EsignHttpResponse getOrgAuthUrl(String orgName,String phoneNumber) throws EsignDemoException {
		String apiaddr="/v3/org-auth-url";
		//请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
		String jsonParm="{\n" +
				"    \"orgAuthConfig\": {\n" +
				"        \"orgName\": \""+orgName+"\",\n" +
				"        \"transactorInfo\": {\n" +
				"            \"psnAccount\": \""+phoneNumber+"\"\n" +
				"        }\n" +
				"    },\n" +
				"    \"clientType\": \"all\",\n" +
				"    \"notifyUrl\":\"https://www.xxx.cn/notify\",\n" +
				"    \"redirectConfig\": {\n" +
				"        \"redirectUrl\": \"https://www.esign.cn\"\n" +
				"    },\n" +
				"    \"authorizeConfig\":{\n" +
				"        \"authorizedScopes\":[\"get_org_identity_info\",\"get_psn_identity_info\",\"org_initiate_sign\",\"psn_initiate_sign\",\"manage_org_resource\",\"manage_psn_resource\",\"use_org_order\"]\n" +
				"    }\n" +
				"}";

		//请求方法
		EsignRequestType requestType= EsignRequestType.POST;
		//生成签名鉴权方式的的header
		Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
		//发起接口请求
		return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
	}

	/**
	 * 查询企业认证授权状态
	 */
	public static EsignHttpResponse getOrgIdentityInfo(String orgName) throws EsignDemoException {
		String apiaddr="/v3/organizations/identity-info?orgName="+orgName;

		//请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
		String jsonParm="{}";
		//请求方法
		EsignRequestType requestType= EsignRequestType.GET;
		//生成签名鉴权方式的的header
		Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
		//发起接口请求
		return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
	}
}
