import { httpApi } from 'hc-vue3-ui'
import router from '~src/router/index'

//封装的请求
export const HcApi = async (obj, msg = true) => {
    return new Promise((resolve) => {
        //发起请求
        httpApi(obj).then((response) => {
            //console.log(response)
            resolve(response)
        }).catch((response) => {
            getResData(response, msg)
            resolve(response)
        })
    })
}

//处理数据
const getResData = ({ code, msg }, isMsg = false) => {
    if (code === 401) {
        window.$message?.error('身份失效,请重新登录！')
        router.push({ path: '/login' }).then()
    } else if (code !== 404 && code !== 500 && code !== 504) {
        if (isMsg) window.$message?.error(msg)
    }
}
