
import axios, { AxiosInstance } from "axios"

const url = {
    development: 'http://172.16.101.135:8001',
    test: '',
    production: ''
}

import { Toast } from "vant"
export class Service {
    public service: AxiosInstance;
    constructor() {
        this.service = axios.create({
            baseURL: process.env.NODE_ENV === 'production' ? url.production : url.development,
            timeout: 15000,
        })
        const get = this.service.get
        const post = this.service.post
        const resolveData = (ret: any) => {
            if (ret.code === 200)
                return ret.data;
            else {
                Toast.fail(ret.message)
                throw new Error(ret.message)
            }
        }

        this.service.get = (...res) => get(...res).then(ret => resolveData(ret.data));
        this.service.post = (...res) => post(...res).then(ret => resolveData(ret.data)).catch(err => {
            resolveData(err.response.data)
        });
    }
}