import { ImgsUrl } from "./Api";

export enum DirectoryId
{
    None = "",
    FileDir = "1", //图片根目录
    MaterialDir = "2", //材质根目录
    ImgDir = "3", //图片根目录
    ToplineDir = "4", //材质根目录
    TemplateDir = "5", //模板根目录
    DrillingDir = "6", //排钻目录
    KnifePathDir = "7", //刀路目录
    Frame = "8", //图框目录
    CeilingContour = "9", //吊顶轮廓
    HistoryDit = "-1",//历史编辑目录
}

export enum RequestStatus
{
    NoLogin = 88888,
    Ok = 0,
    NoPermission = 102,//没有经过授权,不能登录该账号
    DeleteWarn1 = 401,
    DeleteWarn2 = 402,
    NoBuy = 3298, //未购买cad包月服务错误码
    NoBuy1 = 3299,//包月服务未充值
    NoBuy2 = 3300,//包月服务未生效
    NoBuy3 = 3301,//包月服务已失效
    NoBuy4 = 3412, //未购买渲染包月服务
    None = -1, //未知错误
    OffLine = 44444, //踢下线
    NoToken = 6600, //酷家乐未授权
    CreateTempNoLogo = 802, //导入模板时未查询到json文件logo
}
export interface IResponseData
{
    err_code: RequestStatus;
    err_msg: string;
    [key: string]: any;
}

export async function PostJson<T = object>(
    url: string, body: Exclude<T, BodyInit>,
    isShowErrMsg = true)
{
    while (true)
    {
        let res = await Post(url, JSON.stringify(body), isShowErrMsg);
        return res;
    }
}

export async function Post(url: string, body?: BodyInit, isShowErrMsg = true): Promise<IResponseData>
{
    try
    {
        let res = await fetch(url, {
            method: "POST",
            mode: "cors",
            credentials: "include",
            body,
        });
        let result = await res.json();
        return result;
    }
    catch (error)
    {
        // ReportError(`请求url错误:${url}`);
        return { err_code: RequestStatus.None, err_msg: `请求失败,地址:${url}` };
    }
}
