zoukankan      html  css  js  c++  java
  • 在不单独拿出来封装的情况下全局使用$api

    所有一下均可在man.js中写入;

    const prePage = ()=>{
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    // #ifdef H5
    return prePage;
    // #endif
    return prePage.$vm;
    }

    const msg = (title, duration=1500, mask=false, icon='none')=>{
    //统一提示方便全局修改
    if(Boolean(title) === false){
    return;
    }
    uni.showToast({
    title,
    duration,
    mask,
    icon
    });
    }
    const getReqXml = (obj)=>{

    return new Promise(resolve=>{
    // var baseUrl = 'http://xqt.imaj.top:8080';
    var baseUrl = 'http://xiaoq.shgb.xyz:8080';
    var accessToken = uni.getStorageSync("token");
    if(obj.baseUrl){
    baseUrl = obj.baseUrl;
    }
    uni.request({
    url:baseUrl+obj.url,
    method: obj.type?obj.type:'GET',
    header:{
    'content-type': 'application/x-www-form-urlencoded',
    // "Access-Token":obj.reqJson.token,
    token:accessToken
    },
    dataType:'json',
    data:obj.reqJson,
    changeOrigin: true, // 允许跨域
    timeout:10000,
    success:(res)=>{
    if(res.data.code == 0){
    resolve(res.data);
    }else{
    msg(res.data.message);
    hideLoading();
    }
    },
    fail:(e)=>{
    console.log(e);
    msg(e.errMsg);
    hideLoading();
    },
    complete:()=>{
    console.log("complete");
    }
    })
    })
    }

    const getUserInfoXml = (obj)=>{
    return new Promise(resolve=>{
    // var baseUrl = 'http://xqt.imaj.top:8080';
    var baseUrl = 'http://xiaoq.shgb.xyz:8080';
    uni.request({
    url:baseUrl+obj.url,
    method: obj.type?obj.type:'GET',
    dataType:'json',
    data:obj.reqJson,
    changeOrigin: true, // 允许跨域
    timeout:10000,
    success:(res)=>{
    resolve(res);
    },
    fail:(e)=>{
    console.log(e);
    msg(e.errMsg);
    hideLoading();
    },
    complete:()=>{
    console.log("complete");
    }
    })
    })
    }

    挂载在vue上:

    Vue.prototype.$api = {prePage,msg,getReqXml,getUserInfoXml};

  • 相关阅读:
    UVA 10056 What is the Probability ?
    Reporting Services Report 带参数
    頁面刷新後,滾動條位置保持不變
    use this as the default and do not ask again
    JQuery Tab 滑动们导航菜单效果
    poj3256
    poj2060
    poj3280
    poj3261
    poj2135
  • 原文地址:https://www.cnblogs.com/robot666/p/12720162.html
Copyright © 2011-2022 走看看