// 'http://192.168.1.222:8088','https://file-dev.xyyxt.net/'线下
// 'https://api.xyyxt.net' 'https://file.xyyxt.net/'线上
import store from '@/store/index.js'
import api from './api.js'
var num = 1
//接口api   
// export const BASE_URL = 'https://api.xyyxt.net'   //release
export const BASE_URL = 'http://api.xyyxt.net:19009'   //预发布
// export const BASE_URL = 'http://42.192.164.187:19005'    //test
// export const BASE_URL = 'http://192.168.1.222:5055'    //dev

 //图片上传api
// export const BASE_IMG_URL = 'https://file.xyyxt.net/'     //release
export const BASE_IMG_URL = 'https://file-dev.xyyxt.net/'  //test


// export const WEBVIEW_URL = 'http://192.168.1.38:8082/'  //test
export const WEBVIEW_URL = 'http://api.xyyxt.net:6066/'  //预发布
// export const WEBVIEW_URL = 'https://m.xyyxt.net/'  //release

// export const socket_url = 'ws://42.192.164.187:19005/webSocket/'  //test
// export const socket_url = 'ws://120.79.166.78:19009/webSocket/'  //预发布
// export const socket_url = 'wss://api.xyyxt.net/webSocket/'  //release

export const  PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4qkbwIKErstK1sFESPEhOShpR
pj4+sOVpJHxl5r/2xLBfA/MrXcAEra5Ro9cXNQSqmLLt8wecoLk/glfa5IdhXV0h
RVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwU
hF6WiNlWfQTVoF1rhwIDAQAB
-----END PUBLIC KEY-----`;

export const version = '6.0.0' 
export const tenantId = '867735392558919680' 
export const myRequest = (options) => {
	if (store.state.allowLoading && !options.noLoading) {
		uni.showLoading({
			title: '拼命加载中...',
			mask:true,
		})
	}

	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || 'GET',
			data: options.data,
			header: options.noToken ? {
				TenantId: tenantId,
			} : {
				AuthorizationToken: 'WX ' + (token ? token : uni.getStorageSync('token_temp')),
				TenantId: tenantId
			},
			success: async (res) => {
				// if(num == 2) {
				// 	return;
				// } else {
				// 	num++
				// }
				// doRequest(options)
					
				if (res.data.code == 401) {
					if (num <= 2) {
						if (!uni.getStorageSync('user_account')) {
							var pages = getCurrentPages() // 获取栈实例
							let currentRoute  = pages[pages.length-1].route; // 获取当前页面路由
							if(currentRoute != 'pages4/login/login') {
								uni.navigateTo({
									url: '/pages4/login/login'
								});
							}
						} else {
							num++
							res = await doRequest(options)
						}

					}else{
						uni.removeStorageSync('user_account');
						var pages = getCurrentPages() // 获取栈实例
						let currentRoute  = pages[pages.length-1].route; // 获取当前页面路由
						if(currentRoute != 'pages4/login/login') {
							uni.navigateTo({
								url: '/pages4/login/login'
							});
						}
					}
				}
				resolve(res)
			},
			fail: (err) => {
				uni.showToast({
					title: "请求接口失败",
					icon: 'none'
				})
				reject(JSON.stringify(err))
			},
			complete: () => {
				uni.hideLoading()
				//		uni.hideToast()
			}
		})
	})
	async function doRequest(response) {
		let user_account = uni.getStorageSync('user_account')
		var datas = {
			url: '/refreshToken/' + user_account,
			method: 'get',
			noToken: true
		}
		const res = await myRequest(datas)
		if (res.data.code === 200) {
			uni.setStorageSync('token', res.data.data.token)

			var userInfo = {
				url: '/app/user/getInfo',
				method: 'get',
			}
			const resUser = await myRequest(userInfo)
			if (resUser.data.code === 200) {
				store.state.userInfo = resUser.data.data
				uni.setStorageSync('user_account',user_account)
				num = 1;
			}
			let onset = await myRequest(response)
			return onset
		} else {
			var pages = getCurrentPages() // 获取栈实例
			let currentRoute  = pages[pages.length-1].route; // 获取当前页面路由
			if(currentRoute != 'pages4/login/login') {
				uni.navigateTo({
					url: '/pages4/login/login'
				});
			}
		}
	}
}
