小程序开发前接口封装(uni-app)

//全局请求路径
const baseUrl = "****";   //你的接口地址

//防止多次请求
let ajaxTimes = 0;

export const Http = (options)=>{

    ajaxTimes++;
 
   //请求未完成  加载动画
    uni.showLoading({
        title:"加载中...",
        mask:true
    })

    return new Promise((resolve,reject)=>{
        uni.request({
            url:baseUrl+options.url,
            method: options.method || "post",
            data:options.data || {},
            success:(res)=>{
                resolve(res);
            },
            fail:(error)=>{
                reject(error);
            },
            complete:()=>{

                ajaxTimes--;
                if(ajaxTimes === 0 ){
                    uni.hideLoading();
                }
                
            }
        })
    })
}
posted @ 2021-01-16 19:06  概世英雄  阅读(308)  评论(0)    收藏  举报