package com.mjlogin.service;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.HttpResponse;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.Header;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class PhoneCheck {
	public static String getCheck(String loginToken) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String responseBody = "";
		try {
			HttpPost httpPost = new HttpPost("https://api.verification.jpush.cn/v2/web/loginTokenVerify");

			// 设置请求头
			httpPost.setHeader("Content-Type", "application/json");

			// 设置Basic认证 (7d431e42dfa6a6d693ac2d04:5e987ac6d2e04d95a9d8f0d1)
			String appKey= "b887e7338c27611c29bcdab4";
			String appSecret= "026877ccc36d03b0a673dc26";

			String auth = appKey+":"+appSecret;
			byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
			String authHeader = "Basic " + new String(encodedAuth);
			httpPost.setHeader("Authorization", authHeader);

			// 设置请求体
	        String jsonBody = "{\"loginToken\":\"" + loginToken + "\"}";

			StringEntity entity = new StringEntity(jsonBody, StandardCharsets.UTF_8);
			httpPost.setEntity(entity);

			// 执行请求
			HttpResponse response = httpClient.execute(httpPost);

			// 获取响应
			responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
			int statusCode = response.getStatusLine().getStatusCode();

			System.out.println("Status Code: " + statusCode);
			System.out.println("Response: " + responseBody);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return responseBody;
	}

	public static void main(String[] args) {
		PhoneCheck.getCheck(
				"RuyXf6ioDCqBm1ZRlH307AktN1On5MO6M5cg6S9iuioioQ3X/rG6xcIWUKhgmTwu9Yh6zREiZAbNj3tGdBcHYLIRRWBFVLe90/RiPqO1QTQwYe5Yw0juorBokpDGo9huIPN8FWPfmb77vG9EqiCbV2v9shizPSkTnSCaum3akOO2wHkflf6piqheaKHYKmkdxSJLNodogQ6OtZB4RCyKvbjYAwK/+yHQDIwJiodQme7XkQ2xqsnRfLbu7fzwCwFwH91PCwbHcLodn2WbgTJp4QBOA/pQCSUA9eA2Mx3cZllHcWPo85Ww9HCssb/V1G0GOkObf03Ko6bbPs/9d7uH9BIvOSBII+r9Z5Y/fNQlzHa1DCGC+k/RQa85XstqBii7q+dtyoL2puv6beTlU1ZQnqTWglqbk1x5JAQbt3HdNiDRXX31kn3O2klOZHprlxHbtobvOU2/LOpCc2a+a7UrQEKSpgyZ5vhTOiEsJOAxYE2/QGIwqr+BSxsjTwX1bPEOKxgdak4G2Z9hiXxLBtZ96Q==");
	}
}