refactor(api): 重构 API调用并优化企业信息处理
- 移除各 API函数中的重复 headers 设置 - 在全局请求拦截器中添加企业 ID 头部信息 - 优化企业信息状态管理,确保信息不为空时才进行更新 -调整 API 调用方式,统一使用新格式
This commit is contained in:
@ -97,7 +97,7 @@ export const fetchGenderDistributionsList = (params: any) => {
|
||||
// 导出一个函数,用于获取产品列表
|
||||
export const fetchProductList = () => {
|
||||
// 使用Http.get方法,发送GET请求,获取产品列表
|
||||
return Http.get('/v1/products/list', {}, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.get('/v1/products/list');
|
||||
};
|
||||
|
||||
// 导出一个函数,用于获取成功案例列表
|
||||
@ -108,12 +108,12 @@ export const fetchSuccessCaseList = () => {
|
||||
|
||||
// 试用产品
|
||||
export const trialProduct = (id: number) => {
|
||||
return Http.post(`/v1/products/${id}/try`, {}, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.post(`/v1/products/${id}/try`);
|
||||
};
|
||||
|
||||
// 修改企业名称
|
||||
export const updateEnterpriseName = (data: any) => {
|
||||
return Http.patch(`/v1/enterprises/name`, data, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.patch(`/v1/enterprises/name`, data);
|
||||
};
|
||||
|
||||
// 发送修改手机号验证码
|
||||
@ -133,7 +133,7 @@ export const updateMyInfo = (data: any) => {
|
||||
|
||||
// 获取企业账号分页
|
||||
export const fetchSubAccountPage = (params: any) => {
|
||||
return Http.get(`/v1/enterprises/users`, params, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.get(`/v1/enterprises/users`, params);
|
||||
};
|
||||
|
||||
// 获取企业账号分页
|
||||
@ -143,10 +143,10 @@ export const fetchImageUploadFile = (params: any) => {
|
||||
|
||||
// 移除企业子账号
|
||||
export const removeEnterpriseAccount = (userId: number) => {
|
||||
return Http.delete(`/v1/enterprises/users/${userId}`, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.delete(`/v1/enterprises/users/${userId}`);
|
||||
};
|
||||
|
||||
// 获取企业邀请码
|
||||
export const getEnterpriseInviteCode = () => {
|
||||
return Http.get(`/v1/enterprises/invite-code`, {}, { headers: { 'enterprise-id': 1 } });
|
||||
return Http.get(`/v1/enterprises/invite-code`);
|
||||
};
|
||||
|
||||
@ -8,7 +8,10 @@
|
||||
|
||||
import axios from 'axios';
|
||||
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
||||
|
||||
import { useEnterpriseStore } from '@/stores/modules/enterprise';
|
||||
import pinia from '@/stores';
|
||||
const store = useEnterpriseStore(pinia);
|
||||
const enterprise = store.getEnterpriseInfo();
|
||||
//* 导出Request类,可以用来自定义传递配置来创建实例
|
||||
export class Request {
|
||||
//* axios 实例
|
||||
@ -27,6 +30,10 @@ export class Request {
|
||||
} else {
|
||||
config.headers!.satoken = '123';
|
||||
}
|
||||
|
||||
if (enterprise) {
|
||||
config.headers!['enterprise-id'] = enterprise.id;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(err: any) => {
|
||||
|
||||
@ -7,8 +7,12 @@ interface EnterpriseInfo {
|
||||
used_sub_account_count: number;
|
||||
}
|
||||
|
||||
interface EnterpriseState {
|
||||
enterpriseInfo: EnterpriseInfo | null;
|
||||
}
|
||||
|
||||
export const useEnterpriseStore = defineStore('enterprise', {
|
||||
state: () => ({
|
||||
state: (): EnterpriseState => ({
|
||||
// todo 暂时写死,登录功能完成后记得重置为null哦
|
||||
enterpriseInfo: {
|
||||
id: 1,
|
||||
@ -24,15 +28,21 @@ export const useEnterpriseStore = defineStore('enterprise', {
|
||||
this.enterpriseInfo = enterpriseInfo;
|
||||
},
|
||||
setEnterpriseName(name: string) {
|
||||
if (this.enterpriseInfo) {
|
||||
this.enterpriseInfo.name = name;
|
||||
}
|
||||
},
|
||||
incUsedUpdateNameCount() {
|
||||
if (this.enterpriseInfo) {
|
||||
this.enterpriseInfo.used_update_name_count++;
|
||||
}
|
||||
},
|
||||
incUsedSubAccountCount() {
|
||||
if (this.enterpriseInfo) {
|
||||
this.enterpriseInfo.used_sub_account_count++;
|
||||
}
|
||||
},
|
||||
getEnterpriseInfo(): EnterpriseInfo {
|
||||
getEnterpriseInfo(): EnterpriseInfo | null {
|
||||
return this.enterpriseInfo;
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user