• 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返回数据
    },
    })
    })
    }

  • 相关阅读:
    创建表练习
    【数据分析】NumPy
    conda环境中pytorch1.2.0版本安装包安装一直失败解决办法!!!
    0031【Edabit ★☆☆☆☆☆】【使用箭头函数】Using Arrow Functions
    Linux下LCD驱动的编写
    虚拟机构建单体项目及前后端分离项目
    Ketlle数据采集和预处理工具的认知和基本应用
    k8s--基础--22.10--storageclass--类型--Azure 磁盘
    无人机避障技术
    【前端实例代码】使用 HTML CSS 和 JavaScript 制作一个响应式搜索栏
  • 原文地址:https://blog.csdn.net/xiaosi1413/article/details/125172481