import http from './http.js'


let limit = {}
let debounce = {}

const config = {
	// 示例
	// wxLogin : {url : '/api/wxLogin', method : 'POST', 
	// auth : false, showLoading : true, loadingTitle : '加载中...',
	// limit : 1000
	// },
	
	getConfig : {url : '/api/getConfig', method : 'GET', limit : 500},
	
	
	// 微信登录接口
	wxLogin: {
		url: '/login/login',
		method: 'POST', 
		limit : 500,
		showLoading : true,
	},
	// 修改个人信息接口
	updateInfo: {
		url: '/user/edit',
		method: 'POST',
		auth: true, 
		limit : 800,
		showLoading : true,
	},
	// 获取用户信息
	getInfo: {
		url: '/user/info',
		method: 'GET',
		auth: true, 
		limit : 200,
	},
	//隐私政策
	getPrivacyPolicy: {
		url: '/login/getPrivacyPolicy',
		method: 'GET',
	},
	//用户协议
	getUserAgreement: {
		url: '/login/getUserAgreement',
		method: 'GET',
	},
	
	
	
	// 获取团队
	teamList: {
		url: '/team/list',
		method: 'GET',
	},
	// 申请加入团队
	teamApply: {
		url: '/team/apply',
		method: 'POST',
		auth: true, 
		limit : 800,
	},
	
	
	// 实名认证
	authApply: {
		url: '/auth/apply',
		method: 'POST',
		auth: true, 
		limit : 800,
		showLoading : true,
	},
	// 实名认证详情
	authInfo: {
		url: '/auth/info',
		method: 'GET',
	},
	
	
	// 打卡时间的接口
	clockTime: {
		url: '/clock/time',
		method: 'POST',
		showLoading : true,
		auth : true,
	},
	// 人脸识别
	clockVerifyFace: {
		url: '/clock/verify/face',
		method: 'POST',
		showLoading : true,
		auth : true,
		limit : 2000,
		loadingTitle : '核验中',
	},
	
	// 打卡
	clock: {
		url: '/clock/in',
		method: 'POST',
		showLoading : true,
		auth: true, 
		limit : 800,
	},
	// 打卡详情列表
	clockList: {
		url: '/clock/in/log',
		method: 'GET',
	},
	// 打卡统计
	clockTotal: {
		url: '/clock/in/total',
		method: 'GET',
		showLoading : true,
		debounce : 400,
	},
	
	
	
	// 项目列表分页
	clockProjectList: {
		url: '/clock/project/list',
		method: 'GET',
		auth : true,
		showLoading : true,
	},
	// 设置项目经纬度
	clockProjectLocation: {
		url: '/clock/project/location',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
}



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 = req.url
		let storage = limit[storageKey]
		if (storage && new Date().getTime() - storage < req.limit) {
			return
		}
		limit[storageKey] = new Date().getTime()
	}

	//必须登录
	if (req.auth) {
		if (!uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/login/login'
			})
			console.error('需要登录')
			return
		}
	}
	
	// 接口防抖
	if(req.debounce){
		
		let storageKey = req.url
		let storage = debounce[storageKey]
		
		if (storage) {
			clearTimeout(storage)
		}
		
		debounce[storageKey] = setTimeout(() => {
			
			clearTimeout(storage)
			
			delete debounce[storageKey]
			
			http.http(req.url, data, callback, req.method,
				loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
		}, req.debounce)
		
		return
	}

	http.http(req.url, data, callback, req.method,
		loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
}



export default api