
import { isArray } from '@/utils/is';
import { useMessage } from '@/hooks/useMessage';

const { createMessage } = useMessage()
export interface Result<T = any> {
    code: number;
    msg: string;
    data: T;
}
/**
 * 返回数据处理函数
 * @param isEmptyList 是否输出空list
 * @returns 
 */
function useReturnData(isEmptyList = false) {
    function resData(res, message?: string) {
        if (res.code === 200 && res.msg === "SUCCESS") {
            // 判断是否为数组
            return isArray(res.data) ? res.data : (message ? createMessage.success(message) : createMessage.success(res.data))
        } else {
            return isEmptyList ? [] : 500
        }
    }

    return {
        resData
    }
}

/**
 * code = 200
 */
function resCodeSuccess(res: Result) {
    return res && res.code == 200 && res.data
}

export {
    useReturnData,
    resCodeSuccess
}