feat: 添加假登录和权限控制
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Agent
2026-03-24 00:36:40 +00:00
parent 7ca2e1d5a4
commit 17f6b49e5d
4 changed files with 141 additions and 11 deletions

View File

@@ -56,5 +56,8 @@
"text": "订单" "text": "订单"
} }
] ]
} },
"customerHidePages": [
"pages/order/create"
]
} }

View File

@@ -5,11 +5,13 @@
<view class="avatar"> <view class="avatar">
<text class="username">{{ userInfo.username || '用户' }}</text> <text class="username">{{ userInfo.username || '用户' }}</text>
</view> </view>
<view class="role-tag">{{ userInfo.role || '销售员' }}</view> <view class="role-tag" :class="isCustomer ? 'customer' : 'admin'">
{{ roleText }}
</view>
</view> </view>
<!-- 功能菜单 --> <!-- 功能菜单 - 管理员/销售 -->
<view class="menu-grid"> <view class="menu-grid" v-if="!isCustomer">
<view class="menu-item" @click="goTo('/pages/product/list')"> <view class="menu-item" @click="goTo('/pages/product/list')">
<text class="menu-icon">📦</text> <text class="menu-icon">📦</text>
<text class="menu-text">商品管理</text> <text class="menu-text">商品管理</text>
@@ -28,8 +30,20 @@
</view> </view>
</view> </view>
<!-- 快捷操作 --> <!-- 功能菜单 - 顾客 -->
<view class="section"> <view class="menu-grid" v-else>
<view class="menu-item" @click="goTo('/pages/product/list')">
<text class="menu-icon">📦</text>
<text class="menu-text">商品浏览</text>
</view>
<view class="menu-item" @click="goTo('/pages/order/list')">
<text class="menu-icon">📋</text>
<text class="menu-text">我的订单</text>
</view>
</view>
<!-- 快捷操作 - 管理员/销售 -->
<view class="section" v-if="!isCustomer">
<view class="section-title">今日概览</view> <view class="section-title">今日概览</view>
<view class="stats-grid"> <view class="stats-grid">
<view class="stat-item"> <view class="stat-item">
@@ -47,6 +61,16 @@
</view> </view>
</view> </view>
<!-- 顾客提示 -->
<view class="section" v-else>
<view class="section-title">温馨提示</view>
<view class="tips">
<text class="tip-text"> 您可以浏览商品</text>
<text class="tip-text"> 您可以查看半年内的订单</text>
<text class="tip-text"> 如需下单请联系销售人员</text>
</view>
</view>
<!-- 退出登录 --> <!-- 退出登录 -->
<button class="logout-btn" @click="logout">退出登录</button> <button class="logout-btn" @click="logout">退出登录</button>
</view> </view>
@@ -56,11 +80,14 @@
import authApi from '@/api/auth' import authApi from '@/api/auth'
import orderApi from '@/api/order' import orderApi from '@/api/order'
import productApi from '@/api/product' import productApi from '@/api/product'
import { getRole, isCustomer as checkIsCustomer } from '@/utils/auth'
export default { export default {
data() { data() {
return { return {
userInfo: {}, userInfo: {},
role: 'admin',
isCustomer: false,
stats: { stats: {
orderCount: 0, orderCount: 0,
actualAmount: 0, actualAmount: 0,
@@ -68,12 +95,31 @@ export default {
} }
} }
}, },
computed: {
roleText() {
return this.isCustomer ? '顾客' : '销售员'
}
},
onLoad() { onLoad() {
this.role = getRole()
this.isCustomer = checkIsCustomer()
this.loadUserInfo() this.loadUserInfo()
if (!this.isCustomer) {
this.loadStats() this.loadStats()
}
}, },
methods: { methods: {
async loadUserInfo() { async loadUserInfo() {
// 假登录时从本地存储获取
const localRole = uni.getStorageSync('role')
if (localRole) {
this.userInfo = {
username: localRole === 'admin' ? '管理员' : '顾客',
role: localRole
}
return
}
try { try {
const userInfo = await authApi.getCurrentUser() const userInfo = await authApi.getCurrentUser()
this.userInfo = userInfo this.userInfo = userInfo
@@ -83,13 +129,11 @@ export default {
}, },
async loadStats() { async loadStats() {
try { try {
// 获取今日订单统计
const today = new Date().toISOString().split('T')[0] const today = new Date().toISOString().split('T')[0]
const stats = await orderApi.getStatistics({ startDate: today }) const stats = await orderApi.getStatistics({ startDate: today })
this.stats.orderCount = stats.orderCount || 0 this.stats.orderCount = stats.orderCount || 0
this.stats.actualAmount = stats.actualAmount || 0 this.stats.actualAmount = stats.actualAmount || 0
// 获取库存预警
const alerts = await productApi.getStockAlerts() const alerts = await productApi.getStockAlerts()
this.stats.stockAlerts = alerts ? alerts.length : 0 this.stats.stockAlerts = alerts ? alerts.length : 0
} catch (e) { } catch (e) {
@@ -109,6 +153,8 @@ export default {
console.error(e) console.error(e)
} }
uni.removeStorageSync('token') uni.removeStorageSync('token')
uni.removeStorageSync('userId')
uni.removeStorageSync('role')
uni.reLaunch({ url: '/pages/login/index' }) uni.reLaunch({ url: '/pages/login/index' })
} }
} }
@@ -144,12 +190,20 @@ export default {
.role-tag { .role-tag {
margin-left: 20rpx; margin-left: 20rpx;
padding: 8rpx 16rpx; padding: 8rpx 16rpx;
background: #e6f7ff;
color: #1890ff;
border-radius: 8rpx; border-radius: 8rpx;
font-size: 24rpx; font-size: 24rpx;
} }
.role-tag.admin {
background: #e6f7ff;
color: #1890ff;
}
.role-tag.customer {
background: #fff7e6;
color: #fa8c16;
}
.menu-grid { .menu-grid {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
@@ -210,6 +264,17 @@ export default {
margin-top: 10rpx; margin-top: 10rpx;
} }
.tips {
display: flex;
flex-direction: column;
}
.tip-text {
font-size: 26rpx;
color: #666;
line-height: 40rpx;
}
.logout-btn { .logout-btn {
background: #fff; background: #fff;
color: #ff4d4f; color: #ff4d4f;

View File

@@ -95,11 +95,48 @@ export default {
return 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 { try {
const data = await authApi.passwordLogin(this.username, this.password) const data = await authApi.passwordLogin(this.username, this.password)
uni.setStorageSync('token', data.token) uni.setStorageSync('token', data.token)
uni.setStorageSync('refreshToken', data.refreshToken) uni.setStorageSync('refreshToken', data.refreshToken)
uni.setStorageSync('userId', data.userId) uni.setStorageSync('userId', data.userId)
uni.setStorageSync('role', data.role || 'customer')
uni.showToast({ title: '登录成功', icon: 'success' }) uni.showToast({ title: '登录成功', icon: 'success' })
setTimeout(() => { setTimeout(() => {

25
src/utils/auth.js Normal file
View File

@@ -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()
}