import {initHeaderDevice} from './header.js'
const request=async(url,type,data)=>{
	// const baseUrl='http://portal.cloudsmake.cn' //测试
	const baseUrl='https://api.yyhock.com'//线上
	// 这里确保一定是可以获取得到的
	const headerDevice = uni.getStorageSync('m-header-device')
	let rd = null;
	if (headerDevice) {
		rd = headerDevice
	} else {
	    const common = initHeaderDevice()
		
	    rd = JSON.stringify(common)
	}
	console.log(baseUrl,'rd')
	return new Promise((resolve,reject)=>{
		const token=uni.getStorageSync('token')
		const tokenFinance=uni.getStorageSync('tokenFinance')
		uni.request({
		    url: baseUrl+url,
			method:type,
		    data: {
		        ...data
		    },
			header: {
				Accept: "application/json;charset=UTF-8",
				"Content-Type": "application/json;charset=UTF-8",
				device:rd,
				Authorization:tokenFinance,//贷超
				Author:token,//多典花
			},
		    success: (res) => { 
				
		        const {data, statusCode} = res;
		        if (statusCode === 200) {
		        	if (data.success==true) {
		        		return resolve(data)
		            } else {  
		        	    if(data.code=='403'){
							console.log(res,'402');   
                            uni.showToast({
                                title: data.message,
                                icon: "none",
                                duration: 2000
                                                
                            })
                            uni.removeStorageSync('token')	
							// this.Go('uni:/pages/login/login')
                        }else{
								uni.showToast({
								    title: data.message,
								    icon: "none",
								    duration: 2000
								                    
								})
								return false
                        }
		        					
					}
				} else {
		        		console.log(res.errMsg,'!200');
		        	return reject({
		        		data,
		        		message: res.errMsg
		        	})
		        }
		    },
			fail:(err)=> {
				console.log(err,954)
				// return uni.showToast({
				// 	icon:'loading',
				// 	title:'请求失败'
				// })
				reject(err)
			}
		});
	
	})

}
export default request;