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: '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',
	},

	/**
	 * 首页相关接口
	 */

	// 添加建议
	addAdvice: {
		url: '/info/addAdvice',
		method: 'POST',
		limit: 500,
		showLoading: true,
	},
	// 添加志愿者
	addVolunteer: {
		url: '/info/addVolunteer',
		method: 'POST',
		limit: 500,
		showLoading: true,
	},
	// 获取景区列表
	queryAreaList: {
		url: '/info/queryAreaList',
		method: 'GET',
		showLoading: true,
	},
	// 根据id获取景区详情
	queryAreaListById: {
		url: '/info/queryAreaListById',
		method: 'GET',
		showLoading: true,
	},
	// 根据id获取文章详情
	queryArticleById: {
		url: '/info/queryArticleById',
		method: 'GET',
		showLoading: true,
	},
	// 获取文章列表
	queryArticleList: {
		url: '/info/queryArticleList',
		method: 'GET',
		showLoading: true,
	},
	// 根据分类获取文章列表
	queryArticleListByType: {
		url: '/info/queryArticleListByType',
		method: 'GET',
		showLoading: true,
	},
	// 获取banner图列表
	queryBannerList: {
		url: '/info/queryBannerList',
		method: 'GET',
		showLoading: true,
	},
	// 获取景点列表
	// areaId: 0-瓷都镇区 1-湖田片区 2-高岭片区 3-瑶里片区 4-蛟潭片区; categoryTyep: 0-景点 1-美食店铺 2-民宿 3-厕所
	querySpotList: {
		url: '/info/querySpotList',
		method: 'GET',
		showLoading: true,
	},
	// 获取视频列表
	queryVedioById: {
		url: '/info/queryVedioById',
		method: 'GET',
		showLoading: true,
	},
	
	
	
	
	// amusement 游玩项目相关接口
	
	
	// 获取非遗体验列表
	queryExperienceList: {
		url: '/amusement/queryExperienceList',
		method: 'GET',
		showLoading: true,
	},
	// 获取非遗体验详情
	queryExperienceById: {
		url: '/amusement/queryExperienceById',
		method: 'GET',
		showLoading: true,
	},
	// 获取路径定制、我要研学列表
	queryAmusementList: {
		url: '/amusement/queryAmusementList',
		method: 'GET',
		showLoading: true,
	},
	// 获取路径定制、我要研学详情
	queryAmusementById: {
		url: '/amusement/queryAmusementById',
		method: 'GET',
		showLoading: true,
	},
	
	
	
	
	// 修改地址
	updateAddress: {
		url: '/user/updateAddress',
		method: 'POST',
		showLoading: true,
	},
	// 查询地址
	queryAddress: {
		url: '/user/queryAddress',
		method: 'GET',
		showLoading: true,
	},
	// 删除地址
	deleteAddress: {
		url: '/user/deleteAddress',
		method: 'DELETE',
		showLoading: true,
	},
	// 添加地址
	addAddress: {
		url: '/user/addAddress',
		method: 'POST',
		showLoading: true,
	},
	// 根据角色Id获取角色信息详情
	queryRoleInfoById: {
		url: '/user/queryRoleInfoById',
		method: 'GET',
		showLoading: true,
	},
	// 根据角色类型获取角色信息列表-讲解员-达人-摄影师
	queryRoleInfoList: {
		url: '/user/queryRoleInfoList',
		method: 'GET',
		showLoading: true,
	},
	// 添加收藏
	addCollection: {
		url: '/user/addCollection',
		method: 'POST',
		showLoading: true,
	},
	// 取消收藏
	deleteCollection: {
		url: '/user/deleteCollection',
		method: 'POST',
		showLoading: true,
	},
	// 获取收藏列表
	queryCollectionList: {
		url: '/user/queryCollectionList',
		method: 'GET',
		showLoading: true,
	},
	
	
	
	
	// 查询预约时间段
	queryOrderTime: {
		url: '/order/queryOrderTime',
		method: 'GET',
		showLoading: true,
	},
	// 查询订单列表
	queryOrderList: {
		url: '/order/queryOrderList',
		method: 'GET',
		showLoading: true,
	},
	
	
	
	//文创好物-获取商品详情
	queryWaresById: {
		url: '/wares/queryWaresById',
		method: 'GET',
		showLoading: true,
	},
	//文创好物-获取商品列表
	queryWaresList: {
		url: '/wares/queryWaresList',
		method: 'GET',
		showLoading: true,
	},
	// 添加购物车
	addShopcar: {
		url: '/wares/addShopcar',
		method: 'GET',
		showLoading: true,
	},
	// 获取购物车列表
	queryShopcarList: {
		url: '/wares/queryShopcarList',
		method: 'GET',
		showLoading: true,
	},
	// 取消购物车
	deleteShopcar: {
		url: '/wares/deleteShopcar',
		method: 'GET',
		showLoading: true,
	},
	// 修改购物车信息
	updateShopcar: {
		url: '/wares/updateShopcar',
		method: 'GET',
		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_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