unipp 封装request请求

在main.js中
写一个全局的url
Vue.prototype.globalurl="http://www.....";

封装方法

 1 Vue.prototype.globalajax=function(url,data,way,fun){
 2         var token = uni.getStorageSync('token');
 3         uni.request({
 4             url:this.globalurl+url,
 5             data:data,
 6             header: {
 7                 'Accept': 'application/json',
 8                 'content-type': 'application/json',
 9                 'token':  token,
10             },
11             method:way,
12             success: (res) => {
13                 return fun(res);
14             }
15         });
16 };

封装一个错误提示框

Vue.prototype.showError=function(msg, callback){
        uni.hideLoading()
        uni.showModal({
          title: '友情提示',
          content: msg,
          showCancel: false,
          success: function (res) {
            callback && callback();
          },
        });
};

vue页面引用

// 获取店铺信息
            getShopInfo(){
                var a = this
                a.globalajax('shop_info',{
                       shop_id:a.shop_id
                    },'GET',function(res) {
                        console.log(res);
                        if(res.data.code==200){
                            a.shop_info = res.data.data.shop
                        }else{
                            a.showError(res.data.error);
                        }
                    }
                );
            },

 

posted @ 2021-01-05 10:53  飘向北方0539  阅读(109)  评论(0)    收藏  举报