const baseUrl = process.env.VUE_APP_BASE_URL;
function parseUrl(url) {
    let _baseUrl = baseUrl;
    if (url.startsWith('http')) {
        return url;
    }
    if (!_baseUrl.endsWith('/')) {
        _baseUrl += '/';
    }
    if (url.startsWith('/')) {
        url = url.slice(1);
    }
    return _baseUrl + url;
}
const http = {
    parseUrl: parseUrl,
    setToken(token) {
        uni.setStorageSync('token', token);
        this.token = token;
    },
    clearToken() {
        this.token = '';
        uni.removeStorageSync('token');
        console.log('clear token');
    },
    getToken() {
        if (!this.token) {
            try {
                this.token = uni.getStorageSync('token');
            } catch (e) {}
        }
        return this.token;
    },
    get(url, params, options) {
        options = options || {};
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'GET',
                url: parseUrl(url),
                data: params,
                dataType: 'json',
                header: {
                    Accept: 'application/json',
                    Authorization: this.getToken() ? 'Bearer ' + this.getToken() : 'Bearer ',
                    ...(options.header || {})
                },
                success(res) {
                    if (res && res.statusCode === 200) {
                        resolve(res.data);
                    } else {
                        reject(res.data || res);
                    }
                },
                fail(err) {
                    reject(err.data || err);
                }
            });
        });
    },
    post(url, data, options, backHeader = false) {
        console.log('post');
        options = options || {};
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'post',
                url: parseUrl(url),
                data: data,
                dataType: 'json',
                header: {
                    Accept: 'application/json',
                    'content-type': 'application/x-www-form-urlencoded',
                    Authorization: this.getToken() ? 'Bearer ' + this.getToken() : '',
                    ...(options.header || {})
                },
                success(res) {
                    if (res && res.statusCode === 200) {
                        resolve(backHeader ? res : res.data);
                    } else {
                        reject(res.data || res);
                    }
                },
                fail(err) {
                    reject(err.data || err);
                }
            });
        });
    },
    postJson(url, data, options, backHeader = false) {
        options = options || {};
        return new Promise((resolve, reject) => {
            uni.request({
                method: 'post',
                url: parseUrl(url),
                data: data,
                dataType: 'json',
                header: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json',
                    Authorization: this.getToken() ? 'Bearer ' + this.getToken() : '',
                    ...(options.header || {})
                },
                success(res) {
                    if (res && res.statusCode === 200) {
                        resolve(backHeader ? res : res.data);
                    } else {
                        reject(res.data || res);
                    }
                },
                fail(err) {
                    reject(err.data || err);
                }
            });
        });
    },
    uploadFile(filePath, options) {
        options = options || {};
        return new Promise((resolve, reject) => {
            uni.uploadFile({
                url: baseUrl + '/upload/file',
                filePath: filePath,
                name: 'file',
                header: {
                    Accept: 'application/json',
                    'content-type': 'application/x-www-form-urlencoded',
                    Authorization: this.getToken() ? 'Bearer ' + this.getToken() : '',
                    ...(options.header || {})
                },
                formData: options.formData || {},
                success(res) {
                    if (res && res.statusCode === 200) {
                        try {
                            resolve(res.data);
                        } catch (e) {
                            reject(e);
                        }
                    } else {
                        reject(res);
                    }
                },
                fail(err) {
                    reject(err);
                }
            });
        });
    }
};
export default {
    http: http,
    install(_Vue) {
        _Vue.prototype.$baseUrl = baseUrl;
        _Vue.prototype.$http = http;
        _Vue.prototype.$request = options => {
            options = options || {};
            options.url = parseUrl(options.url);
            return new Promise((resolve, reject) => {
                options.success = res => {
                    resolve(res);
                };
                options.success = err => {
                    reject(err);
                };
            });
        };
    }
};
