uni封装请求

使用uniapp发送请求时,要用到自身的API,uni.request

可以将其封装在index文件里,后面使用的时候也更加方便

const BASE_URL = 'https://localhost:8081/api';

export const $req = (option) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL+option.url,
            method:option.method || 'GET',
            data: option.data || {},
            success: res =>{
                if(res.data.meta.status !== 200){
                    return uni.showToast({
                        title:'失败'
                    })
                }
                resolve(res)
            },
            fail:(error)=>{
                return uni.showToast({
                    title:'失败'
                })
                reject(error)
            }
        })
    })
}

具体配置看这里 

 

posted @ 2022-12-22 16:20  幻影之舞  阅读(41)  评论(0)    收藏  举报