feat: 添加商品管理页面和角色权限控制
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Agent
2026-03-24 00:53:43 +00:00
parent 17f6b49e5d
commit b79f951514
5 changed files with 615 additions and 17 deletions

View File

@@ -0,0 +1,496 @@
<template>
<view class="page">
<!-- 搜索栏 -->
<view class="search-bar">
<input
class="search-input"
v-model="keyword"
placeholder="搜索商品名称"
@confirm="search"
/>
<button class="search-btn" @click="search">搜索</button>
<button class="add-btn" @click="addProduct">+</button>
</view>
<!-- 商品列表 -->
<view class="product-list">
<view
v-for="item in products"
:key="item.productId"
class="product-item"
>
<view class="product-info" @click="editProduct(item)">
<text class="product-name">{{ item.name }}</text>
<text class="product-spec">{{ item.spec || '-' }}</text>
<view class="product-price">
<text class="price">¥{{ item.price }}</text>
<text class="unit">/{{ item.unit }}</text>
</view>
<view class="product-status">
<text :class="['status', item.status === 1 ? 'on' : 'off']">
{{ item.status === 1 ? '上架' : '下架' }}
</text>
</view>
</view>
<view class="product-actions">
<view class="action-btn" @click="toggleStatus(item)">
{{ item.status === 1 ? '下架' : '上架' }}
</view>
<view class="action-btn delete" @click="deleteProduct(item)">
删除
</view>
</view>
</view>
<!-- 空状态 -->
<view v-if="products.length === 0" class="empty">
<text>暂无商品</text>
</view>
</view>
<!-- 商品表单弹窗 -->
<view class="modal" v-if="showModal">
<view class="modal-mask" @click="closeModal"></view>
<view class="modal-content">
<view class="modal-header">
<text>{{ isEdit ? '编辑商品' : '新增商品' }}</text>
<text class="close-btn" @click="closeModal">×</text>
</view>
<view class="modal-body">
<view class="form-item">
<text class="label">商品名称*</text>
<input class="input" v-model="form.name" placeholder="请输入商品名称" />
</view>
<view class="form-item">
<text class="label">规格</text>
<input class="input" v-model="form.spec" placeholder="请输入规格" />
</view>
<view class="form-item">
<text class="label">单位*</text>
<input class="input" v-model="form.unit" placeholder="如:个、箱、米" />
</view>
<view class="form-item">
<text class="label">价格*</text>
<input class="input" type="digit" v-model="form.price" placeholder="请输入价格" />
</view>
<view class="form-item">
<text class="label">分类</text>
<picker :range="categories" range-key="name" @change="onCategoryChange">
<view class="picker">
{{ form.categoryId ? getCategoryName(form.categoryId) : '请选择分类' }}
</view>
</picker>
</view>
<view class="form-item">
<text class="label">备注</text>
<textarea class="textarea" v-model="form.remark" placeholder="请输入备注" />
</view>
</view>
<view class="modal-footer">
<button class="cancel-btn" @click="closeModal">取消</button>
<button class="confirm-btn" @click="saveProduct">保存</button>
</view>
</view>
</view>
</view>
</template>
<script>
import productApi from '@/api/product'
import { canManageProduct } from '@/utils/auth'
export default {
data() {
return {
keyword: '',
products: [],
categories: [],
showModal: false,
isEdit: false,
form: {
productId: '',
name: '',
spec: '',
unit: '',
price: '',
categoryId: '',
remark: '',
status: 1
}
}
},
onLoad() {
if (!canManageProduct()) {
uni.showToast({ title: '无权限', icon: 'none' })
uni.navigateBack()
return
}
this.loadCategories()
this.loadProducts()
},
methods: {
async loadCategories() {
try {
const categories = await productApi.getCategories()
this.categories = categories || []
} catch (e) {
console.error(e)
}
},
async loadProducts() {
try {
const res = await productApi.getProducts({
keyword: this.keyword,
page: 1,
pageSize: 100
})
this.products = res.records || []
} catch (e) {
console.error(e)
}
},
search() {
this.loadProducts()
},
addProduct() {
this.isEdit = false
this.form = {
productId: '',
name: '',
spec: '',
unit: '',
price: '',
categoryId: '',
remark: '',
status: 1
}
this.showModal = true
},
editProduct(item) {
this.isEdit = true
this.form = { ...item }
this.showModal = true
},
closeModal() {
this.showModal = false
},
onCategoryChange(e) {
const index = e.detail.value
this.form.categoryId = this.categories[index].categoryId
},
getCategoryName(categoryId) {
const cat = this.categories.find(c => c.categoryId === categoryId)
return cat ? cat.name : ''
},
async saveProduct() {
if (!this.form.name) {
uni.showToast({ title: '请输入商品名称', icon: 'none' })
return
}
if (!this.form.unit) {
uni.showToast({ title: '请输入单位', icon: 'none' })
return
}
if (!this.form.price) {
uni.showToast({ title: '请输入价格', icon: 'none' })
return
}
try {
if (this.isEdit) {
await productApi.updateProduct(this.form)
uni.showToast({ title: '更新成功', icon: 'success' })
} else {
await productApi.createProduct(this.form)
uni.showToast({ title: '创建成功', icon: 'success' })
}
this.closeModal()
this.loadProducts()
} catch (e) {
console.error(e)
uni.showToast({ title: e.message || '操作失败', icon: 'none' })
}
},
async toggleStatus(item) {
const newStatus = item.status === 1 ? 0 : 1
try {
await productApi.updateProduct({
productId: item.productId,
status: newStatus
})
uni.showToast({ title: newStatus === 1 ? '已上架' : '已下架', icon: 'success' })
this.loadProducts()
} catch (e) {
console.error(e)
uni.showToast({ title: '操作失败', icon: 'none' })
}
},
async deleteProduct(item) {
uni.showModal({
title: '确认删除',
content: `确定要删除商品"${item.name}"吗?`,
success: async (res) => {
if (res.confirm) {
try {
await productApi.deleteProduct(item.productId)
uni.showToast({ title: '删除成功', icon: 'success' })
this.loadProducts()
} catch (e) {
console.error(e)
uni.showToast({ title: '删除失败', icon: 'none' })
}
}
}
})
}
}
}
</script>
<style>
.page {
padding: 20rpx;
}
.search-bar {
display: flex;
margin-bottom: 20rpx;
}
.search-input {
flex: 1;
height: 70rpx;
padding: 0 20rpx;
background: #fff;
border-radius: 8rpx;
font-size: 28rpx;
}
.search-btn {
width: 120rpx;
height: 70rpx;
line-height: 70rpx;
background: #3cc51f;
color: #fff;
border: none;
border-radius: 8rpx;
margin-left: 20rpx;
font-size: 28rpx;
}
.add-btn {
width: 70rpx;
height: 70rpx;
line-height: 70rpx;
background: #1890ff;
color: #fff;
border: none;
border-radius: 8rpx;
margin-left: 20rpx;
font-size: 40rpx;
}
.product-list {
background: #fff;
border-radius: 16rpx;
}
.product-item {
display: flex;
justify-content: space-between;
padding: 24rpx;
border-bottom: 1rpx solid #f5f5f5;
}
.product-info {
flex: 1;
}
.product-name {
font-size: 28rpx;
font-weight: bold;
display: block;
}
.product-spec {
font-size: 24rpx;
color: #999;
margin-top: 8rpx;
display: block;
}
.product-price {
margin-top: 12rpx;
}
.price {
color: #ff4d4f;
font-size: 28rpx;
font-weight: bold;
}
.unit {
color: #999;
font-size: 24rpx;
}
.product-status {
margin-top: 8rpx;
}
.status {
font-size: 22rpx;
padding: 4rpx 12rpx;
border-radius: 4rpx;
}
.status.on {
background: #e6f7ff;
color: #1890ff;
}
.status.off {
background: #fff7e6;
color: #fa8c16;
}
.product-actions {
display: flex;
flex-direction: column;
justify-content: center;
}
.action-btn {
padding: 8rpx 20rpx;
background: #e6f7ff;
color: #1890ff;
border-radius: 4rpx;
font-size: 24rpx;
margin-bottom: 10rpx;
}
.action-btn.delete {
background: #fff1f0;
color: #ff4d4f;
}
.empty {
padding: 100rpx;
text-align: center;
color: #999;
}
/* 弹窗 */
.modal {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 999;
}
.modal-mask {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.5);
}
.modal-content {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: #fff;
border-radius: 24rpx 24rpx 0 0;
max-height: 80vh;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 30rpx;
border-bottom: 1rpx solid #f5f5f5;
font-size: 32rpx;
font-weight: bold;
}
.close-btn {
font-size: 48rpx;
color: #999;
}
.modal-body {
padding: 30rpx;
max-height: 60vh;
overflow-y: auto;
}
.form-item {
margin-bottom: 24rpx;
}
.label {
display: block;
font-size: 26rpx;
color: #666;
margin-bottom: 10rpx;
}
.input {
width: 100%;
height: 70rpx;
padding: 0 20rpx;
background: #f5f5f5;
border-radius: 8rpx;
font-size: 28rpx;
}
.picker {
width: 100%;
height: 70rpx;
padding: 0 20rpx;
background: #f5f5f5;
border-radius: 8rpx;
font-size: 28rpx;
line-height: 70rpx;
}
.textarea {
width: 100%;
height: 150rpx;
padding: 20rpx;
background: #f5f5f5;
border-radius: 8rpx;
font-size: 28rpx;
}
.modal-footer {
display: flex;
padding: 30rpx;
border-top: 1rpx solid #f5f5f5;
}
.cancel-btn, .confirm-btn {
flex: 1;
height: 80rpx;
line-height: 80rpx;
border-radius: 40rpx;
font-size: 28rpx;
}
.cancel-btn {
background: #f5f5f5;
color: #666;
margin-right: 20rpx;
}
.confirm-btn {
background: #3cc51f;
color: #fff;
}
</style>