From 17f6b49e5d99ae3fb4d581fd6909ec7dd1e835f7 Mon Sep 17 00:00:00 2001 From: Agent Date: Tue, 24 Mar 2026 00:36:40 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=81=87=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=92=8C=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages.json | 5 ++- src/pages/index/index.vue | 85 ++++++++++++++++++++++++++++++++++----- src/pages/login/index.vue | 37 +++++++++++++++++ src/utils/auth.js | 25 ++++++++++++ 4 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 src/utils/auth.js diff --git a/src/pages.json b/src/pages.json index 42d232e..4cd62dc 100644 --- a/src/pages.json +++ b/src/pages.json @@ -56,5 +56,8 @@ "text": "订单" } ] - } + }, + "customerHidePages": [ + "pages/order/create" + ] } diff --git a/src/pages/index/index.vue b/src/pages/index/index.vue index 007e2a0..0dd06cc 100644 --- a/src/pages/index/index.vue +++ b/src/pages/index/index.vue @@ -5,11 +5,13 @@ {{ userInfo.username || '用户' }} - {{ userInfo.role || '销售员' }} + + {{ roleText }} + - - + + 📦 商品管理 @@ -28,8 +30,20 @@ - - + + + + 📦 + 商品浏览 + + + 📋 + 我的订单 + + + + + 今日概览 @@ -47,6 +61,16 @@ + + + 温馨提示 + + • 您可以浏览商品 + • 您可以查看半年内的订单 + • 如需下单,请联系销售人员 + + + @@ -56,11 +80,14 @@ import authApi from '@/api/auth' import orderApi from '@/api/order' import productApi from '@/api/product' +import { getRole, isCustomer as checkIsCustomer } from '@/utils/auth' export default { data() { return { userInfo: {}, + role: 'admin', + isCustomer: false, stats: { orderCount: 0, actualAmount: 0, @@ -68,12 +95,31 @@ export default { } } }, + computed: { + roleText() { + return this.isCustomer ? '顾客' : '销售员' + } + }, onLoad() { + this.role = getRole() + this.isCustomer = checkIsCustomer() this.loadUserInfo() - this.loadStats() + if (!this.isCustomer) { + this.loadStats() + } }, methods: { async loadUserInfo() { + // 假登录时从本地存储获取 + const localRole = uni.getStorageSync('role') + if (localRole) { + this.userInfo = { + username: localRole === 'admin' ? '管理员' : '顾客', + role: localRole + } + return + } + try { const userInfo = await authApi.getCurrentUser() this.userInfo = userInfo @@ -83,13 +129,11 @@ export default { }, async loadStats() { try { - // 获取今日订单统计 const today = new Date().toISOString().split('T')[0] const stats = await orderApi.getStatistics({ startDate: today }) this.stats.orderCount = stats.orderCount || 0 this.stats.actualAmount = stats.actualAmount || 0 - // 获取库存预警 const alerts = await productApi.getStockAlerts() this.stats.stockAlerts = alerts ? alerts.length : 0 } catch (e) { @@ -109,6 +153,8 @@ export default { console.error(e) } uni.removeStorageSync('token') + uni.removeStorageSync('userId') + uni.removeStorageSync('role') uni.reLaunch({ url: '/pages/login/index' }) } } @@ -144,12 +190,20 @@ export default { .role-tag { margin-left: 20rpx; padding: 8rpx 16rpx; - background: #e6f7ff; - color: #1890ff; border-radius: 8rpx; font-size: 24rpx; } +.role-tag.admin { + background: #e6f7ff; + color: #1890ff; +} + +.role-tag.customer { + background: #fff7e6; + color: #fa8c16; +} + .menu-grid { display: flex; flex-wrap: wrap; @@ -210,6 +264,17 @@ export default { margin-top: 10rpx; } +.tips { + display: flex; + flex-direction: column; +} + +.tip-text { + font-size: 26rpx; + color: #666; + line-height: 40rpx; +} + .logout-btn { background: #fff; color: #ff4d4f; diff --git a/src/pages/login/index.vue b/src/pages/login/index.vue index 89d4a7d..528a122 100644 --- a/src/pages/login/index.vue +++ b/src/pages/login/index.vue @@ -95,11 +95,48 @@ export default { return } + // 假登录(演示用) + if (this.username === 'admin' && this.password === 'admin') { + const mockData = { + token: 'mock-token-admin', + userId: 'admin-001', + role: 'admin' + } + uni.setStorageSync('token', mockData.token) + uni.setStorageSync('userId', mockData.userId) + uni.setStorageSync('role', mockData.role) + + uni.showToast({ title: '登录成功', icon: 'success' }) + setTimeout(() => { + uni.reLaunch({ url: '/pages/index/index' }) + }, 1000) + return + } + + // 顾客登录 + if (this.username === 'customer' && this.password === 'customer') { + const mockData = { + token: 'mock-token-customer', + userId: 'customer-001', + role: 'customer' + } + uni.setStorageSync('token', mockData.token) + uni.setStorageSync('userId', mockData.userId) + uni.setStorageSync('role', mockData.role) + + uni.showToast({ title: '登录成功', icon: 'success' }) + setTimeout(() => { + uni.reLaunch({ url: '/pages/index/index' }) + }, 1000) + return + } + try { const data = await authApi.passwordLogin(this.username, this.password) uni.setStorageSync('token', data.token) uni.setStorageSync('refreshToken', data.refreshToken) uni.setStorageSync('userId', data.userId) + uni.setStorageSync('role', data.role || 'customer') uni.showToast({ title: '登录成功', icon: 'success' }) setTimeout(() => { diff --git a/src/utils/auth.js b/src/utils/auth.js new file mode 100644 index 0000000..2b8f656 --- /dev/null +++ b/src/utils/auth.js @@ -0,0 +1,25 @@ +/** + * 权限判断工具 + */ + +export function getRole() { + return uni.getStorageSync('role') || 'guest' +} + +export function isAdmin() { + return getRole() === 'admin' +} + +export function isCustomer() { + return getRole() === 'customer' +} + +export function canCreateOrder() { + // 只有管理员/销售可以创建订单,顾客不可以 + return !isCustomer() +} + +export function canViewAllOrders() { + // 只有管理员/销售可以查看全部订单 + return !isCustomer() +}