import axios from 'axios'

const baseUrl = 'https://oc-testapi.finezb.com';
// const baseUrl = 'http://192.168.3.156:8862';
// const header = {
//     "Token":localStorage.getItem('user_token'),
//     "Encrypt":localStorage.getItem('room_id'),
// }

// if(localStorage.getItem('user_token') == undefined){
    
// }
const request = (url, method, data) => {
    var header = {
        "Token":uni.getStorageSync('user_token'),
        "Encrypt":uni.getStorageSync('room_id'),
    }
    return new Promise((resolve, reject) => {
       if (method === 'GET') {
            axios({
                    headers:header,
                    method: method,
                    url: baseUrl + url,
                    params: data
                }).then(res => {
                    if (res.data.code === 200) {
                        resolve(res);
                    } else {
                        resolve(res);
                        
                    }
                })
                .catch(err => {
                    reject(err); //失败
                });
        } else if (method === 'POST') {
            axios({
                    headers:header,
                    method: method,
                    url: baseUrl + url,
                    data:data
                }).then(res => {
                    if (res.data.code === 200) {
                        resolve(res);
                    } else {
                        resolve(res);
                    }
                })
                .catch(err => {
                    reject(err); //失败
                });
        }
    })
}

export default request