package com.yida.data.common.core.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yida.data.common.core.constant.MagAppConstant;
import com.yida.data.common.core.exception.FebsException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * mag-app工具类
 * 多云教研室
 *
 * @author ZYJ
 */
@Slf4j
public class MagAppUtil {

    /**
     * 返回的status状态码json字段名称
     */
    public static final String STATUS_NAME = "code";

    /**
     * 返回的data数据json字段名称
     */
    private static final String DATA_NAME = "data";

    /**
     * 返回的msg数据json字段名称
     */
    private static final String MSG_NAME = "msg";

    /**
     * 获取用户信息
     *
     * @param magAppUrl    mag-app地址
     * @param magAppSecret mag-app密钥
     * @param appUserToken app用户token
     */
    public static JSONObject getUserInfo(String magAppUrl, String magAppSecret, String appUserToken) {
        String url = magAppUrl + MagAppConstant.GET_USER_INFO;
        JSONObject body = JSONUtil.createObj().set("token", appUserToken).set("secret", magAppSecret);
        String res = HttpUtil.get(url, body);
        JSONObject resJson = JSONUtil.parseObj(res);
        log.info("mag-app获取用户信息,res:[{}],params:[{}]", res, body);
        if (!resJson.getBool("success")) {
            throw new FebsException(resJson.getStr(MSG_NAME));
        }
        return JSONUtil.parseObj(resJson.getStr(DATA_NAME));
    }
}
