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'
	},
	// 获取客服联系电话
	customUser: {
		url: '/user/custom',
		method: 'get',
	},

	// banner列表
	bannerList: {
		url: '/banner/list',
		method: 'GET',
		showLoading: true,
	},

	// 公告列表
	noticeList: {
		url: '/notice/list',
		method: 'GET',
		showLoading: true,
	},
	// 公告详情
	noticeOne: {
		url: '/notice/one',
		method: 'GET',
		showLoading: true,
	},


	// 商品列表
	goodsPage: {
		url: '/goods/page',
		method: 'GET',
		showLoading: true,
		debounce : 300,
	},
	// 商品详情
	goodsOne: {
		url: '/goods/one',
		method: 'GET',
		showLoading: true,
	},
	// 购物车分页
	cartPage: {
		url: '/goods/shopping/cart/page',
		method: 'GET',
		showLoading: true,
		auth: true,
	},
	// 加入购物车
	cartAdd: {
		url: '/goods/join/shopping/cart',
		method: 'POST',
		showLoading: true,
		auth: true,
	},
	// 删除购物车
	cartDel: {
		url: '/goods/shopping/cart/delete',
		method: 'POST',
		showLoading: true,
		auth: true,
	},
	// 修改购物车数量
	cartNum: {
		url: '/goods/shopping/cart/add/or/delete',
		method: 'POST',
		auth: true,
		debounce: 1000,
	},



	//分类列表
	categoryList: {
		url: '/category/list',
		method: 'GET',
		showLoading: true,
	},

	//新增地址
	addressAdd: {
		url: '/address/add',
		method: 'POST',
		limit: 500,
		auth: true,
		showLoading: true,
	},
	//修改默认地址
	addressDefault: {
		url: '/address/default',
		method: 'POST',
		auth: true,
		showLoading: true,
	},
	//删除地址
	addressDelete: {
		url: '/address/delete',
		method: 'POST',
		auth: true,
		showLoading: true,
	},
	//修改地址
	addressEdit: {
		url: '/address/edit',
		method: 'POST',
		limit: 500,
		auth: true,
		showLoading: true,
	},
	//分页查询地址
	addressPage: {
		url: '/address/page',
		method: 'GET',
		auth: true,
		showLoading: true,
	},
	// 分页查询我的客户
	getMyUserPage: {
		url: '/shop/user',
		method: 'GET',
		auth: true,
		showLoading: true,
	},


	//优惠券分页接口
	couponPage: {
		url: '/coupon/page',
		method: 'GET',
		auth: true,
		showLoading: true,
	},
	
	//分页查询订单
	orderPage: {
		url: '/order/page',
		method: 'GET',
		auth: true,
		showLoading: true,
	},
	//查询订单详情
	orderOne: {
		url: '/order/one',
		method: 'GET',
		auth: true,
		showLoading: true,
	},


	// 微信登录接口
	wxLogin: {
		url: '/login/login',
		method: 'POST',
		limit: 500,
		showLoading: true,
	},

	// 修改个人信息接口
	updateInfo: {
		url: '/user/edit',
		method: 'POST',
		auth: true,
		limit: 500,
		showLoading: true,
	},
	// 获取用户信息
	getInfo: {
		url: '/user/info',
		method: 'GET',
		auth: true,
		showLoading: true,
	},


	// 获取我的租赁
	getLeasePage: {
		url: '/user/lease/page',
		method: 'POST',
		showLoading: true,
	},
	// 申请成为水洗店
	applyShopHotel: {
		url: '/shop/apply',
		method: 'POST',
		showLoading: true,
		auth: true,
	},
	// 店铺申请提现
	storeWithdrawalApplication: {
		url: '/shop/withdrawal/apply',
		method: 'POST',
		showLoading: true,
		auth: true,
	},
	// 提现日志
	withdrawalLog: {
		url: '/shop/withdrawal/page',
		method: 'GET',
		showLoading: true,
		auth: true,
	},
	// 水洗店首页数据
	laundryStoreHomeData: {
		url: '/shop/index',
		method: 'GET',
		// showLoading : true,
		auth: true,
	},
	// 获取绑定店铺的数据
	bindShop: {
		url: '/shop/bind',
		method: 'GET',
		// showLoading : true,
		auth: true,
	},
	// 修改店铺营业时间
	updateJobTime: {
		url: '/shop/job/time',
		method: 'POST',
		// showLoading : true,
		auth: true,
	},
	// 退货、换货申请
	refundOrder: {
		url: '/order/refund',
		method: 'POST',
		// showLoading : true,
		auth: true,
	},
	// 商品租赁下订单
	orderCreate: {
		url: '/order/create',
		method: 'POST',
		showLoading : true,
		limit: 500,
		auth: true,
	},
	
	// 商品租赁订单支付
	orderPay: {
		url: '/order/pay',
		method: 'POST',
		showLoading : true,
		limit: 500,
		auth: true,
	},
	// 确认收货
	orderConfirm: {
		url: '/order/confirm',
		method: 'POST',
		showLoading : true,
		limit: 500,
		auth: true,
	},
	// 根据商品id获取规格
	goodsSku: {
		url: '/goods/sku',
		method: 'GET',
		auth: true,
	},
	// 购物车修改多规格
	editSku: {
		url: '/goods/shopping/cart/edit/sku',
		method: 'POST',
		auth: true,
	},
	
	
	// 水洗店账号绑定微信
	bindWx: {
		url: '/shop/bind/wx',
		method: 'POST',
		auth: true,
	},
	// 上传自己的桌布
	tablecloth: {
		url: '/order/upload/tablecloth',
		method: 'POST',
		auth: true,
	},
	// 水洗店确认接单
	orderConfirmAccept: {
		url: '/order/confirm/accept',
		method: 'POST',
		auth: true,
	},
	// 酒店换货支付
	replacePay: {
		url: '/order/replace/pay',
		method: 'POST',
		auth: true,
	},
	// 水洗店换货发货
	sendLogistics: {
		url: '/order/send/logistics',
		method: 'POST',
		auth: true,
	},
	// 获取全局总金额优惠满减
	confCoupon: {
		url: '/conf/coupon',
		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')) {
			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