import http from './http.js'
import utils from '../utils/utils.js'
import commentApi from './model/comment.js'


let limit = {}
let debounce = {}

const config = {
	// 示例
	// wxLogin : {url : '/api/wxLogin', method : 'POST', 
	// auth : false, showLoading : true, loadingTitle : '加载中...',
	// limit : 1000
	// },
	
	getConfig : {url : '/login/getConfigInfo', method : 'GET', auth: false, limit : 500},
	
	
	// 微信登录接口
	wxLogin: {
		url: '/login/login',
		method: 'GET', 
		limit : 500,
		showLoading : true,
	},
	// 修改个人信息接口
	updateInfo: {
		url: '/token/updateInfo',
		method: 'POST',
		auth: true, 
		limit : 500,
		showLoading : true,
	},
	// 获取个人信息接口
	getInfo: {
		url: '/token/getInfo',
		method: 'GET',
		auth: true,
		limit : 500,
	},
	// 绑定手机号码
	bindPhone: {
		url: '/login/bindPhone',
		method: 'GET',
		auth: true,
		limit : 500,
	},
	//隐私政策
	getPrivacyPolicy: {
		url: '/login/getPrivacyPolicy',
		method: 'GET',
	},
	//用户协议
	getUserAgreement: {
		url: '/login/getUserAgreement',
		method: 'GET',
	},
	
	
	// 不需要登录的接口
	
	
	//获取分类
	getClassInfo: {
		url: '/city/getClassInfo',
		method: 'GET',
		auth: false,
	},
	//获取首页头部信息
	getIndexHeaderInfo: {
		url: '/city/getIndexHeaderInfo',
		method: 'GET',
		auth: false,
	},
	//获取banner列表
	getBannerList: {
		url: '/city/getBannerList',
		method: 'GET',
		auth: false,
	},
	//获取分类类型列表
	getClassifyList: {
		url: '/city/getClassifyList',
		method: 'GET',
		auth: false,
	},
	//获取工作信息列表
	getJobPage: {
		url: '/city/getJobPage',
		method: 'GET',
	},
	//获取工作详情
	getJobDetail: {
		url: '/city/getJobDetail',
		method: 'GET',
	},
	//根据分类获取租房信息列表
	getRentPage: {
		url: '/city/getRentPage',
		method: 'GET',
	},
	//获取租房详情
	getRentDetail: {
		url: '/city/getRentDetail',
		method: 'GET',
	},
	//获取动态帖子列表
	getPostPage: {
		url: '/city/getPostPage',
		method: 'GET',
	},
	//获取帖子详情
	getPostDetail: {
		url: '/city/getPostDetail',
		method: 'GET',
	},
	//获取活动列表信息
	getActivityPage: {
		url: '/city/getActivityPage',
		method: 'GET',
	},
	//获取活动详情
	getActivityDetail: {
		url: '/city/getActivityDetail',
		method: 'GET',
	},
	//获取活动详情
	getActivityDetail: {
		url: '/city/getActivityDetail',
		method: 'GET',
	},
	//获取门店信息列表（美食）
	getStorePage: {
		url: '/city/getStorePage',
		method: 'GET',
	},
	//获取门店详情（美食）
	getStoreDetail: {
		url: '/city/getStoreDetail',
		method: 'GET',
	},
	//发布按钮列表
	getPublishList: {
		url: '/city/getPublishList',
		method: 'GET',
	},
	//获取城市列表服务区域
	getCityList: {
		url: '/city/getCityList',
		method: 'GET',
	},
	//获取景点列表带分页
	getScenicPage: {
		url: '/city/getScenicPage',
		method: 'GET',
	},
	//获取景点详情
	getScenicDetail: {
		url: '/city/getScenicDetail',
		method: 'GET',
	},
	//获取江华人信息接口
	getJiangHuInfo: {
		url: '/city/getJiangHuInfo',
		method: 'GET',
	},
	//获取人找车分页列表
	getPeoplePage: {
		url: '/city/getPeoplePage',
		method: 'GET',
	},
	//获取人找车详情
	getPeopleDetail: {
		url: '/city/getPeopleDetail',
		method: 'GET',
	},
	//获取车找人分页列表
	getCatPage: {
		url: '/city/getCatPage',
		method: 'GET',
	},
	//获取车找人详情
	getCatDetail: {
		url: '/city/getCatDetail',
		method: 'GET',
	},
	//查询商城信息列表带分页
	getShopPingPage : {
		url: '/city/getShopPingPage',
		method: 'GET',
	},
	//根据商品标识查询商品信息详情
	getShopPingDetail : {
		url: '/city/getShopPingDetail',
		method: 'GET',
	},
	
	
	
	
	
	
	//发布帖子\动态
	publishPost: {
		url: '/token/publishPost',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//发布租房信息
	publishRent: {
		url: '/token/publishRent',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//发布招聘信息
	publishJob: {
		url: '/token/publishJob',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//发布车找人信息
	publishCar: {
		url: '/token/publishCar',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//查询自己发布的动态
	getMyPostPage: {
		url: '/token/getMyPostPage',
		method: 'GET',
		auth : true,
	},
	//删除自己发布的动态
	deletePost: {
		url: '/token/deletePost',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	//店铺认证
	companyAuthentication: {
		url: '/token/companyAuthentication',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//个人认证
	personalAuthentication: {
		url: '/token/personalAuthentication',
		method: 'POST',
		limit : 1000,
		auth : true,
		showLoading : true,
	},
	//获取分享二维码
	getQrCode: {
		url: '/token/getQrCode',
		method: 'GET',
		auth : true,
	},
	//获取粉丝列表接口
	getFansList: {
		url: '/token/getFansList',
		method: 'GET',
		auth : true,
	},
	
	
	
	// 用户查询地址列表
	getAddressList : {
		url: '/token/getAddressList',
		method: 'GET',
		auth : true,
	},
	// 用户获取地址信息列表详情
	getAddressDetail : {
		url: '/token/getAddressDetail',
		method: 'GET',
		auth : true,
	},
	// 用户增加或者编辑地址信息
	addOrEditAddress : {
		url: '/token/addOrEditAddress',
		method: 'POST',
		auth : true,
		showLoading : true,
		limit : 1000,
	},
	// 删除地址
	deleteAddress : {
		url: '/token/deleteAddress',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	
	
	
	// 获取今日是否签到
	getTodaySign : {
		url: '/token/getTodaySign',
		method: 'GET',
		auth : true,
		showLoading : true,
	},
	// 签到
	sign : {
		url: '/token/sign',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	// 查看积分流水
	getPointsFlow : {
		url: '/token/getPointsFlow',
		method: 'GET',
		auth : true,
		showLoading : true,
	},
	// 查看钱包流水
	getWalletFlow : {
		url: '/token/getWalletFlow',
		method: 'GET',
		auth : true,
		showLoading : true,
	},
	
	//提现
	storeWithdrawalApplication : {
		url: '/token/withdraw',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	// 点击查看联系方式 类型 0帖子 1租房 2工作 3门店
	checkGivePhone : {
		url: '/token/checkGivePhone',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
	
	// 查询店铺中的商品、美食
	getGoodsList : {
		url: '/token/getGoodsList',
		method: 'GET',
	},
	
	// 查询我的店铺
	myShop : {
		url: '/token/myShop',
		method: 'GET',
		auth : true,
	},
	
	// 领取提现成功
	requestMerchantTransfer : {
		url: '/cashout/getMoney',
		method: 'GET',
		auth : true,
	},
	
	// 删除我的招聘
	deleteMyJob : {
		url: '/token/deleteMyJob',
		method: 'POST',
		auth : true,
	},
	
	// 删除我的租房
	deleteMyRent : {
		url: '/token/deleteMyRent',
		method: 'POST',
		auth : true,
	},
	
	// 编辑我的招聘
	editMyJob : {
		url: '/token/editMyJob',
		method: 'POST',
		auth : true,
	},
	
	// 编辑我的租房
	editMyRent : {
		url: '/token/editMyRent',
		method: 'POST',
		auth : true,
	},
	
	// 查询我的招聘
	getMyJob : {
		url: '/token/getMyJob',
		method: 'GET',
		auth : true,
	},
	
	// 查询我的租房
	getMyRent : {
		url: '/token/getMyRent',
		method: 'GET',
		auth : true,
	},
	
	// 新-获取我的客服信息列表
	getMyService : {
		url: '/token/getMyService',
		method: 'GET',
		auth : true,
	},
	
	// 新-获取大转盘抽奖列表
	getLuckDrawList : {
		url: '/token/getLuckDrawList',
		method: 'GET',
		auth : true,
	},
	
	// 新-大转盘抽奖
	luckDraw : {
		url: '/token/luckDraw',
		method: 'POST',
		auth : true,
		showLoading : true,
	},
}

const models = ['order', 'group', 'article', 'browseRecord']

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

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, 
		req)
}



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]
	}
}

// 添加评论模块API
addApiModel(commentApi, 'comment')



export default api