import { app } from '@/config'
import Taro from '@tarojs/taro'

export interface RequestParams {
    url: string
    method: 'GET' | 'POST' | 'PUT' | 'DELETE'
    header?: object
    data?: string | object
}

const request = (request: RequestParams): Promise<any> => {
    // Taro.showLoading({
    //   title: '加载中',
    // })
    return new Promise((resolve, reject) => {
        Taro.request({
            url: `${app.API_URL()}${request.url}`,
            method: request.method,
            timeout: 5000,
            dataType: 'json',
            header: request.header || {},
            data: request.data || {},
            success: (res) => {
                resolve(res.data)
                // Taro.hideLoading()
            },
            fail: (err) => {
                reject(err)
                Taro.hideLoading()
            },
        })
    })
}

export default request
