import {
	apiurl
} from "./apiurl.js"

import queryParams from "../utils/queryParams.js"

// 此处第二个参数vm,就是我们在页面使用的this,你可以通过vm获取vuex等操作,更多内容详见uView对拦截器的介绍部分:
const install = (Vue, vm) => {

	let httpMap = {}
	const http = uni.$u.http
	
	// 循环请求路径对象生成对应的方式请求
	Object.keys(apiurl).forEach((key) => {
		if(apiurl[key]?.type=='get'){
			if(apiurl[key].addUrl){
				httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url+data.code, {}, config);
			}else {
				httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, {params:data}, config);
			}
		} else if (apiurl[key]?.type == 'delete') {
			httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url + `${queryParams(data)}`, {}, config);
		} else{
			httpMap[key] = (params = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, params, config);
		}
	    
	});
	
	// 将各个定义的接口名称,统一放进对象挂载到vm.$u.api(因为vm就是this,也即this.$u.api)下
	vm.$u.api = {
		...httpMap
	};
}

export default {
	install
}