uniapp使用Promise封装请求then方法添加回调

import baseConfig from '@/common/common.js'/** * Create by WH 2019/11/12 * apiRequest方法 * @data:請求參數 * @header:請求頭 * @Url: Url 請求的接口地址 * @errMsg: 錯誤提示信息 */export function apiRequest(arg = {}) { //拼装参数与官方一致 https://uniapp.dcloud.io/api/request/request?id=request let params = { url: '', data: {}, header: { "content-type": "application/x-www-form-urlencoded" }, method: 'get', dataType: '', responseType: '', sslVerify: true, errMsg: '系统繁忙,请稍后再试', ...arg, isInclude: true } // console.log('params', params); //請求提示,当请求方式为购物车商品数量加减时隐藏 if (!params.hideRequestToast) { uni.hideLoading(); uni.showLoading({ mask: true }); } // console.log('apiRequest',baseConfig.baseUrl.hejiamallH5 ) //把请求放到 Promise 里面 就实现了我们习惯的.then() return new Promise((resolve, reject) => { uni.request({ url: baseConfig.baseUrl.hejiamallH5 + params.url, data: params.data, header: params.header, method: params.method, dataType: params.dataType, success: (res) => { // console.log('request', res); if (res.statusCode == 200) { uni.hideLoading(); if (res.data.code === 0) { // uni.showToast({ // title: res.data.message, // icon: "none" // }) resolve(res.data); } if (res.data.code === 1) { // if(!params.hideRequestToast){ // uni.showToast({ // title: res.data.message, // icon:"none" // }) // } resolve(res.data); } } else { uni.hideLoading(); uni.hideToast(); uni.showToast({ title: params.errMsg, icon: "none" }) } }, fail: (err) => { uni.hideLoading(); uni.hideToast(); uni.showToast({ title: params.errMsg, icon: "none" }) reject(err); }, complete: () => {} }); })}


你可能感兴趣的:(uniapp使用Promise封装请求then方法添加回调)