This commit is contained in:
王子琦
2026-01-13 15:19:49 +08:00
parent f58e05d962
commit 6af59d985f
16 changed files with 339 additions and 33 deletions

View File

@@ -6,8 +6,9 @@
<div>
<el-button type="text" @click="$router.push('/orders')">我的订单</el-button>
<el-button type="text" @click="$router.push('/profile')">个人中心</el-button>
<el-button type="text" @click="$router.push('/admin')">后台管理</el-button>
<el-button type="primary" @click="$router.push('/login')">登录</el-button>
<el-button v-if="isAdmin" type="text" @click="$router.push('/admin')">后台管理</el-button>
<el-button v-if="!isLoggedIn" type="primary" @click="$router.push('/login')">登录</el-button>
<el-button v-else type="danger" @click="handleLogout">注销</el-button>
</div>
</el-header>
<el-main>
@@ -41,6 +42,7 @@
<script>
import { listProducts, listCategories } from '../api/product';
import { logout } from '../api/auth';
export default {
data() {
@@ -50,14 +52,36 @@ export default {
categories: [],
categoryId: null,
keyword: '',
placeholder: 'https://via.placeholder.com/300x200?text=Flower'
placeholder: 'https://via.placeholder.com/300x200?text=Flower',
isAdmin: false,
isLoggedIn: false
};
},
created() {
const user = JSON.parse(localStorage.getItem('user') || 'null');
this.isAdmin = user && user.role === 'ADMIN';
this.isLoggedIn = Boolean(localStorage.getItem('token'));
this.loadCategories();
this.loadProducts();
},
methods: {
handleLogout() {
const token = localStorage.getItem('token');
if (token) {
this.$confirm('确认退出登录?', '提示', { type: 'warning' })
.then(() => {
logout().finally(() => {
this.$message.success('已退出登录');
localStorage.removeItem('token');
localStorage.removeItem('user');
this.isLoggedIn = false;
this.isAdmin = false;
this.$router.push('/login');
});
})
.catch(() => {});
}
},
loadCategories() {
listCategories().then((res) => {
this.categories = res.data.data || [];