import $http from '@/utils/httpRequest'

class API {
	constructor(constructorData) {
		const map={
			add:'post',
			del:'post',
			update:'post',
			get:'get',
			getList:'get'
		} 
		for(let key in map){
			let url=constructorData[key].url||constructorData[key]
			const method=constructorData[key].method||map[key]
			this[key]=function(data){
				console.log(data)
				return $http({
					url: (key=='get'?(url+'/'+data.id||data):url) +(key=='del'?'?id='+(data.id||data):''),
					method:method,
					params:(method=='get'&&key!='del')?data:{},
					data: (method=='post'&&key!='del')?data:{}
				})
			}
		}
	}
}

export class API1 {
    constructor(url) {
        const map = {
            add: 'POST',
            del: 'DELETE',
            update: 'PUT',
            get: 'GET'
        }
        this.url = url
        for (let key in map) {
            this[key] = function (data) {
                data = Array.isArray(data) ? data : {
                    ...data
                }
                if (key === 'del') {
                    
                }
                const par={ url:key==='get'?url+'/list':url, method: map[key] }
                if(key==='get'){
                    par.params=data
                }else{
                    par.data=data
                }
                return  $http(par)
            }
        }
    }
}
export default API