Last active
August 3, 2021 13:56
-
-
Save Houserqu/63aaa76dcdf708b2f63f725e16cc778a to your computer and use it in GitHub Desktop.
http请求封装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios' | |
| import { MessagePlugin, DialogPlugin } from '@tencent/tdesign-vue' // 换成需要的提示工具库 | |
| const instance = axios.create({ | |
| timeout: 1000, | |
| }) | |
| instance.interceptors.request.use(config => config) | |
| instance.interceptors.response.use(response => { | |
| const { | |
| data, | |
| config: { | |
| errToast = true, // 是否提示错误 | |
| errMsg = null, // 错误提示信息(用于覆盖接口返回的提示信息) | |
| successToast = false, // 是否提示成功 | |
| successMsg = null, // 成功提示信息(用于覆盖接口返回的提示信息) | |
| meta = false, // 是否返回完整响应体(用于需要自己判断成功失败的情况) | |
| }, | |
| } = response | |
| // 返回完整响应体 | |
| if (meta) { | |
| return data | |
| } | |
| // 业务逻辑错误 | |
| if (data.ret !== 0) { | |
| if (errToast) { | |
| DialogPlugin.alert({ | |
| header: '操作失败', | |
| body: errMsg || data.msg || '系统繁忙', | |
| confirmBtn: null, | |
| }) | |
| } | |
| return null | |
| } | |
| if (successToast) { | |
| MessagePlugin.success(successMsg || data.msg || '操作成功') | |
| } | |
| // 始终返回真值,用于调用者判断请求是否成功 | |
| return data.data || true | |
| }, err => { | |
| DialogPlugin.alert({ | |
| header: '操作失败', | |
| body: err.message, | |
| confirmBtn: null, | |
| }) | |
| return null // 不抛出异常,调用者通过返回值判断是否成功 | |
| }) | |
| export const post = instance.post | |
| export const get = instance.get |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let Http = {}; | |
| /** | |
| * fetch.get请求封装 | |
| */ | |
| Http.get = (url,params='')=>{ | |
| if (params) { | |
| let paramsArray = []; | |
| //encodeURIComponent | |
| Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key])) | |
| if (url.search(/\?/) === -1) { | |
| url += '?' + paramsArray.join('&'); | |
| } else { | |
| url += '&' + paramsArray.join('&'); | |
| } | |
| } | |
| return new Promise(function (resolve, reject) { | |
| fetch(API_DOMAIN + url, { | |
| method: 'get', | |
| }).then((response) => response.json()) | |
| .then((responseData) => { | |
| let checkCodeResult = checkCode(responseData.statusCode); | |
| if(checkCodeResult.code == 200){ | |
| resolve(responseData); | |
| }else{ | |
| //触发store action 弹出提示框 | |
| store.dispatch(doTopTips('warn',checkCodeResult.message)); | |
| } | |
| }) | |
| .catch((err) => { | |
| reject(err); | |
| }); | |
| }); | |
| }; | |
| /** | |
| * fetch.post请求封装 | |
| */ | |
| Http.post = (url,formData)=>{ | |
| return new Promise(function (resolve, reject) { | |
| fetch(API_DOMAIN + url, { | |
| method: 'post', | |
| body:formData, | |
| }).then((response) => response.json()) | |
| .then((responseData) => { | |
| let checkCodeResult = checkCode(responseData.statusCode); | |
| if(checkCodeResult.code == 200){ | |
| resolve(responseData); | |
| }else{ | |
| //触发store action 弹出提示框 | |
| store.dispatch(doTopTips('warn',checkCodeResult.message)); | |
| } | |
| }) | |
| .catch((err) => { | |
| reject(err); | |
| }); | |
| }); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const config = require('../config') | |
| module.exports = function request(url, userRequestOpt = {}, userFuncOpt) { | |
| const defaultRequestOpt = { | |
| method: 'POST', | |
| header: { | |
| 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | |
| 'X-Requested-With': 'XMLHttpRequest', | |
| }, | |
| } | |
| const defaultFuncOpt = { | |
| errToast: true, // 是否显示错误提示 | |
| errMsg: null, // 自定义错误消息 | |
| meteResult: false, // 返回原始请求内容 | |
| } | |
| const funcOpt = { | |
| ...defaultFuncOpt, | |
| userFuncOpt | |
| } | |
| return new Promise((resolve, reject) => { | |
| wx.request({ | |
| url: config.API_DOMAIN + url, | |
| ...defaultRequestOpt, | |
| ...userRequestOpt, | |
| success: (data) => { | |
| // 请求级检查 | |
| if (data.statusCode == 200) { | |
| // 返回请求内容 | |
| const result = data.data; | |
| if (result.statusCode == 200) { | |
| // 成功 | |
| resolve(funcOpt.meteResult ? result : result.data); | |
| } else { | |
| // 失败 | |
| if (funcOpt.errToast) { | |
| wx.showToast({ | |
| icon: 'none', | |
| title: funcOpt.errMsg || data.message | |
| }) | |
| } | |
| reject(new Error(result.message)) | |
| } | |
| } else { | |
| reject(new Error(data.errMsg)) | |
| } | |
| }, | |
| fail: function (err) { | |
| reject(err) | |
| } | |
| }) | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment