import axios from 'axios'
import settle from 'axios/lib/core/settle'
let baseURL = 'http://apiv2.yksafety.net/ssp/v2'
const instance = axios.create({
    baseURL,
    timeout : 6000,
		responseType: 'json',
})

// 请求拦截
instance.interceptors.request.use(
    config => {
        // return config
				return new Promise( ( resolve , reject ) => {
				    // let settle = require('/axios/lib/core/settle')
				    uni.request({
				        method : config.type.toUpperCase(),
				        url : config.baseURL + config.path,
				        header : config.headers,
				        data : config.data,
				        dataType : config.dataType,
				        responseType : config.responseType,
				        complete : function complete( response ) {
				            response = {
				                data : response.data,
				                status : response.statusCode,
				                errMsg : response.errMsg,
				                header : response.header,
				                config : config
				            };
				        settle( resolve , reject , response );
				        },
				    });
				})
    },
    error => {
        return Promise.reject( error );
    }
);

// 响应拦截
instance.interceptors.response.use( res => {
	console.log('res',res)
    if ( res.status == 200 ){
        return res;
    } else {
        return Promise.reject( res.data.msg );
    }
});

// 自定义适配器 ， 适配uniapp语法
// axios.defaults.adapter = function( config ) {
// 	console.log('config',config)
    
// }

export default function request(url, params) {
  console.log('url', url);
  return instance(url, params);
}