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

  • 相关阅读:
    电力社区电力故障,潜在风险如何避免?
    关于 axios 是什么?以及怎么用?
    企业微信公众号怎么建立和运营?
    08_Linux基础-vim-tmux-字符编码
    【从零学习python 】74. UDP网络程序:端口问题与绑定信息详解
    文件判空工具类
    数据库复习带答案
    【机器学习】岭回归和LASSO回归详解以及相关计算实例-加利福尼亚的房价数据集、红酒数据集
    SystemVerilog
    治疗开发拖延症-任务拆分和执行
  • 原文地址:https://blog.csdn.net/xiaosi1413/article/details/125172481