return new Promise((resolve, reject) => { let _config = null options.complete = (response) => { let statusCode = response.statusCode response.config = _config if (process.env.NODE_ENV === 'development') { if (statusCode === 200) { // console.log("【" + _config.requestId + "】 结果:" + JSON.stringify(response.data)) } } if (this.interceptor.response) { let newResponse = this.interceptor.response(response) if (newResponse) { response = newResponse } } if (statusCode === 200) { //成功 var rs = response.data; if (rs.code === 0) { // 请求成功后返回 resolve(response.data); } else if (rs.code == 401) { uni.navigateTo({ url: '../login/login' }) } else { uni.showToast({ title: rs.msg, icon: 'none', duration: 2000 }); } } else { uni.showToast({ title: "接口执行异常", icon: 'none', duration: 2000 }); reject(response) } } _config = Object.assign({}, this.config, options) _config.requestId = new Date().getTime() if (this.interceptor.request) { this.interceptor.request(_config) } if (process.env.NODE_ENV === 'development') { // console.log("【" + _config.requestId + "】 地址:" + _config.url) if (_config.data) { // console.log("【" + _config.requestId + "】 参数:" + JSON.stringify(_config.data)) } } uni.request(_config); }); }, get(url, data, options) { if (!options) { options = {} } options.url = url options.data = data options.method = 'GET' return this.request(options) }, post(url, data, options) { if (!options) { options = {} } options.url = url options.data = data options.method = 'POST' return this.request(options) }, put(url, data, options) { if (!options) { options = {} } options.url = url options.data = data options.method = 'PUT' return this.request(options) }, delete(url, data, options) { if (!options) { options = {} } options.url = url options.data = data options.method = 'DELETE' return this.request(options) } }
时间: 2024-04-28 18:18:46 浏览: 10
这段代码是一个封装了uni-app中请求方法的对象,具有get、post、put、delete四种请求方法。其中,请求方法通过调用this.request方法实现。在请求方法中,请求的url、数据和请求方法通过options参数传递。在请求发送前,会执行拦截器中的request方法,可以进行一些请求前的处理。在请求响应后,会执行拦截器中的response方法,可以进行一些请求响应后的处理。如果请求成功,会判断返回数据中的状态码,如果状态码为200且返回数据中的code为0,则会resolve返回数据。如果状态码为200但返回数据中的code不为0,则会弹出一个toast提示错误信息。如果状态码不为200,则会reject返回响应。如果返回数据中的code为401,则会跳转到登录页面。
相关问题
return new promise(resolve =>
这句话是JS代码,意思是返回一个新的Promise对象,并且将resolve方法作为参数传入Promise的构造函数中。resolve方法通常用来将Promise的状态从“pending”变为“fulfilled”,即表示操作成功完成。
let promise = new Promise(resolve => { });
这是一个JavaScript语句,意思是创建一个Promise对象,并将其赋值给一个名为promise的变量。Promise是一种异步编程的解决方案,用于处理异步操作,并可以优雅地处理异步操作的结果或错误。Promise对象有三种状态:pending(等待状态)、fulfilled(已完成状态)和rejected(已拒绝状态)。在这里,我们通过Promise的构造函数,传入一个函数作为参数,这个函数接收一个resolve函数作为参数,表示异步操作执行完成后的处理。在函数体中,我们可以编写异步操作的代码,并在异步操作完成后,调用resolve函数,传入异步操作的结果作为参数,将Promise对象的状态从pending变为fulfilled。