/**
 * @Method Description
 * @Author: zjd@
 * @Description: 数据请求整合 处理
 * @BASE_URL server 
 * @param {a===Object||file} 传给后台参数Method 请求方法   url  所请求的接口路径
 * @return  Promise对象  所有数据信息
 * @createTime: 2024-7-22 15:05:06
 */
import BASE_URL from "./config.js";
export const request = (url, data = {}, method = 'post', loading = false, contentType =
	'application/json',extraHeader={}) => {
	if (loading) {
		uni.showLoading({
			title: '加载中',
			mask: true
		})

	};
	let token = '';
	if (process.env.UNI_PLATFORM == "h5") {
		if (window.location.href.indexOf('//.igandan.com') > -1) {
			token = uni.getStorageSync('AUTH_TOKEN_App');
		} else {
			token = uni.getStorageSync('DEV_AUTH_TOKEN_App');
		}
	} else if(process.env.UNI_PLATFORM == "mp-weixin") {
		const {
			envVersion
		} = uni.getAccountInfoSync().miniProgram;
		if (envVersion == "release") {
			token = uni.getStorageSync('AUTH_TOKEN_App');
		} else {
			token = uni.getStorageSync('DEV_AUTH_TOKEN_App');
		}
	}else{
		if (BASE_URL.indexOf('dev') == -1) {
			token = uni.getStorageSync('AUTH_TOKEN_App');
			
		} else {
			token = uni.getStorageSync('DEV_AUTH_TOKEN_App');
		}
	}
	let header = {
		'content-type': contentType,
	}
	let defaultData={}
	
	let freeList=['/manager/getSystemTimeStamp','/expertAPI/smsLogin','/login/mobile','/expertAPI/login','/expertAPI/index']
	 let postData={
		 ...data
	 }
	if(freeList.indexOf(url)!=-1){
		if(freeList[4].indexOf(url)!=-1){
			 if(!token){
				 header['Authorization']='Bearer ' + ''
			 }else{
				  header['Authorization']='Bearer ' + token;
				  let userInfo= uni.getStorageSync('userInfo')
				  defaultData = {
				  			version: '4.0.0',
				  			user_uuid:userInfo.uuid,
				  			client_type: 'A', //client_type,
							timestamp:new Date().getTime()
				  		}
				postData={
						 ...data,
						 ...defaultData
				}
			 }
		}
	}else{
			if(token){
				 header['Authorization']='Bearer ' +token;
				 let userInfo= uni.getStorageSync('userInfo')
				 console.log(userInfo)
				   defaultData = {
				 			version: '4.0.0',
				 			user_uuid:userInfo.uuid,
				 			client_type: 'A', //client_type,
							timestamp:new Date().getTime()
				 	}
				
					postData={
							 ...data,
							 ...defaultData
					}
			}
   }
	
      
	
	return new Promise(function(e, n) {
		let timestamp = Date.now();
		uni.request({
			data: postData,
			url: url.indexOf('http') != -1 ? url : encodeURI(BASE_URL + url),
			method: method,
			sslVerify: false,
			header:{...header,...extraHeader},
			timeout: 10000,
			success: async (res) => {
				var Authorization_token = res.header.Authorization;
				if (Authorization_token) {
					if (process.env.NODE_ENV === 'development') {
						uni.setStorageSync('DEV_AUTH_TOKEN_App', Authorization_token);
					} else {
						uni.setStorageSync('AUTH_TOKEN_App', Authorization_token);
					}

				}
				if (loading) {
					uni.hideLoading();
				};
				if(url.indexOf('manager/getSystemTimeStamp')!=-1){
					e(res)
				}else if (res.data.code == 200 || res.data.code == 1 || res.data.code == "1" || res.data.code == "200"){
					e(res.data)
				}else if (res.data.code == 401 || res.data.code == 403 || res.data.code ==
					405 || res.data.code == 406 || res.data.code == 37006) {
					
					uni.redirectTo({
						url: '/pages_app/login/login'
					});
					

				} else if (res.data.code == 500) {

					uni.showToast({
						title: res.data.message,
						icon: 'none',

					})
					n(res)
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',
					});
					n(res)


				}
			},
			fail: function(err) {
				"request:fail " === err.errMsg && msg("请求数据失败！"), n(err.data);
			}
		});
	});
}