import store from '@/store/index.js'
import {url,wsUrl,htoProtocol} from './config.js'

const customRequest = (opts, data) => {
	let configList = uni.getStorageSync('configList');
	let configIndex = uni.getStorageSync('configIndex');
	if(!configList || configIndex==-1){
		uni.showToast({
			title: '请求域名为空，请先添加',
			icon: 'none',
			duration: 1500
		})
		setTimeout(()=>{
			uni.navigateTo({
				url:'/pages/tabbar/config'
			})
		},1500)
		return null;
	}else{
		let basicDefaultOpts = {
			url: configList[configIndex].protocol + configList[configIndex].address + opts.url,
			data: data,
			method: opts.method,
			header: opts.header,
		}
		let promise = new Promise(function(resolve, reject) {
			uni.request(basicDefaultOpts).then((res) => {
				console.log("res",res);
				resolve(res[1].data)
				if(Object.prototype.toString.call(res[1].data) !== '[object Object]'){
					uni.showToast({
						title: '请求错误，请检查域名是否错误',
						icon: 'none',
						duration: 1500
					})
				}
			}).catch(
				(response) => {
					console.log("response",JSON.stringify(response));
					reject(response)
				}
			)
		})
		// console.log(JSON.stringify(promise));
		return promise
	}
	
		
};

const basicRequest = (opts, data) => {
	let basicDefaultOpts = {
		url: url + opts.url,
		data: data,
		method: opts.method,
		header: opts.header,
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(basicDefaultOpts).then((res) => {
			// console.log(JSON.stringify(res));
			resolve(res[1].data)
		}).catch(
			(response) => {
				reject(response)
			}
		)
	})
	// console.log(JSON.stringify(promise));
	return promise	
};

//带Token请求
const TokenRequest = (opts, data) => {
	let tokenKey = uni.getStorageSync('token');
	let configList = uni.getStorageSync('configList');
	let configIndex = uni.getStorageSync('configIndex');
	if(!configList || configIndex==-1){
		uni.showToast({
			title: '请求域名为空，请先添加',
			icon: 'none',
			duration: 1500
		})
		setTimeout(()=>{
			uni.navigateTo({
				url:'/pages/tabbar/config'
			})
		},1500)
		return null;
	}else{
		// tokenKey = store.getters.tokenKey;
		//此token是登录成功后后台返回保存在storage中的
		let DefaultOpts = {
			url: configList[configIndex].protocol + configList[configIndex].address + opts.url,
			data: data,
			method: opts.method,
			header: {
				'Authorization': tokenKey,
			},
		}
		let promise = new Promise(function(resolve, reject) {
			uni.request(DefaultOpts).then(
				(res) => {
					// console.log("请求返回",res)
					// 令牌过期关闭所有页面跳转登录页
					if(res[1].data.code==61){
						uni.showToast({
							title: '登录已过期，请重新登录',
							icon: 'none',
							duration: 1500
						})
						store.commit('setTokenKey', '');
						uni.removeStorageSync('token');
						
						setTimeout(()=>{
							uni.reLaunch({url:'/pages/tabbar/login'});
						},1500)
					}else if(res[1].data.code>0){
						uni.showToast({
							title: res[1].data.message,
							icon: 'none',
							duration: 1500
						})
					}
					resolve(res[1].data)
				}
			).catch(
				(response) => {
					console.log("请求报错",response)
					uni.showToast({
						title: '服务器开小差了呢，请您稍后再试',
						icon: 'none',
						duration: 1500
					})
					reject(response)
				}
			)
		})
		// console.log(JSON.stringify(promise));
		return promise
	}
}

//自带url请求，不使用公共域名的Token请求
const TokenURlRequest = (opts, data) => {
	let tokenKey = uni.getStorageSync('token');
	
	// tokenKey = store.getters.tokenKey;
	//此token是登录成功后后台返回保存在storage中的
	let DefaultOpts = {
		url: opts.url,
		data: data,
		method: opts.method,
		header: {
			'Authorization': 'Bearer '+tokenKey,
		},
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(DefaultOpts).then(
			(res) => {
				// 令牌过期关闭所有页面跳转登录页
				if(res[1].data.error=="invalid_token"){
					 uni.reLaunch({url:'/pages/index/login'});
				}
				resolve(res[1].data)
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	// console.log(JSON.stringify(promise));
	return promise
}

// 将对象导出外部引入使用
export default{
	// baseUrl,
	customRequest,
	basicRequest,
	TokenRequest,
	TokenURlRequest
}