/**
 * @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";
//import host from "@/utils/host";
//import {msg} from "./util.js"
import pageUrl from './pageUrl'
// #ifdef H5
import api from '../api/api.js'
import cookie from './cookie.js'
// #endif
//const BASE_URL=host+"/api"
export const request = (url, data = {}, method = 'post', loading = false, contentType = 'application/x-www-form-urlencoded') => {
	if (loading) {
		uni.showLoading({
			title: '加载中',
			mask: true
		})

	};
	let token = '';
	if(process.env.UNI_PLATFORM =="h5"){
		if(window.location.href.indexOf('//casedata.igandan.com')>-1){
			token = uni.getStorageSync('AUTH_TOKEN_CASEDATA');
		}else{
			token = uni.getStorageSync('DEV_AUTH_TOKEN_CASEDATA');
		}
	}else{
		  const { envVersion } = uni.getAccountInfoSync().miniProgram;
          if (envVersion == "release") {
			token = uni.getStorageSync('AUTH_TOKEN_CASEDATA');
		  }else{
			token = uni.getStorageSync('DEV_AUTH_TOKEN_CASEDATA');
		  }
	}
	
	//  if(!token){
	//   let freeList=['/login','/code/phone','/login/wx','/index','/user/check'];
	//     if(freeList.indexOf(url) == -1){
	// 	 let page_url=pageUrl();
	// 	 if(page_url.indexOf('/login/login')==-1){

	// 		 uni.setStorageSync('redirectUrl',page_url);
	// 		 uni.navigateTo({
	// 		    url: '/pages/login/login?redirectUrl=has'
	// 		 });
	// 		 return false;
	// 	 }else{


	// 		 uni.setStorageSync('redirectUrl','');
	// 		 uni.navigateTo({
	// 		    url: '/pages/login/login'
	// 		 });
	// 		  return false;
	// 	 }

	// }
	//  }
	let header = {
		'content-type': contentType,
		'Authorization': 'Bearer ' + token
	}
	return new Promise(function (e, n) {
		let timestamp = Date.now();
		uni.request({
			data,
			url: url.indexOf('http') != -1 ? url : encodeURI(BASE_URL + url + "?timestamp=" + timestamp),
			method: method,
			sslVerify: false,
			header: url.indexOf('/manager/getSignature4bing') == -1 ? header : {},
			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_CASEDATA', Authorization_token);
					} else {
						uni.setStorageSync('AUTH_TOKEN_CASEDATA', Authorization_token);
					}

				}
				if (loading) {
					uni.hideLoading();
				};
				if (res.data.code == 200) {
					e(res)
				} else if (res.data.code == 401 || res.data.code == 403 || res.data.code == 405 || res.data.code == 406) {
					if(process.env.UNI_PLATFORM =="h5"){
					let video_token = cookie.readCookie('video_token');
					console.log(video_token);
					if(video_token){
					 const res = await api.h5Login({
						token: video_token
					});
				    let result = res.data.data;
					console.log(result);
					if (window.location.href.indexOf('//casedata.igandan.com')>-1) {
						uni.setStorageSync("AUTH_TOKEN_CASEDATA",result.token);
					} else {
						uni.setStorageSync("DEV_AUTH_TOKEN_CASEDATA",result.token);
					};
						uni.setStorageSync("userInfo",{
						avatar:result.avatar,
						user_id:result.user_id,
						status:result.status,
						user_name:result.user_name,
						doctor_id:result.doctor_id,
					});
					window.location.reload();
					}else{
						let H5url = 'https://dev-wx.igandan.com';
						if(window.location.href.indexOf('//dev-casedata.igandan.com') == -1) {
							H5url = 'https://wx.igandan.com'
						}
						window.location.href = H5url + "/hcp/Signup2020online_tologin?back_url=" + encodeURIComponent(window.location.href);
					}
					}else{
							let freeList = ['/login/wechat/mobile', '/code/phone', '/login/mobile_login', '/index', '/user/check'];
					if (freeList.indexOf(url) == -1) {
						let page_url = pageUrl();
						uni.setStorageSync('redirectUrl', page_url);
						uni.navigateTo({
							url: '/pages/login/login?redirectUrl=has'
						});
						return false
					}
					}
			
				} else if (res.data.code == 500) {
					n(res)
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none',

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