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


let limit = {}
let debounce = {}

const models = ['login', 'user', 'config']

const config = {

	//轮播图-轮播图列表
	queryBannerList: {
		url: '/workorder/banner/queryBannerList',
		method: 'GET',
		auth : true,
	},
	
	
	//收藏信息-添加收藏
	addCollection: {
		url: '/workorder/collection/addCollection',
		method: 'GET',
		auth : true,
	},
	//收藏信息-取消收藏
	deleteCollection: {
		url: '/workorder/collection/deleteCollection',
		method: 'GET',
		auth : true,
	},
	//收藏信息-我的收藏
	queryCollectionList: {
		url: '/workorder/collection/queryCollectionList',
		method: 'GET',
		auth : true,
	},

	//工单信息-查询工单列表
	queryTemplateList: {
		url: '/workorder/template/queryTemplateList',
		method: 'GET',
		auth : true,
	},
	//公告信息-查询公告列表
	queryNewsList: {
		url: '/workorder/news/queryNewsList',
		method: 'GET',
		auth : true,
	},
	// 公告信息-根据id查询公告信息
	queryNewsById: {
		url: '/workorder/news/queryNewsById',
		method: 'GET',
		auth : true,
	},
	//工单信息-工序卡1(选配)详情
	queryStepOne: {
		url: '/workorder/template/queryStepOne',
		method: 'GET',
		auth : true,
	},
	//工单信息-工序卡2详情
	queryStepTwo: {
		url: '/workorder/template/queryStepTwo',
		method: 'GET',
		auth : true,
	},
	//工单信息-工序卡3详情
	queryStepThree: {
		url: '/workorder/template/queryStepThree',
		method: 'GET',
		auth : true,
	},
	//工单信息-工序卡4(总成)详情
	queryStepFour: {
		url: '/workorder/template/queryStepFour',
		method: 'GET',
		auth : true,
	},
	//工单信息-根据id查询工单详情
	queryTemplateById: {
		url: '/workorder/template/queryTemplateById',
		method: 'GET',
		auth : true,
		showLoading : true,
	},
	//工单信息-修改工单
	updateTemplate: {
		url: '/workorder/template/updateTemplate',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	//工单信息-修改常规参数-工序卡1(选配)
	updateGeneralStepOne: {
		url: '/workorder/template/updateGeneralStepOne',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改工序参数-工序卡1(选配)
	updateParamStepOne: {
		url: '/workorder/template/updateParamStepOne',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改常规参数-工序卡2
	updateGeneralStepTwo: {
		url: '/workorder/template/updateGeneralStepTwo',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改工序参数-工序卡2
	updateParamStepTwo: {
		url: '/workorder/template/updateParamStepTwo',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改常规参数-工序卡3
	updateGeneralStepThree: {
		url: '/workorder/template/updateGeneralStepThree',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改工序参数-工序卡3
	updateParamStepThree: {
		url: '/workorder/template/updateParamStepThree',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改常规参数-工序卡4(总成)
	updateGeneralStepFour: {
		url: '/workorder/template/updateGeneralStepFour',
		method: 'POST',
		auth : true,
	},
	//工单信息-修改工序参数-工序卡4(总成)
	updateParamStepFour: {
		url: '/workorder/template/updateParamStepFour',
		method: 'POST',
		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}`);
			continue
		}
		config[`${k}`] = model[k]
		// config[`${key}_${k}`] = model[k]
	}
}

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

export default api