//用BASEURL来表示域名地址
// const BASE_URL = 'http://192.168.110.153:9002'
// const BASE_URL = 'https://zswl.api.jpy.wang/zswl-cloud-bdb'
// const BASE_URL = 'https://api.dev.zonelife.cn'
import {
	BASE_URL
} from '@/utils/config.js'

//封装请求方法
export let request = (options) => {
	return new Promise((resolve, reject) => {
			//请求路径拼接，，其中options.url就是通过下面方法myRequest获取到接口部分的url
			//method--请求方法，不是method的post就是get
			console.log('requert',options.url.indexOf('/zswl-cloud-bdb'))
			let base_url = BASE_URL
			options.url = options.url.replace('/zswl-cloud-shop','');
			base_url = options.url.indexOf('/zswl-cloud-bdb') != -1 ? 'https://api.dev.zonelife.cn' : BASE_URL
			base_url = options.url.indexOf('/springbatchservice') != -1 ? 'https://api.dev.zonelife.cn' : base_url  
			if(options.url.indexOf('http')==-1){
				options.url = base_url + options.url
			}
			console.log(options.url)
			//请求的参数，当没有参数的时候就是空对象
			request.requestTask =uni.request({
					url: options.url,
					method: options.method || 'GET',
					data: options.data || {},
					header: {
						accessToken: uni.getStorageSync('token')
					},
					success: (res) => {
						if (res.statusCode !== 200) {
							return uni.showToast({
								title: '获取失败',
								icon: 'error',
							})
							// uni.hideLoading()
						} else if (res.data.msg != '成功') { //接口返回报错
							uni.showToast({
								title: res.data.content || res.data.msg || '请求失败',
								icon: 'none',
								duration:3000
							})
							console.error('报错接口：',base_url + options.url,'报错信息：', res.data);

							// token过期或出了问题
							if (res.data.exception && res.data.exception.type ==
								'AuthenticationCredentialsNotFoundException') {
									uni.showToast({
										title: '登录过期',
										icon: 'error'
									})
								if (uni.getStorageSync('refreshToken') && uni.getStorageSync('token') ) {
									// 刷新token
									uni.showLoading({
										title: '刷新登录中',
									})
									uni.request({
											url: base_url + '/zswl-cloud-bdb/user/refreshToken',
											method: 'GET',
											data: {
												refreshToken: uni.getStorageSync('refreshToken')
											},
											success(r) {
												uni.hideLoading()
												if (r.data.content == '刷新令牌错误') {
													uni.showToast({
														title: '登录失效',
														icon: 'fail'
													})
													uni.clearStorageSync()
													uni.reLaunch({
														url: '/pages/index/index'
													})
												} else {
													
													uni.setStorageSync('token', r.data.content) //登录状态
													// 刷新页面
													var pages = getCurrentPages();
													var url = '/' + pages[pages.length - 1].route
													uni.reLaunch({
														url
													})
													
											}
										}
									})
								}else{
									
									uni.showModal({
										title:'请登录',
										confirmText:'去登录',
										success(res){
											if(res.confirm){
												var pages = getCurrentPages()
												let parmas = pages[pages.length - 1].options
												console.log(11111,parmas);
												let str = ''
												for (let key in parmas) {
													str += `&${key}=${parmas[key]}`
												}
												console.log(2222222,str);
												var url = '/' + pages[pages.length - 1].route
												console.log(333333,url);
												uni.reLaunch({
													url:'/login/login/login?redirect='+url+str
												})
											}else if (res.cancel) {
												console.log('拒绝登录，跳转首页');
												uni.switchTab({
													url:'/pages/index/index'
												})
											}
										}
									})
									
								}
						}

					}
					resolve(res.data)
				},
				//请求失败
				fail: (err) => {
					uni.showToast({
						title: '请求接口失败'
					})
					reject(err)
				}
			})
	})
}