栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

uniCloud 统一拦截器使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

uniCloud 统一拦截器使用

官方文档:

uni.addInterceptor(STRING, OBJECT)

添加拦截器

STRING 参数说明

需要拦截的api名称,如:uni.addInterceptor('request', OBJECT) ,将拦截 uni.request()

OBJECT 参数说明

参数名类型必填默认值说明平台差异说明
invokeFunction拦截前触发
successFunction成功回调拦截
failFunction失败回调拦截
completeFunction完成回调拦截

需要在main.js里添加一下代码

// 统一的错误拦截
uniCloud.addInterceptor('callFunction',{
    success(res){
        const {result}=res
        if(result.errCode!==0){
            throw new Error(result.errMsg)
        }
    }
})

具体业务代码:(uniCloud配合uni-id用户注册逻辑)

async register() {
					try{
						uni.showLoading({
							title:"注册中"
						})
						console.log('111'+this.avatar.url)
						const forms = await this.$refs.forms.validate()
						const res=await uniCloud.callFunction({
							name:"index",
							data:{
								...forms,
								avatar: this.avatar.url
							}
						})
							uni.showToast({
								title:"注册成功",
								icon:'none'
							})
						console.log(res.result.code)
						console.log(res)
					}catch(e){ 
						uni.showToast({
							title: e.message || '校验失败',
							icon: 'none'
						})
					}finally{
						uni.hideLoading()
					}	
				}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295341.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号