package com.jkcredit.invoice.util;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @description: Pattern
 * @author: mashengyi
 * @create: 2019-05-30 17:24
 * @version: V1.0
 **/
public class CorsFilterCheckUtil {


    public static String getApi(String requestApi) {
        String api = "";
        Pattern p = compile("api=(\\w+)&");
        Matcher m = p.matcher(requestApi);
        while (m.find()) {
            api = m.group(1);
        }
        if (StringUtils.isEmpty(api)) {
            api = JSONObject.parseObject(requestApi.substring(0, requestApi.length() - 1)).getString("api");
        }
        return api;
    }


    public static String getAppKey(String requestAppkey) {
        String appkey = "";
        Pattern p = compile("appKey=(\\w+)&");
        Matcher m = p.matcher(requestAppkey);
        while (m.find()) {
            appkey = m.group(1);
        }
        if (StringUtils.isEmpty(appkey)) {
            appkey = JSONObject.parseObject(requestAppkey.substring(0, requestAppkey.length() - 1)).getString("appKey");
        }
        return appkey;
    }


    public static String getAppSecret(String requestAppSecret) {
        String appSecret = "";
        Pattern p = compile("appSecret=(\\w+)&");
        Matcher m = p.matcher(requestAppSecret);
        while (m.find()) {
            appSecret = m.group(1);
        }
        if (StringUtils.isEmpty(appSecret)) {
            appSecret = JSONObject.parseObject(requestAppSecret.substring(0, requestAppSecret.length() - 1)).getString("appSecret");
        }
        return appSecret;
    }


    public static String getData(String requestData) {
        String data = "";
        Pattern p = compile("data=([^&]*)&");
        Matcher m = p.matcher(requestData);
        while (m.find()) {
            data = m.group(1);

        }
        if (StringUtils.isEmpty(data)) {
            data = JSONObject.parseObject(requestData.substring(0, requestData.length() - 1)).getString("data");
        }
        return data;
    }

}
