//引入配置文件
import config from './config.js'
import * as comon from './common.js' //引入common
import * as db from './db.js'
//需要token验证接口
let retryRequests=[];
const send = (url,method,data,callback, complete) => {
	let token = db.get("token");
	let header={}
	if(token){
		 header={
			'Accept': 'application/json',
			'Content-Type': 'application/json',
			'Authorization':db.get('token')
		}
	}else{
		 header={
			'Accept': 'application/json',
			'Content-Type': 'application/json',
		}
	}
	uni.request({
		url:config.apiBaseUrl+url,
		data:data,
		header: header,
		method: method,
		success: (response) => {
			if (response.data.code === 40000) {
				uni.showToast({
					icon: 'loading',
					title: response.data.msg ? response.data.msg : "请登录！",
					success: () => {
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/my/login'
							})
						}, 1500)
					}
				});
				return false;
			}
			const result = response.data
			callback(result);
			
		},
		complete: (response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			complete ? complete() : "";
		},
		fail: (error) => {
			console.log('2222222',error)
			uni.showLoading({
				title: '网络开小差了'
			});
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {}
		},
	});
}

 const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}
	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 登录
export const loginApi=(data,callback)=>send('/wechat/code2Session/',"POST",data,callback)
// 绑定
export const loginBind=(data,callback)=>send('/wechat/wxbind/','POST',data,callback)
// 轮播图
export const getSwiper=(callback)=>send('/wechat/activity/list/','GET','',callback)
// 轮播图
export const getNewCar=(is_hot,CarName,callback)=>send(`/wechat/vehicle/list/?is_hot=${is_hot}&name=${CarName}`,'GET','',callback)
// 买车详情 
export const getBuyCar=(series_id,callback)=>send(`/wechat/vehicle/models/?series_id=${series_id}`,'GET','',callback)
//买车详情列表
export const getBuyCarDetial=(id,callback)=>send(`/wechat/vehicle/model/detail/?id=${id}`,'GET','',callback)
// 二手车 
export const getUsedCarList=(name,brand,gearbox,color,displacement,emission,seat_count,oil_type,price_range,mileage__range,years_range,callback)=>send(`/wechat/usedvehicle/list/?name=${name}&brand=${brand}&gearbox=${gearbox}&color=${color}&displacement=${displacement}&emission=${emission}&seat_count=${seat_count}&oil_type=${oil_type}&price_range=${price_range}&mileage__range=${mileage__range}&years_range=${years_range}`,'GET','',callback)
// 二手车详情
export const getUsedCarDetial=(id,callback)=>send(`/wechat/usedvehicle/detail/?id=${id}`,'GET','',callback)
// 门店
export const getStoreList=(name,callback)=>send(`/wechat/store/list/?name=${name}`,'GET','',callback)
// 商品列表
export const getProductList=(name,id,callback)=>send(`/wechat/product/list/?name=${name}&category=${id}`,'GET','',callback)
// 商品类别
export const getProductType=(callback)=>send(`/wechat/product/types/`,'GET','',callback) 
// 商品详情
export const getProductDetial=(id,callback)=>send(`/wechat/product/detail/?id=${id}`,'GET','',callback)
// 搜索类型
export const getSearchType=(callback)=>send(`/wechat/usedvehicle/options/`,'GET','',callback)
// 询问底价
export const postAskPrice=(data,callback)=>send(`/wechat/customer/inquiry/`,'POST',data,callback)
// 添加地址
export const postAddress=(data,callback)=>send(`/wechat/customer/address/`,'POST',data,callback)
// 获取地址列表
export const getAddress=(callback)=>send(`/wechat/customer/address/`,'GET','',callback)
// 修改地址
export const postEdimtAddress=(id,data,callback)=>send(`/wechat/customer/address/${id}/addr_update/`,'POST',data,callback)
// 删除地址
export const getdeleteAddress=(id,callback)=>send(`/wechat/customer/address/${id}/addr_destroy/`,'GET','',callback)
// 修改默认选项
export const getRadio=(id,status,callback)=>send(`/wechat/customer/address/${id}/set_default/?status=${status}`,'GET','',callback)
// 卖车
export const postUserdCar=(data,callback)=>send(`/wechat/customer/usedvehicle_estimate/`,'POST', data,callback)
// 意向门店列表
export const getBrands=(callback)=>send(`/wechat/customer/usedvehicle/brands/`,'GET','',callback)
// 预约试驾
export const postDriveReserve=(data,callback)=>send(`/wechat/customer/drive_reserve/`,'POST',data,callback)
// 服务类型
export const getServeType=(callback)=>send(`/wechat/customer/maint_reserve/options/`,'GET','',callback)
// 预约服务
export const postreserve=(data,callback)=>send(`/wechat/customer/maint_reserve/`,'POST',data,callback)
// 下单页面
export const postshopdetial=(id,callback)=>send(`/wechat/customer/plan_product_order/?id=${id}`,'GET','',callback)
// 下单
export const postPlaceOrder=(data,callback)=>send(`/wechat/customer/product_order/add/`,'POST',data,callback)
// 上传头像
export const postupUserInfo=(data,callback)=>send(`/wechat/setUserInfo/`,'POST',data,callback)
// 获取头像信息
export const getUserInfo=(callback)=>send(`/wechat/info/`,'GET','',callback)
// 车辆绑定
export const postBindCar=(data,callback)=>send(`/wechat/customer/vehicle/`,'POST',data,callback)
// 询价单列表
export const getInquiry=(callback)=>send(`/wechat/customer/inquiry/`,'GET','',callback)
// 预约试驾列表
export const getdriveReserve=(callback)=>send(`/wechat/customer/drive_reserve/`,'GET','',callback)
// 保养 
export const getmaintReserve=(callback)=>send(`/wechat/customer/maint_reserve/`,'GET','',callback)
// 活动
export const getAction=(id,callback)=>send(`/wechat/activity/detail/?id=${id}`,'GET','',callback)
// 订单列表
export const getorderlist=(callback)=>send(`/wechat/customer/product_order/`,'GET','',callback)
// 置换列表
export const getUsedvehicleEstimate=(callback)=>send(`/wechat/customer/usedvehicle_estimate/`,'GET', '',callback)
// 车辆列表
export const getBindCar=(callback)=>send(`/wechat/customer/vehicle/`,'GET','',callback)
// 续保咨询
export const postConsultation=(data,callback)=>send(`/wechat/customer/insur_consultation/`,'POST',data,callback)
// 维修记录
export const getMaintenanceLog=(id,callback)=>send(`/wechat/customer/get_maint_order/?id=${id}`,'GET','',callback)