• uniapp拦截请求


    //把配置项单独处理
    export function service(options = {}) {
    // 获取租户id
    if(!uni.getStorageSync(‘tenantId’)){
    uni.request({
    url:/api/blade-user/app/getTeanantId?domain=${'http://localhost'},
    success: ((res) => {
    uni.setStorageSync(‘tenantId’,res.data.data.tenantId);
    })
    })
    }else{
    return new Promise((resolved, rejected) => {
    var header = ‘application/json’
    if (options.headerType == 1) {
    header = ‘application/x-www-form-urlencoded’
    // options.data = JSON.stringify(options.data)
    } else if (options.headerType == 2) {
    header = ‘multipart/form-data’
    }
    var httpHeaders = {
    ‘content-type’: header,
    ‘Authorization’: “Basic c3dvcmQ6c3dvcmRfc2VjcmV0”,
    }
    var userInfo = uni.getStorageSync(‘userInfo’)
    console.log(userInfo, ‘userInfo’)
    if (userInfo) {
    httpHeaders[‘Blade-Auth’] = userInfo.access_token
    }
    uni.request({
    // url: baseUrl + options.url,
    url: options.url,
    data: options.data,
    method: options.method,
    header: httpHeaders,
    tenantId: uni.getStorageSync(‘tenantId’),
    success: ((res) => {
    // data.msg

    				if (res.statusCode == 200) {
    					uni.hideLoading();
    					resolved(res);
    	
    				} else if (res.statusCode == 401) {
    					uni.showToast({
    						icon: 'none',
    						title: '请求未授权',
    						duration: 1800
    					});
    					rejected(res)
    					setTimeout(function() {
    						uni.redirectTo({
    							url: '/pages/login/login'
    						});
    					}, 2000)
    				} else {
    					uni.showToast({
    						icon: 'none',
    						title: res.data.msg,
    						duration: 1800
    					});
    					rejected(res)
    				}
    	
    			}),
    			fail: (res => {
    				console.log(res, '失败了')
    			})
    		})
    	});
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    }
    // 上传图片接口拦截
    export function serviceImg(data) {
    console.log(“照片”, data)
    return new Promise((resolve, reject) => {
    uni.uploadFile({
    // 微信小程序
    // url: baseUrl + data.url,
    // web
    url: data.url,
    filePath: data.tempFilePaths[0],
    name: ‘file’,
    header: {
    ‘Tenant-Id’: “000000”,
    ‘Blade-Auth’: ‘bearer’ + ’ ’ + uni.getStorageSync(‘token’),
    ‘Authorization’: ‘Basic c2FiZXI6c2FiZXJfc2VjcmV0’
    },
    success: (res) => {
    resolve(res) //如果请求成功,调用resolve返回数据
    },
    })
    })
    }

  • 相关阅读:
    关于远程协作可以分享的有很多,今天单说“定期面对面实现反熵”
    键值对RDD数据自定义分区_大数据培训
    java静态变量理解
    Windows系统远程桌面连接CentOS7
    PassUAC的简单实现(二)
    怎么预防鸡葡萄球菌病 防治鸡球菌病的特效药
    14.(地图数据篇)arcgis地图瓦片数据获取--java代码
    大数据Apache Druid(七):Druid数据的全量更新
    【网站项目】437物流管理系统
    BTCs打造区块链加营销广告数字流量新形式
  • 原文地址:https://blog.csdn.net/xiaosi1413/article/details/125172481