修复模板选择bug

This commit is contained in:
李志强 2026-03-19 15:48:06 +08:00
parent db585c5a32
commit 9ddcdf71b9
7 changed files with 95 additions and 21 deletions

View File

@ -1,2 +1,2 @@
# 生产环境接口
VITE_API_BASE_URL='https://backend.yunzer.cn'
VITE_API_BASE_URL='https://api.yunzer.cn'

View File

@ -37,9 +37,18 @@ authStore.checkAuth()
// 如果用户已登录,在应用启动时加载动态路由
if (authStore.isLoggedIn) {
loadAndAddDynamicRoutes().catch(err => {
console.error('应用启动时加载动态路由失败:', err);
});
loadAndAddDynamicRoutes()
.catch(err => {
console.error('应用启动时加载动态路由失败:', err);
})
.then(() => {
// 检查是否因为 token 无效而导致路由加载失败
const token = localStorage.getItem('token');
if (!token) {
authStore.clearToken();
window.location.href = '#/login';
}
});
}
app.mount('#app')

View File

@ -86,7 +86,7 @@ export async function loadAndAddDynamicRoutes() {
console.error('加载动态路由失败:', error);
dynamicRoutesAdded = true;
routesLoadingPromise = null;
return Promise.resolve();
throw error;
}
})();

View File

@ -171,6 +171,13 @@ export const useMenuStore = defineStore('menu', () => {
stack: err.stack
});
// 如果是 token 无效错误,不使用缓存,直接抛出
if (err.message === 'token无效' || err.response?.status === 401) {
clearCache();
menus.value = [];
throw err;
}
// 如果出错,尝试使用缓存数据
const cachedMenus = loadFromCache();
if (cachedMenus && cachedMenus.length > 0) {

View File

@ -40,16 +40,14 @@ service.interceptors.response.use(
if (error.response) {
switch (error.response.status) {
case 401:
// 处理未授权的错误 - 清除token并跳转登录页
console.error('未授权,请重新登录');
localStorage.removeItem('token');
// 避免循环导入,直接使用路由
localStorage.removeItem('userInfo');
if (window.location.hash !== '#/login') {
window.location.href = '#/login';
}
break;
return Promise.reject(new Error('token无效'));
case 404:
// 处理资源不存在的错误
console.error('请求的资源不存在');
break;
default:

View File

@ -74,14 +74,17 @@ import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh, Loading } from '@element-plus/icons-vue'
import { getThemeList, switchTheme } from '@/api/theme'
import { useAuthStore } from '@/stores/auth'
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL
const authStore = useAuthStore()
//
const loading = ref(false)
const switching = ref('')
const templateList = ref<any[]>([])
const currentTheme = ref('default')
const currentTid = ref<number>(0)
// URL
const getPreviewUrl = (path: string) => {
@ -94,7 +97,8 @@ const getPreviewUrl = (path: string) => {
const fetchTemplates = async () => {
loading.value = true
try {
const res = await getThemeList()
currentTid.value = (authStore.user as any)?.tid || 0
const res = await getThemeList({ tid: currentTid.value })
if (res.code === 200) {
templateList.value = res.data.list || []
currentTheme.value = res.data.currentTheme || 'default'
@ -110,9 +114,13 @@ const fetchTemplates = async () => {
//
const handleUse = async (item: any) => {
if (!currentTid.value) {
ElMessage.error('请先选择租户')
return
}
switching.value = item.key
try {
const res = await switchTheme({ theme_key: item.key })
const res = await switchTheme({ tid: currentTid.value, theme_key: item.key })
if (res.code === 200) {
currentTheme.value = item.key
ElMessage.success('切换成功')

View File

@ -39,11 +39,30 @@
<el-icon v-else class="logo-uploader-icon"><Plus /></el-icon>
</el-upload>
</el-form-item>
<el-form-item label="站点图标" prop="ico">
<el-upload
class="logo-uploader"
:show-file-list="false"
:auto-upload="false"
:on-change="handleIconChange"
>
<img
v-if="ico"
:src="API_BASE_URL + ico.replace(/^\//, '/')"
class="logo-image"
/>
<el-icon v-else class="logo-uploader-icon"><Plus /></el-icon>
</el-upload>
<div class="form-tip">请上传ico格式文件</div>
</el-form-item>
<el-form-item label="企业名称" prop="companyname">
<el-input v-model="companyname" placeholder="请输入企业名称" />
</el-form-item>
<el-form-item label="站点SEO描述" prop="description">
<el-input v-model="description" placeholder="请输入站点SEO描述" />
<el-form-item label="企业介绍" prop="companyintroduction">
<el-input v-model="companyintroduction" type="textarea" rows="5" placeholder="请输入企业介绍" />
</el-form-item>
<el-form-item label="站点简介" prop="description">
<el-input v-model="description" placeholder="请输入站点简介" />
</el-form-item>
<el-form-item label="版权信息" prop="copyright">
<el-input v-model="copyright" placeholder="如:© 2026 公司名称" />
@ -76,17 +95,21 @@ const normalFormRef = ref<FormInstance>();
const sitename = ref("");
const companyname = ref("");
const companyintroduction = ref("");
const logo = ref("");
const logow = ref("");
const description = ref("");
const copyright = ref("");
const icp = ref("");
const ico = ref("");
const formData = {
sitename,
companyname,
companyintroduction,
logo,
logow,
ico,
description,
copyright,
icp,
@ -102,8 +125,10 @@ const initNormalInfos = async () => {
if (res.code === 200 && res.data) {
const data = res.data;
sitename.value = data.sitename || "";
companyintroduction.value = data.companyintroduction || "";
logo.value = data.logo || "";
logow.value = data.logow || "";
ico.value = data.ico || "";
description.value = data.description || "";
copyright.value = data.copyright || "";
icp.value = data.icp || "";
@ -154,8 +179,10 @@ const handleSaveNormalInfos = async () => {
const data = {
tid: authStore.user.tid,
sitename: sitename.value,
companyintroduction: companyintroduction.value,
logo: logo.value,
logow: logow.value,
ico: ico.value,
description: description.value,
companyname: companyname.value,
copyright: copyright.value,
@ -171,11 +198,27 @@ const handleSaveNormalInfos = async () => {
});
};
const handleIconChange = (file: UploadFile) => {
const uploadFormData = new FormData();
uploadFormData.append("file", file.raw);
uploadFormData.append("cate", "site");
uploadFile(uploadFormData).then((uploadRes) => {
if ((uploadRes.code === 200 || uploadRes.code === 201) && uploadRes.data?.url) {
ico.value = uploadRes.data.url.replace(/\\/g, "/");
} else {
ElMessage.error(uploadRes.msg || "上传失败");
}
});
};
const resetnormalForm = () => {
sitename.value = "";
companyname.value = "";
companyintroduction.value = "";
logo.value = "";
logow.value = "";
ico.value = "";
description.value = "";
copyright.value = "";
icp.value = "";
@ -184,8 +227,10 @@ const resetnormalForm = () => {
defineExpose({
sitename,
companyname,
companyintroduction,
logo,
logow,
ico,
description,
copyright,
icp,
@ -198,10 +243,17 @@ onMounted(() => {
</script>
<style lang="less" scoped>
.darkbg{
.darkbg {
background-color: #efefef;
}
.form-tip{
margin-left: 20px;
color: #999;
font-size: 12px;
margin-top: 5px;
}
.logo-uploader {
:deep(.el-upload) {
border: 1px dashed #d9d9d9;