import qs from 'qs'
import ajax from './ajax'
import plugin from '../js/plugin'
import store from '../store'

export default {

    /**
     * get请求
     * @param data
     * @param url
     * @param config
     * @returns {Promise<any>}
     */
    get(url, data = {}, config = {}) {
        return new Promise((resolve, reject) => {
            ajax.get(url, {
                // preview: Number(store.state.common.preview),
                ...data,
            }, {
                ...config,
            }).then(res => {
                if (res.code === '0' || config.pass === true) {
                    resolve(res)
                } else {
                    console.log(res);
                    plugin.showToast(res.message)
                    reject(res)
                }
            })
        })
    },

    /**
     * post请求
     * @param url
     * @param data
     * @param config
     * @returns {Promise<any>}
     */
    post(url, data = {query: ''}, config = {}) {
        if (data.query) {
            url = url + '?' + qs.stringify(data)
            delete data.query
        }

        return new Promise((resolve, reject) => {
            ajax.post(url, {
                // preview: Number(store.state.common.preview),
                ...data,
            }, {
                isJson: true,
                ...config,
            }).then(res => {
                if (res.code === '0' || config.pass === true) {
                    resolve(res)
                } else {
                    plugin.showToast(res.message)
                    reject(res)
                }
            })
        })
    },

    /**
     * post请求
     * @param url
     * @param data
     * @param config
     * @returns {Promise<any>}
     */
    put(url, data = {query: ''}, config = {}) {
        if (data.query) {
            url = url + '?' + qs.stringify(data)
            delete data.query
        }

        return new Promise((resolve, reject) => {
            ajax.put(url, {
                // preview: Number(store.state.common.preview),
                ...data,
            }, {
                isJson: true,
                ...config,
            }).then(res => {
                if (res.code === '0' || config.pass === true) {
                    resolve(res)
                } else {
                    plugin.showToast(res.message)
                    reject(res)
                }
            })
        })
    },

    /**
     * delete请求
     * @param data
     * @param url
     * @returns {Promise<any>}
     */
    delete(url, data = {query: ''}) {
        if (data.query) {
            url = url + '?' + qs.stringify(data)
            delete data.query
        }

        return new Promise((resolve, reject) => {
            ajax.delete(url, {
                ...data,
            }, {
                isJson: true,
            }).then(res => {
                if (res.code === '0' || config.pass === true) {
                    resolve(res)
                } else {
                    plugin.showToast(res.message)
                    reject(res)
                }
            })
        })
    },
}
