js
复制代码
import axios from 'axios';
const instace = axios.create({
baseURL: 'https://internal.takehr.cn', //接口地址
timeout: 5000, //超时时间
});
//配置请求拦截器,在请求之前的数据处理,比如在请求头添加token,所有的请求都会经过拦截器
instace.interceptors.request.use(
//config:该参数表示当前请求的配置对象
config => {
console.log('config', config);
return config;
},
err => {
return Promise.reject(err); //将错误消息挂到promise的失败函数上
},
);
//配置响应拦截器
// 响应拦截器:在请求响应之后对数据处理,比如:登录失败、请求数据失败的处理
instace.interceptors.response.use(
response => {
return response; //这里的response就是请求成功后的res , response.data即是请求成功后回调函数内的参数res.data
},
err => {
return Promise.reject(err); //将错误消息挂到promise的失败函数上
},
);
//封装请求的api
const callapi = (method = 'GET', url, data = {}, customHeaders = {}) => {
return instace({
method,
url,
params: method === 'GET' ? data : {},
data: method === 'POST' ? data : {},
...customHeaders,
});
};
//封装GET,POST请求函数
export const getapi = (url, data, customHeaders) => callapi('GET', url, data, customHeaders);
export const postapi = (url, data, customHeaders) => callapi('POST', url, data, customHeaders);
js
复制代码
import {getapi, postapi} from './axios';
export const login = data => postapi('/api/v1.login/signin', data); // 用户登录
export const register = data => postapi('/api/v1.login/signup', data); // 用户注册
export const SendVerificationCode = data => postapi('/api/v1.login/vertiy', data); // 使用邮箱验证码登录
export const validateResetPasswordEmail = data => postapi('/api/v1.login/findaccount', data); // 忘记密码邮箱校验
export const forgetPassword = data => postapi('/api/v1.login/resetPwd', data); // 忘记密码
export const photo = (data, customHeaders) => postapi('/api/upload/image', data, customHeaders);