import http from './http.js'

const config = {
	// 示例
	// wxLogin : {url : '/api/wxLogin', method : 'POST', 
	// auth : false, showLoading : true, loadingTitle : '加载中...',
	// limit : 1000
	// },
	// 维修员登录接口(手机号)
	repairLogin: { url: '/school/login/login', method: 'GET' , auth : false , showLoading : true , loadingTitle : '登录中...' ,  limit : 500 },
	//学生登录(微信)
	studentLogin: { url: '/school/login/loginWx', method: 'GET' , auth : false , showLoading : true , loadingTitle : '登录中...' ,  limit : 500 },
	//微信登录用户(学生)-修改个人信息
	updateInfo: { url: '/school-api/updateUserInfo', method: 'GET' , auth : true , limit : 500 },
	//微信登录用户(学生)-获取个人信息
	getUserInfo: { url: '/school-api/getUserInfo', method: 'GET' , auth : true , limit : 500 },
	//微信登录用户(学生)-增加报修单
	addSchoolOrder : {url : '/school-api/addUserSchoolOrder', method : 'GET', auth : true ,  limit : 500},
	//驳回(维修员)
	editSchoolOrderError : {url : '/school-api/editSchoolOrderError', method : 'GET', auth : true , limit : 500},
	//结单(维修员)
	editSchoolOrderSuccess : {url : '/school-api/editSchoolOrderSuccess', method : 'GET', auth : true , limit : 500},
	//微信登录用户(学生)-报修列表
	getUserSchoolOrderPage : {url : '/school-api/getUserSchoolOrderPage', method : 'GET', auth : true , limit : 500},
	//获取楼栋
	getActorGoList : {url : '/school-api/getActorGoList', method : 'GET', auth : false , limit : 500},
	//获取室号
	getFloorList : {url : '/school-api/getFloorList', method : 'GET', auth : false ,  limit : 500},
	//小程序(维修员)-列表-分配给维修的订单
	getSchoolOrderList : {url : '/school-api/getUserSchoolOrderList', method : 'GET', auth : true , limit : 500},
	//隐私政策
	getPrivacyPolicy : { url : '/school/login/getPrivacyPolicy', method : 'GET', auth : false , limit : 500},
	//用户协议
	getUserAgreement : { url : '/school/login/getUserAgreement', method : 'GET', auth : false , limit : 500},
	//获取维修项目
	getRepairList : { url : '/school-api/getRepairList', method : 'GET', auth : false , limit : 500},
	//获取流转明细
	getOrderFlowList : { url : '/school-api/getOrderFlowList', method : 'GET', auth : false , limit : 500},
}



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 = 'limit:' + req.url
		let storage = uni.getStorageSync(storageKey)
		if(storage && new Date().getTime() - parseInt(storage) < req.limit){
			return
		}
		uni.setStorageSync(storageKey, new Date().getTime())
	}
	
	//必须登录
	if (req.auth) {
		if (!uni.getStorageSync('token')) {
			uni.navigateTo({
				url: '/pages/login/studentLogin'
			})
			console.error('需要登录')
			return
		}
	}
	
	http.http(req.url, data, callback, req.method, 
	loadingTitle || req.showLoading, loadingTitle || req.loadingTitle)
}



export default api