mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-06 03:57:54 +08:00
feat(service): 完善request方法
This commit is contained in:
parent
014f728faf
commit
310dc43830
@ -36,7 +36,7 @@ export async function createDynamicRoutes(routes: AppRoute.Route[]) {
|
|||||||
const routeStore = useRouteStore();
|
const routeStore = useRouteStore();
|
||||||
routeStore.cacheRoutes = createCatheRoutes(flatRoutes);
|
routeStore.cacheRoutes = createCatheRoutes(flatRoutes);
|
||||||
// 生成路由,有redirect的不需要引入文件
|
// 生成路由,有redirect的不需要引入文件
|
||||||
const mapRoutes = flatRoutes.map((item) => {
|
const mapRoutes = flatRoutes.map((item: any) => {
|
||||||
if (!item.redirect) {
|
if (!item.redirect) {
|
||||||
// 动态加载对应页面
|
// 动态加载对应页面
|
||||||
item['component'] = modules[`../../views${item.path}/index.vue`];
|
item['component'] = modules[`../../views${item.path}/index.vue`];
|
||||||
|
@ -4,15 +4,19 @@ interface Ilogin {
|
|||||||
userName: string;
|
userName: string;
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
interface Itoken {
|
||||||
|
token: string;
|
||||||
|
refreshToken: string;
|
||||||
|
}
|
||||||
export function fetchLogin(params: Ilogin) {
|
export function fetchLogin(params: Ilogin) {
|
||||||
return mockRequest.post('/login', params);
|
return mockRequest.post<any>('/login', params);
|
||||||
}
|
}
|
||||||
export function fetchUpdateToken(params: string) {
|
export function fetchUpdateToken(params: string) {
|
||||||
return mockRequest.post('/updateToken', params);
|
return mockRequest.post<Itoken>('/updateToken', params);
|
||||||
}
|
}
|
||||||
export function fetchUserInfo() {
|
export function fetchUserInfo() {
|
||||||
return mockRequest.get('/getUserInfo');
|
return mockRequest.get('/getUserInfo');
|
||||||
}
|
}
|
||||||
export function fetchUserRoutes(params: string) {
|
export function fetchUserRoutes(params: string) {
|
||||||
return mockRequest.post('/getUserRoutes', params);
|
return mockRequest.post<any>('/getUserRoutes', params);
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ export function fetachPost(params: Itest) {
|
|||||||
}
|
}
|
||||||
/* delete方法测试 */
|
/* delete方法测试 */
|
||||||
export function fetachDelete() {
|
export function fetachDelete() {
|
||||||
return request.Delete('/deleteAPI');
|
return request.delete('/deleteAPI');
|
||||||
}
|
}
|
||||||
/* put方法测试 */
|
/* put方法测试 */
|
||||||
export function fetachPut(params: Itest) {
|
export function fetachPut(params: Itest) {
|
||||||
|
@ -24,10 +24,10 @@ export default class createAxiosInstance {
|
|||||||
// 基础配置
|
// 基础配置
|
||||||
axiosConfig: AxiosRequestConfig = {};
|
axiosConfig: AxiosRequestConfig = {};
|
||||||
|
|
||||||
constructor(axiosConfig: AxiosRequestConfig, backendConfig: Service.BackendResultConfig) {
|
constructor(axiosConfig: AxiosRequestConfig, backendConfig: Service.BackendResultConfig = DEFAULT_BACKEND_OPTIONS) {
|
||||||
// 设置了axios实例上的一些默认配置,新配置会覆盖默认配置
|
// 设置了axios实例上的一些默认配置,新配置会覆盖默认配置
|
||||||
this.instance = axios.create({ ...DEFAULT_AXIOS_OPTIONS, ...axiosConfig });
|
|
||||||
this.backendConfig = { ...DEFAULT_BACKEND_OPTIONS, ...backendConfig };
|
this.backendConfig = { ...DEFAULT_BACKEND_OPTIONS, ...backendConfig };
|
||||||
|
this.instance = axios.create({ ...DEFAULT_AXIOS_OPTIONS, ...axiosConfig });
|
||||||
this.setInterceptor();
|
this.setInterceptor();
|
||||||
}
|
}
|
||||||
// 设置类拦截器的函数
|
// 设置类拦截器的函数
|
||||||
@ -39,6 +39,7 @@ export default class createAxiosInstance {
|
|||||||
// 数据格式转换
|
// 数据格式转换
|
||||||
// handleConfig.headers.setContentType('application/json');
|
// handleConfig.headers.setContentType('application/json');
|
||||||
// const contentType = handleConfig.headers.get('Content-Type');
|
// const contentType = handleConfig.headers.get('Content-Type');
|
||||||
|
|
||||||
const contentType = 'application/json';
|
const contentType = 'application/json';
|
||||||
handleConfig.data = await transformRequestData(handleConfig.data, contentType);
|
handleConfig.data = await transformRequestData(handleConfig.data, contentType);
|
||||||
|
|
||||||
|
@ -1,94 +1,112 @@
|
|||||||
import type { AxiosRequestConfig } from 'axios';
|
import type { AxiosRequestConfig, AxiosInstance } from 'axios';
|
||||||
import createAxiosInstance from './instance';
|
import createAxiosInstance from './instance';
|
||||||
|
|
||||||
|
type RequestMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
||||||
|
interface RequestParam {
|
||||||
|
url: string;
|
||||||
|
method?: RequestMethod;
|
||||||
|
data?: any;
|
||||||
|
config?: AxiosRequestConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getRequestResponse(params: {
|
||||||
|
instance: AxiosInstance;
|
||||||
|
method: RequestMethod;
|
||||||
|
url: string;
|
||||||
|
data?: any;
|
||||||
|
config?: AxiosRequestConfig;
|
||||||
|
}) {
|
||||||
|
const { instance, method, url, data, config } = params;
|
||||||
|
|
||||||
|
let res: any;
|
||||||
|
if (method === 'get' || method === 'delete') {
|
||||||
|
res = await instance[method](url, config);
|
||||||
|
} else {
|
||||||
|
res = await instance[method](url, data, config);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description:
|
* @description:
|
||||||
* @param {AxiosRequestConfig} axiosConfig - axios配置
|
* @param {AxiosRequestConfig} axiosConfig - axios配置
|
||||||
* @param {Service} backendConfig - 后台字段配置
|
* @param {Service} backendConfig - 后台字段配置
|
||||||
* @return {*}
|
* @return {*}
|
||||||
*/
|
*/
|
||||||
export function createRequest(
|
export function createRequest(axiosConfig: AxiosRequestConfig, backendConfig?: Service.BackendResultConfig) {
|
||||||
axiosConfig: AxiosRequestConfig,
|
|
||||||
backendConfig?: Service.BackendResultConfig
|
|
||||||
) {
|
|
||||||
const axiosInstance = new createAxiosInstance(axiosConfig, backendConfig);
|
const axiosInstance = new createAxiosInstance(axiosConfig, backendConfig);
|
||||||
|
/**
|
||||||
|
* 异步promise请求
|
||||||
|
* @param param - 请求参数
|
||||||
|
* - url: 请求地址
|
||||||
|
* - method: 请求方法(默认get)
|
||||||
|
* - data: 请求的body的data
|
||||||
|
* - config: axios配置
|
||||||
|
*/
|
||||||
|
async function asyncRequest<T>(param: RequestParam): Promise<Service.RequestResult<T>> {
|
||||||
|
const { url, method = 'get', data, config } = param;
|
||||||
const { instance } = axiosInstance;
|
const { instance } = axiosInstance;
|
||||||
|
const res = (await getRequestResponse({
|
||||||
|
instance,
|
||||||
|
method,
|
||||||
|
url,
|
||||||
|
data,
|
||||||
|
config,
|
||||||
|
})) as Service.RequestResult<T>;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @description: 通用请求方法
|
* get请求
|
||||||
* @param {string} url- 请求地址
|
* @param url - 请求地址
|
||||||
* @param {RequestMethod} method - 请求方法
|
* @param config - axios配置
|
||||||
* @param {any} data - 请求数据体
|
|
||||||
* @param {AxiosRequestConfig} config - 请求配置
|
|
||||||
* @return {*}
|
|
||||||
*/
|
*/
|
||||||
type RequestMethod = 'get' | 'post' | 'put' | 'delete' | 'patch';
|
function get<T>(url: string, config?: AxiosRequestConfig) {
|
||||||
const request = async (
|
return asyncRequest<T>({ url, config: config });
|
||||||
url: string,
|
}
|
||||||
method: RequestMethod = 'get',
|
|
||||||
data: any,
|
|
||||||
config?: AxiosRequestConfig
|
|
||||||
) => {
|
|
||||||
return instance(url, { method, data, ...config });
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: get请求
|
* post请求
|
||||||
* @param {string} url - 请求地址
|
|
||||||
* @return {*}
|
|
||||||
*/
|
|
||||||
const get = async (url: string, config?: AxiosRequestConfig) => {
|
|
||||||
return instance.get(url, config);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description: post请求
|
|
||||||
* @param url - 请求地址
|
* @param url - 请求地址
|
||||||
* @param data - 请求的body的data
|
* @param data - 请求的body的data
|
||||||
* @param config - axios配置
|
* @param config - axios配置
|
||||||
*/
|
*/
|
||||||
const post = async (url: string, data?: any, config?: AxiosRequestConfig) => {
|
function post<T>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||||
return instance.post(url, data, config);
|
return asyncRequest<T>({ url, method: 'post', data, config: config });
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: delete请求
|
* delete请求
|
||||||
* @param url - 请求地址
|
* @param url - 请求地址
|
||||||
* @param config - axios配置
|
* @param config - axios配置
|
||||||
*/
|
*/
|
||||||
const Delete = async (url: string, config?: AxiosRequestConfig) => {
|
function handleDelete<T>(url: string, config?: AxiosRequestConfig) {
|
||||||
return instance.delete(url, config);
|
return asyncRequest<T>({ url, method: 'delete', config: config });
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: put请求
|
* put请求
|
||||||
* @param url - 请求地址
|
* @param url - 请求地址
|
||||||
* @param data - 请求的body的data
|
* @param data - 请求的body的data
|
||||||
* @param config - axios配置
|
* @param config - axios配置
|
||||||
*/
|
*/
|
||||||
const put = async (url: string, data?: any, config?: AxiosRequestConfig) => {
|
function put<T>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||||
return instance.put(url, data, config);
|
return asyncRequest<T>({ url, method: 'put', data, config: config });
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: patch请求
|
* patch请求
|
||||||
* @param url - 请求地址
|
* @param url - 请求地址
|
||||||
* @param data - 请求的body的data
|
* @param data - 请求的body的data
|
||||||
* @param config - axios配置
|
* @param config - axios配置
|
||||||
*/
|
*/
|
||||||
const patch = async (
|
function patch<T>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||||
url: string,
|
return asyncRequest<T>({ url, method: 'patch', data, config: config });
|
||||||
data?: any,
|
}
|
||||||
config?: AxiosRequestConfig
|
|
||||||
) => {
|
|
||||||
return instance.patch(url, data, config);
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
request,
|
|
||||||
get,
|
get,
|
||||||
post,
|
post,
|
||||||
Delete,
|
delete: handleDelete,
|
||||||
put,
|
put,
|
||||||
patch,
|
patch,
|
||||||
};
|
};
|
||||||
|
@ -255,7 +255,7 @@ onMounted(() => {
|
|||||||
});
|
});
|
||||||
async function getUserList() {
|
async function getUserList() {
|
||||||
startLoading();
|
startLoading();
|
||||||
await fetchUserList().then((res) => {
|
await fetchUserList().then((res: any) => {
|
||||||
listData.value = res.data;
|
listData.value = res.data;
|
||||||
endLoading();
|
endLoading();
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user