import store from '@/store'
import config, {
	baseUrl,
	foreignUrl
} from '@/config'
import {
	getToken,
	getServerUrl
} from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import {
	toast,
	showConfirm,
	tansParams
} from '@/utils/common'

let timeout = 200000

console.log(baseUrl)
const request1 = config => {
	// 是否需要设置 token
	const isToken = (config.headers || {}).isToken === false
	config.header = config.header || {}
	// if (getToken() && !isToken) {
	// 	// config.header['Authorization'] = 'Bearer ' + getToken()
	// 	config.header['UserKey'] = getToken()
	// 	config.header['ServerUrl'] = getServerUrl()
	// } else {
	// 	uni.reLaunch({
	// 		url: '/pages/index/login'
	// 	})
	// }
	// get请求映射params参数
	if (config.params) {
		let url = config.url + '?' + tansParams(config.params)
		url = url.slice(0, -1)
		config.url = url
	}
	const xml2js = require('xml2js');
	return new Promise((resolve, reject) => {
		let requestUrl = uni.getStorageSync("Url") + "/k3cloud/services/LUTAIWebService.asmx/"
		console.log(requestUrl)
		uni.request({
			method: config.method || 'get',
			timeout: config.timeout || timeout,
			url: requestUrl + config.url,
			data: config.data,
			header: config.header,
			dataType: 'json',
			success: res => {
				if (res.statusCode == 200) {
					xml2js.parseString(res.data, (err, result) => {
						console.log(err)
						console.log(result)
						if (err) {
							reject(err);
						}
						// 解析后的结果保存在result中
						const jsonResult = JSON.parse(result.string._);
						resolve(jsonResult)
						// if(jsonResult.flag){
						// 	resolve(jsonResult.result)
						// }else{
						// 	reject(jsonResult.message)
						// }
					});
				} else {
					reject("获取数据失败！")
					console.log(res)
				}

			},
			fail: err => {
				console.log("获取数据失败")
				reject(err)
			},
			complete: () => {
				//uni.hideLoading()
			}
		})
	})
}
export default request1