初始化

This commit is contained in:
2025-01-27 23:24:42 +08:00
parent c8a96306c4
commit 6ae8339299
421 changed files with 35687 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
import { useState } from '#imports';
import type { Config } from '~/types/global';
import type {Website} from "~/api/cms/website/model";
import type {Navigation} from "~/api/cms/navigation/model";
import type {User} from "~/api/system/user/model";
import type {Company} from "~/api/system/company/model";
import type {MyCart, ShopCart} from "~/api/shop/shopCart/model";
// 网站信息
export const useWebsite = () =>
useState<Website>('website', () => {
return {};
});
// 参数配置
export const useConfigInfo = () =>
useState<Config>('config', () => {
return {};
});
// 主导航
export const useMenu = () =>
useState<Navigation[]>('menu', () => {
return [];
});
// 副导航
export const useSubMenu = () =>
useState<Navigation[]>('subMenu', () => {
return [];
});
// 页面元素
export const useForm = () => useState<Navigation>('form', () => {
return {};
});
// 固钉
export const useProductAffix = () =>
useState<boolean>('affixTop', () => {
return false;
});
// 后台管理域名
export const useSysDomain = () => useState('useSysDomain', () => '');
// 登录凭证
export const useToken = () => useState('token', () => '');
// 用户信息
export const useUser = () =>
useState<User>('user', () => {
return {
userId: 0,
avatar: '',
phone: '',
balance: 0,
nickname: '',
gradeId: 0,
gradeName: '',
certification: false,
tenantId: 0,
tenantName: '',
};
});
// 企业信息
export const useCompany = () =>
useState<Company>('company', () => {
return {};
});
// 是否显示登录弹窗
export const useShowLogin = () => useState('showLogin',() => false)
// 购物车状态
export const useCart = () => useState<MyCart>('cart', () => {
return {
appName: '',
domain: '',
adminUrl: '',
menuId: undefined,
num: 1,
type: 1,
payType: 102,
payPrice: 0,
month: 1,
comments: '',
list: [],
totalPrice: 0
};
});

View File

@@ -0,0 +1,43 @@
import {getBaseUrl, isArray} from '~/utils/tool';
import {MODULES_API_URL, TENANT_ID} from "~/config";
type FetchType = typeof $fetch;
export type FetchOptions = Parameters<FetchType>[1];
export const useClientRequest = <T = unknown>(url: string, opts?: FetchOptions) => {
// 配置信息
const runtimeConfig = useRuntimeConfig();
// 请求接口
const baseUrl = ref('');
baseUrl.value = runtimeConfig.public.apiServer;
// 开发环境
if(process.env.NODE_ENV === 'development'){
baseUrl.value = `http://127.0.0.1:9002/api`
}
// 登录凭证token
const token = useToken();
const _token = localStorage.getItem('token');
if(_token){
token.value = _token
}
const defaultOptions: FetchOptions = {
baseURL: baseUrl.value,
onRequest({ options }) {
options.headers = (options.headers || {}) as { [key: string]: string };
if (token.value) {
options.headers.Authorization = token.value;
}
},
onResponse({ response }) {
if (+response.status === 0 && +response._data.code !== 0) {
ElMessage.error(response._data.message);
}
},
onResponseError({ response }) {
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
}
};
return $fetch<T>(runtimeConfig.public.apiServer + url, { ...defaultOptions, ...opts });
};

View File

@@ -0,0 +1,63 @@
/**
* 参考文档
* https://blog.csdn.net/m0_63281537/article/details/126699761
*/
import { useFetch } from '#app';
import type { UseFetchOptions } from '#app';
import {isArray} from '~/utils/tool';
import {isInteger} from "~/utils/common";
export const useServerRequest = <T>(url: string, opts?: UseFetchOptions<T, unknown>) => {
// 配置信息
const runtimeConfig = useRuntimeConfig();
// 请求接口
const baseUrl = ref('');
baseUrl.value = runtimeConfig.public.apiServer;
// 开发环境
if(process.env.NODE_ENV === 'development'){
// baseUrl.value = `http://127.0.0.1:9002/api`
}
// 登录凭证token
const token = useToken();
const _token = localStorage.getItem('token');
if(_token){
token.value = _token
}
const defaultOptions: UseFetchOptions<unknown> = {
baseURL: baseUrl.value,
onRequest({ options }) {
options.headers = (options.headers || {}) as { [key: string]: string };
// options.headers.tenantid = runtimeConfig.public.tenantId;
if (token.value) {
options.headers.Authorization = token.value;
}
// TODO 1 从二级域名解构租户ID
const domain = window.location.hostname;
const parts = domain.split('.');
const subDomain = parts[0];
if (isInteger(subDomain)) {
// options.headers.tenantid = `${subDomain}`;
}
// TODO 2 从绑定域名解构的租户ID
if(localStorage.getItem('TID_ADMIN')){
options.headers.TenantId = `5`;
}
},
onResponse({ response }) {
if (+response.status === 0 && +response._data.code !== 0) {
process.client && ElMessage.error(response._data.message);
}
if(+response.status === 500){
ElMessage.error('网络请求错误')
}
return response._data.data;
},
onResponseError({ response }) {
process.client &&
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
}
};
// console.log('请求接口:', baseUrl.value+url)
return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
};