完成添加商品和查看商品
This commit is contained in:
@@ -192,7 +192,7 @@ const formData = ref({
|
||||
|
||||
onMounted(async () => {
|
||||
await ensureSupabaseReady()
|
||||
const mId = supa.getSession().user?.id as string | null
|
||||
const mId = supa.getSession().user?.getString('id') ?? null
|
||||
if (!mId) {
|
||||
uni.showToast({ title: '商家未获取到信息,请重新登录', icon: 'none' })
|
||||
return
|
||||
@@ -376,7 +376,7 @@ async function doSaveProduct(targetStatus: number) {
|
||||
|
||||
// 必须获取正确的 merchant_id,并且存在
|
||||
if (!formData.value.merchant_id || formData.value.merchant_id.startsWith('merchant_123')) {
|
||||
const mId = supa.getSession().user?.id as string | null
|
||||
const mId = supa.getSession().user?.getString('id') ?? null
|
||||
if (!mId) {
|
||||
uni.showToast({ title: '未获取到商家信息,请重新登录', icon: 'none' })
|
||||
return
|
||||
@@ -399,10 +399,10 @@ async function doSaveProduct(targetStatus: number) {
|
||||
formData.value.published_at = null
|
||||
}
|
||||
|
||||
const payload = {
|
||||
const payload : UTSJSONObject = {
|
||||
merchant_id: formData.value.merchant_id,
|
||||
name: formData.value.name,
|
||||
category_id: formData.value.category_id || null,
|
||||
category_id: formData.value.category_id,
|
||||
base_price: Number(formData.value.base_price),
|
||||
main_image_url: formData.value.image_urls.length > 0 ? formData.value.image_urls[0] : '', // 首图同步
|
||||
image_urls: formData.value.image_urls,
|
||||
@@ -414,6 +414,11 @@ async function doSaveProduct(targetStatus: number) {
|
||||
attributes: formData.value.attributes,
|
||||
published_at: formData.value.published_at
|
||||
}
|
||||
// 新建商品时自动生成唯一的 product_code
|
||||
if (!formData.value.id) {
|
||||
const uid = Math.random().toString(36).substring(2, 10).toUpperCase()
|
||||
payload['product_code'] = `P${Date.now()}${uid}`
|
||||
}
|
||||
|
||||
let result;
|
||||
if (formData.value.id) {
|
||||
@@ -422,8 +427,9 @@ async function doSaveProduct(targetStatus: number) {
|
||||
.update(payload)
|
||||
.eq('id', formData.value.id)
|
||||
.eq('merchant_id', formData.value.merchant_id)
|
||||
.execute()
|
||||
} else {
|
||||
result = await supa.from('ml_products').insert([payload])
|
||||
result = await supa.from('ml_products').insert([payload]).execute()
|
||||
}
|
||||
|
||||
uni.hideLoading()
|
||||
|
||||
Reference in New Issue
Block a user