增加添加商品功能
This commit is contained in:
@@ -162,60 +162,82 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref } from 'vue'
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { openRoute } from '@/layouts/admin/store/adminNavStore.uts'
|
||||
import StatusSwitch from '@/components/StatusSwitch.uvue'
|
||||
import supa, { ensureSupabaseReady } from '@/components/supadb/aksupainstance'
|
||||
|
||||
const total = ref(49)
|
||||
const total = ref(0)
|
||||
const activeStatus = ref('selling')
|
||||
const activeDropdownId = ref<number | null>(null)
|
||||
|
||||
const statusTabs = ref([
|
||||
{ key: 'selling', label: '出售中的商品', count: 49 },
|
||||
{ key: 'warehouse', label: '仓库中的商品', count: 4 },
|
||||
{ key: 'soldout', label: '已经售罄商品', count: 11 },
|
||||
{ key: 'alarm', label: '警戒库存商品', count: 27 },
|
||||
{ key: 'recycle', label: '回收站的商品', count: 176 },
|
||||
{ key: 'selling', label: '出售中的商品', count: 0 },
|
||||
{ key: 'warehouse', label: '仓库中的商品', count: 0 },
|
||||
{ key: 'soldout', label: '已经售罄商品', count: 0 },
|
||||
{ key: 'alarm', label: '警戒库存商品', count: 0 },
|
||||
{ key: 'recycle', label: '回收站的商品', count: 0 },
|
||||
])
|
||||
|
||||
const productList = ref([
|
||||
{
|
||||
id: 963,
|
||||
image: 'https://img1.baidu.com/it/u=254065646,3100346083&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
|
||||
name: 'UR2024夏季新款女装复古纯欲氛围感一字肩短款T恤衫UWG440060',
|
||||
activities: ['kj', 'pt'],
|
||||
typeName: '普通商品',
|
||||
price: '0.01',
|
||||
sales: 639,
|
||||
stock: 1602,
|
||||
sort: 9999,
|
||||
status: 1
|
||||
},
|
||||
{
|
||||
id: 108,
|
||||
image: 'https://img2.baidu.com/it/u=3033501986,2204481084&fm=253&fmt=auto&app=138&f=JPEG?w=569&h=500',
|
||||
name: 'FOMIX 蛋壳椅 进口头层牛皮橙色单人沙发椅Egg chair设计师师单椅单沙头层牛皮/单椅',
|
||||
activities: ['pt', 'ms'],
|
||||
typeName: '普通商品',
|
||||
price: '7580.00',
|
||||
sales: 14,
|
||||
stock: 16638,
|
||||
sort: 9999,
|
||||
status: 1
|
||||
},
|
||||
{
|
||||
id: 48,
|
||||
image: 'https://img0.baidu.com/it/u=1762118431,3101886131&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
|
||||
name: '阿迪达斯官网 adidas BBALL CAP COT 男女训练运动帽子FQ5270 传奇墨水蓝/传奇墨水蓝/白 XL',
|
||||
activities: ['kj', 'pt', 'ms'],
|
||||
typeName: '普通商品',
|
||||
price: '100.00',
|
||||
sales: 841,
|
||||
stock: 2318,
|
||||
sort: 9998,
|
||||
status: 1
|
||||
const productList = ref<any[]>([])
|
||||
|
||||
onMounted(() => {
|
||||
fetchProducts()
|
||||
uni.$on('REFRESH_PRODUCT_LIST', () => {
|
||||
fetchProducts()
|
||||
})
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
uni.$off('REFRESH_PRODUCT_LIST')
|
||||
})
|
||||
|
||||
// 从 ml_products 表中获取商品数据
|
||||
async function fetchProducts() {
|
||||
await ensureSupabaseReady()
|
||||
|
||||
// 从本地缓存获取 current merchant_id
|
||||
const currentMerchantId = uni.getStorageSync('merchant_id') as string | null
|
||||
|
||||
if (!currentMerchantId) {
|
||||
uni.showToast({ title: '未获取到商家信息,请重新登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
])
|
||||
|
||||
try {
|
||||
const { data, error, count } = await supa
|
||||
.from('ml_products')
|
||||
.select('id, name, main_image_url, base_price, available_stock, status, created_at', { count: 'exact' })
|
||||
.eq('merchant_id', currentMerchantId)
|
||||
.order('created_at', { ascending: false })
|
||||
|
||||
if (error) {
|
||||
console.error('Fetch products error:', error)
|
||||
return
|
||||
}
|
||||
|
||||
if (data) {
|
||||
productList.value = data.map((item: any) => {
|
||||
return {
|
||||
id: item.id,
|
||||
image: item.main_image_url || '',
|
||||
name: item.name || '未命名商品',
|
||||
activities: [],
|
||||
typeName: '普通商品',
|
||||
price: item.base_price !== null ? Number(item.base_price).toFixed(2) : '0.00',
|
||||
sales: 0,
|
||||
stock: item.available_stock || 0,
|
||||
sort: 0,
|
||||
status: item.status || 0
|
||||
}
|
||||
})
|
||||
total.value = count || data.length
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('获取商品列表失败:', err)
|
||||
uni.showToast({ title: '加载失败', icon: 'none' })
|
||||
}
|
||||
}
|
||||
|
||||
function getActivityName(tag: string): string {
|
||||
if (tag === 'kj') return '砍价'
|
||||
@@ -225,6 +247,11 @@ function getActivityName(tag: string): string {
|
||||
}
|
||||
|
||||
function goEdit(id: number | null) {
|
||||
if (id !== null) {
|
||||
uni.setStorageSync('edit_product_id', id)
|
||||
} else {
|
||||
uni.removeStorageSync('edit_product_id')
|
||||
}
|
||||
openRoute('product_edit')
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user