import http from './http.js'
import utils from '../utils/utils.js'

let limit = {}
let debounce = {}


const models = [
	'score', 'boss', 'login', 'vip', 'company', 'work','index-lzx', 'resume',
	'config',
	'examination',
	'collect',
	'contact',
	'workEntryInfo',
]

const config = {
	// 示例
	// wxLogin : {url : '/api/wxLogin', method : 'POST', 
	// auth : false, showLoading : true, loadingTitle : '加载中...',
	// limit : 1000
	// },
	
	// getConfig : {url : '/api/getConfig', method : 'GET', limit : 500},
	
	
	/**
	 * 公共的接口
	 */
	
	//关于本程序
	commonAboutUs: {
		url: '/api/common/aboutUs',
		method: 'GET',
	},
	// 我的服务-兑换码
	commonAddExchange: {
		url: '/api/common/addExchange',
		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',
	},
	//获取工作性质列表
	commonQueryJobNatureList: {
		url: '/api/common/queryJobNatureList',
		method: 'GET',
	},
	//获取banner图列表
	commonQueryBannerList: {
		url: '/api/common/queryBannerList',
		method: 'GET',
	},
	//获取开放地址列表
	commonQueryAddressList: {
		url: '/api/common/queryAddressList',
		method: 'GET',
	},
	//会员中心-正式积分||临时积分
	commonQueryScore: {
		url: '/api/common/queryScore',
		method: 'GET',
	},
	//积分记录
	commonQueryScoreRecord: {
		url: '/api/common/queryScoreRecord',
		method: 'GET',
	},
	//我的服务-获取VIP配置信息
	commonQueryVipType: {
		url: '/api/common/queryVipType',
		method: 'GET',
	},
	
	/**
	 * 求职者的接口
	 */
	
	
	
	//我的找活
	employeeQueryResumeByUserId: {
		url: '/api/employee/queryResumeByUserId',
		method: 'GET',
		auth: true, 
	},
	//联系记录-我看过谁
	employeeQueryWatchWho: {
		url: '/api/employee/queryWatchWho',
		method: 'GET',
		auth: true, 
	},
	//电子合同-获取电子合同列表
	employeeQueryContractList: {
		url: '/api/employee/queryContractList',
		method: 'GET',
		auth: true, 
	},
	
	/**
	 * boss的接口
	 */
	//电子合同-获取电子合同列表
	bossQueryContractList: {
		url: '/api/boss/queryContractList',
		method: 'GET',
		auth: 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')) {
			utils.toLogin()
			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)
}


function addApiModel(model, key){
	for(let k in model){
		if(config[`${k}`]){
			console.error(`重名api------model=${key},key=${k}`);
			uni.showModal({
				title: `重名api`,
				content: `model=${key},key=${k}`
			})
			continue
		}
		config[`${k}`] = model[k]
		// config[`${key}_${k}`] = model[k]
	}
}

models.forEach(key => {
	addApiModel(require(`./model/${key}.js`).default, key)
})


export default api