济南院-智慧客服
This commit is contained in:
25
src/customerServiceHelpers/constants/http.js
Normal file
25
src/customerServiceHelpers/constants/http.js
Normal file
@ -0,0 +1,25 @@
|
||||
// HTTP状态码映射(可补充)
|
||||
export const HTTP_STATUS_CODE_MAP = {
|
||||
200: '服务请求成功!',
|
||||
201: '新建或修改数据成功!',
|
||||
202: '请求已进入后台排队中!',
|
||||
204: '删除数据成功!',
|
||||
400: '发出的请求有错误,未能执行相关操作!',
|
||||
401: '无权限访问,请重新登录!',
|
||||
403: '已得到授权,但访问被禁止!',
|
||||
404: '请求失败,未找到相关资源!',
|
||||
406: '请求格式不可得!',
|
||||
410: '请求的资源已被永久删除!',
|
||||
422: '请求过程中出现验证错误!',
|
||||
500: '服务器发生错误!',
|
||||
502: '网关错误!',
|
||||
503: '服务不可用,服务器暂时过载或正在维护!',
|
||||
504: '网关超时!',
|
||||
};
|
||||
|
||||
// 携带着请求体的请求方法
|
||||
export const WITH_REQUEST_BODY_METHODS = ['post', 'put', 'patch'];
|
||||
|
||||
// 请求后端接口失败时对应的几种code值(与后端协定)
|
||||
export const RESPONSE_FAILED_CODES = [400, 500];
|
||||
|
110
src/customerServiceHelpers/request.js
Normal file
110
src/customerServiceHelpers/request.js
Normal file
@ -0,0 +1,110 @@
|
||||
import axios from 'axios';
|
||||
import { WITH_REQUEST_BODY_METHODS } from './constants/http';
|
||||
|
||||
|
||||
function paddingBaseURL(config) {
|
||||
return Object.assign(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装通用的axios公共方法 (需要结合业务逻辑来进一步完善)
|
||||
* @param {Object} config - 非通用性(自定义)的一些axios请求配置
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const $axios = function (config) {
|
||||
|
||||
let defaultConfig = paddingBaseURL(config);
|
||||
|
||||
if (WITH_REQUEST_BODY_METHODS.includes(config.method)) {
|
||||
if (!config.data && config.params) {
|
||||
defaultConfig.data = config.params;
|
||||
defaultConfig.params = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
axios(defaultConfig)
|
||||
.then(result => {
|
||||
const { data } = result;
|
||||
resolve(data);
|
||||
})
|
||||
.catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export function $get(url, options) {
|
||||
return $axios({ ...options, url, method: 'get' });
|
||||
}
|
||||
|
||||
export function $post(url, options) {
|
||||
return $axios({ ...options, url, method: 'post' });
|
||||
}
|
||||
|
||||
export function $put(url, options) {
|
||||
return $axios({ ...options, url, method: 'put' });
|
||||
}
|
||||
|
||||
export function $patch(url, options) {
|
||||
return $axios({ ...options, url, method: 'patch' });
|
||||
}
|
||||
|
||||
export function $delete(url, options) {
|
||||
return $axios({ ...options, url, method: 'delete' });
|
||||
}
|
||||
|
||||
export function $head(url, options) {
|
||||
return $axios({ ...options, url, method: 'head' });
|
||||
}
|
||||
|
||||
export function $options(url, options) {
|
||||
return $axios({ ...options, url, method: 'options' });
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 下载专用请求
|
||||
* @param {downloadUrl} 接口路径 params:接口参数
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function $download(downloadUrl, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios(
|
||||
{
|
||||
method: "post",
|
||||
url: downloadUrl,
|
||||
data: params,
|
||||
responseType: "blob",
|
||||
}
|
||||
).then(result => {
|
||||
resolve(result);
|
||||
}).catch(err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 上传附件
|
||||
* @param {uploadUrl} 接口路径 params:接口参数
|
||||
* @params formData封装的数据
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function $upload(uploadUrl, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(uploadUrl, {
|
||||
method: 'POST',
|
||||
body: params.params,
|
||||
headers:{
|
||||
'Authorization':sessionStorage.getItem('token')?'Bearer '+sessionStorage.getItem('token'):null
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
return response.json()
|
||||
}).then(json => {
|
||||
resolve(json)
|
||||
}).catch(error => {
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user