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: '/api/login/login',
		method: 'GET', 
		limit : 500,
		showLoading : true,
	},
	// 修改个人信息接口
	updateInfo: {
		url: '/info/updateInfo',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	//隐私政策
	getPrivacyPolicy: {
		url: '/login/getPrivacyPolicy',
		method: 'GET',
	},
	//用户协议
	getUserAgreement: {
		url: '/login/getUserAgreement',
		method: 'GET',
	},
	
	
	
	/**
	 * 公共的接口
	 */
	
	//关于本程序
	commonAboutUs: {
		url: '/api/common/aboutUs',
		method: 'GET',
	},
	//新建账本
	commonAddBill: {
		url: '/api/common/addBill',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	// 我的服务-兑换码
	commonAddExchange: {
		url: '/api/common/addExchange',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	// 技工问题
	commonAddQuestion: {
		url: '/api/common/addQuestion',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	// 我的服务-会员充值(开通VIP)
	commonAddRecharge: {
		url: '/api/common/addRecharge',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	// 我的服务-获取积分-充值积分
	commonAddScoreByRecharge: {
		url: '/api/common/addScoreByRecharge',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	//获取工种列表
	commonQueryJobTypeList: {
		url: '/api/common/queryJobTypeList',
		method: 'GET',
	},
	//获取banner图列表
	commonQueryBannerList: {
		url: '/api/common/queryBannerList',
		method: 'GET',
	},
	//获取开放地址列表
	commonQueryAddressList: {
		url: '/api/common/queryAddressList',
		method: 'GET',
	},
	//全年收支
	commonQueryBill: {
		url: '/api/common/queryBill',
		method: 'GET',
	},
	//项目列表
	commonQueryNotebookList: {
		url: '/api/common/queryNotebookList',
		method: 'GET',
	},
	//会员中心-正式积分||临时积分
	commonQueryScore: {
		url: '/api/common/queryScore',
		method: 'GET',
	},
	//积分记录
	commonQueryScoreRecord: {
		url: '/api/common/queryScoreRecord',
		method: 'GET',
	},
	
	/**
	 * 求职者的接口
	 */
	
	
	//获取工作信息列表
	employeeQueryJobList: {
		url: '/api/employee/queryJobList',
		method: 'GET',
	},
	//根据Id查看工作详情
	employeeQueryJobById: {
		url: '/api/employee/queryJobById',
		method: 'GET',
	},
	//我的收藏
	employeeQueryCollectionJobList: {
		url: '/api/employee/queryJobCollectionList',
		method: 'GET',
	},
	//联系记录-我看过谁(我的找活)
	employeeQueryWatchWho: {
		url: '/api/employee/queryWatchWho',
		method: 'GET',
	},
	//联系记录-谁看过我(谁看过我的简历)
	employeeQueryWatchMe: {
		url: '/api/employee/queryWatchMe',
		method: 'GET',
	},
	//电子合同-获取电子合同列表
	employeeQueryContractList: {
		url: '/api/employee/queryContractList',
		method: 'GET',
	},
	
	/**
	 * boss的接口
	 */
	//获取简历列表
	bossQueryJobList: {
		url: '/api/boss/queryResumeListByAll',
		method: 'GET',
	},
	//根据Id查看简历详情
	bossQueryResumeById: {
		url: '/api/boss/queryResumeById',
		method: 'GET',
	},
	//电子合同-获取电子合同列表
	bossQueryContractList: {
		url: '/api/boss/queryContractList',
		method: 'GET',
	},
	//我的收藏
	bossQueryCollectionJobList: {
		url: '/api/boss/queryJobCollectionList',
		method: 'GET',
	},
	//会员中心-联系记录-我看过谁(我的招工)
	bossQueryWatchWho: {
		url: '/api/boss/queryWatchWho',
		method: 'GET',
	},
	//会员中心-联系记录-谁看过我(谁看过我的招工)
	bossQueryWatchMe: {
		url: '/api/boss/queryWatchMe',
		method: 'GET',
	},
}

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_order/auth/wxLogin'
			})
			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