package com.bohuikeji.delivery.backstage.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.util.sanyan.*;
import jodd.http.HttpRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * 闪验工具类
 * @author 孙劲辉
 */
public class SanYanUtils {

    /**
     * 骑手安卓版id
     */
    private static final String APPID_COURIER_ANDROID="ZBjPuLjp";
    /**
     * 骑手安卓版key
     */
    private static final String APPKEY_COURIER_ANDROID="EQnNso34";

    public static final String privateKey = "";

    /**
     * 查询真实手机号URL
     */
    public static final String GET_PHONE_URL="https://api.253.com/open/flashsdk/mobile-query";


    /**
     * 获取手机号码
     * @param token
     * @return
     * @throws Exception
     */
    public static String getPhone(String token) throws Exception {
        String encryptType = "0";
        Map<String, String> params = new HashMap<String, String>();
        params.put("token", token);
        params.put("appId", APPID_COURIER_ANDROID);
        params.put("sign", SignUtils.getSign(params, APPKEY_COURIER_ANDROID));
        JSONObject jsonObject = OkHttpUtil.postRequest(GET_PHONE_URL, params);
        if (null != jsonObject) {
            System.out.println("response:" + jsonObject.toJSONString());
            //返回码 200000为成功
            String code = jsonObject.getString("code");
            //返回消息
            String message = jsonObject.getString("message");
            //是否收费
            String chargeStatus = jsonObject.getString("chargeStatus");
            if ("200000".equals(code)) {
                String dataStr = jsonObject.getString("data");
                JSONObject dataObj = JSONObject.parseObject(dataStr);
                String mobile = dataObj.getString("mobileName");
                if ("0".equals(encryptType)) {
                    String key = MD5.getMD5Code(APPKEY_COURIER_ANDROID);
                    mobile = AESUtils.decrypt(mobile, key.substring(0, 16), key.substring(16));
                } else if ("1".equals(encryptType)) {
                    mobile = RSAUtils.decryptByPrivateKeyForLongStr(mobile, privateKey);
                }
                return mobile;
            }
        }
        return null;
    }
}
