• 在不单独拿出来封装的情况下全局使用$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};

  • 相关阅读:
    JAVA 动态代理
    IDEA 配置阿里云Maven
    JAVA Spring5静态代理
    Spring5 常用注解
    react
    Angular
    微信小程序
    H5
    vue-ui
    jQuery
  • 原文地址:https://www.cnblogs.com/robot666/p/12720162.html
Copyright © 2020-2023  润新知