import http from './http.js'

const config = {
	// 示例
	// wxLogin : {url : '/api/wxLogin', method : 'POST', 
	// auth : false, showLoading : true, loadingTitle : '加载中...',
	// limit : 1000
	// },
	
	getConfig : {url : '/api/getConfig', method : 'GET'},
	// 登录接口
	loginLogin: { url: '/pay-api/login/login', method: 'GET', limit : 500},
	//获取商品信息
	getPayShopOne : {url : '/pay-api/info/getPayShopOne', method : 'GET'},
	//创建支付订单
	createPayOrder : {url : '/pay-api/info/createPayOrder', method : 'POST', limit : 2000},
	//获取个人信息
	getInfo : {url : '/pay-api/info/getInfo', method : 'GET'},
	//获取订单列表
	getPayOrderPage : {url : '/pay-api/info/getPayOrderPage', method : 'GET'},
	//修改个人信息
	updateInfo : {url : '/pay-api/info/updateInfo', method : 'GET', limit : 500},
}



export function api(key, data, callback, loadingTitle){
	let req = config[key]
	
	if (!req) {
		console.error('无效key' + key);
		return
	}
	
	if(typeof callback == 'string'){
		loadingTitle = callback
	}
	
	if(typeof data == 'function'){
		callback = data
		data = {}
	}
	
	// 接口限流
	if(req.limit){
		let storageKey = 'limit:' + req.url
		let storage = uni.getStorageSync(storageKey)
		if(storage && new Date().getTime() - parseInt(storage) < req.limit){
			return
		}
		uni.setStorageSync(storageKey, new Date().getTime())
	}
	
	//必须登录
	if (req.auth) {
		if (!uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
			console.error('需要登录')
			return
		}
	}
	
	http.http(req.url, data, callback, req.method, 
	loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
}



export default api