mall-app-t/pages/product/manageGoods.vue

588 lines
15 KiB
Vue
Raw Permalink Normal View History

2024-06-03 01:09:01 +08:00
<template>
2024-08-28 18:36:22 +08:00
<pb-layout
class="manage-goods"
:title="goodsTitle"
leftIcon="leftIcon"
navbar="normal"
:bgStyle="bgStyle"
opacityBgUi="bg-white"
color="black"
>
<view class="goods-form">
<uni-forms ref="formRef" v-model="formData" :rules="rules" label-position="top" label-width="160">
<uni-forms-item label="商品封面图" name="picUrl" required>
<p-uploader
v-model:url="formData.picUrl"
:readonly="!canEdit"
fileMediatype="image"
limit="1"
mode="grid"
:imageStyles="{ width: '168rpx', height: '168rpx' }"
/>
</uni-forms-item>
<uni-forms-item label="商品轮播图" name="sliderPicUrls" required>
<p-uploader
v-model:url="formData.sliderPicUrls"
:readonly="!canEdit"
fileMediatype="image"
limit="6"
mode="grid"
:imageStyles="{ width: '168rpx', height: '168rpx' }"
/>
</uni-forms-item>
<uni-forms-item label="商品名称" name="name" required>
<uni-easyinput
type="text"
trim="all"
v-model="formData.name"
:disabled="!canEdit"
placeholder="请输入商品名称"
/>
</uni-forms-item>
<uni-forms-item
label="商品分类"
@tap="openPicker('category', 'multiple')"
name="categoryId"
label-position="left"
required
>
<uni-easyinput
type="text"
v-model="formData.categoryText"
:styles="selfStyles"
placeholderStyle="color:#8a8a8a"
:clearable="false"
:inputBorder="false"
placeholder="请选择商品分类"
disabled
>
<template v-slot:right>
<uni-icons type="right" />
</template>
</uni-easyinput>
</uni-forms-item>
<uni-forms-item
label="商品品牌"
name="brandId"
label-position="left"
required
@tap="openPicker('brand', 'single')"
>
<uni-easyinput
type="text"
v-model="formData.brandText"
:styles="selfStyles"
placeholderStyle="color:#8a8a8a"
:clearable="false"
:inputBorder="false"
placeholder="请选择商品品牌"
disabled
>
<template v-slot:right>
<uni-icons type="right" />
</template>
</uni-easyinput>
</uni-forms-item>
<uni-forms-item label="商品规格" name="skus" required label-position="left">
<view class="btn-group">
<button class="ss-reset-button ss-set-property" @tap="clickSetProperty">规格设置</button>
</view>
</uni-forms-item>
<uni-forms-item label="商品关键词" name="keyword" required>
<uni-easyinput type="text" v-model="formData.keyword" :disabled="!canEdit" placeholder="请输入商品关键词" />
</uni-forms-item>
<uni-forms-item label="商品简介" name="introduction" required>
<uni-easyinput
type="textarea"
:disabled="!canEdit"
trim="all"
autoHeight
v-model="formData.introduction"
placeholder="请输入商品简介"
/>
</uni-forms-item>
<uni-forms-item
label="物流设置"
@tap="openPicker('delivery', 'single')"
name="deliveryTypes"
label-position="left"
required
>
<uni-easyinput
type="text"
:clearable="false"
:styles="selfStyles"
placeholderStyle="color:#8a8a8a"
:inputBorder="false"
v-model="formData.deliveryText"
placeholder="请选择配送方式"
disabled
>
<template v-slot:right>
<uni-icons type="right" />
</template>
</uni-easyinput>
</uni-forms-item>
<uni-forms-item label="虚拟销量" name="virtualSalesCount" required>
<uni-easyinput
v-model="formData.virtualSalesCount"
type="number"
:disabled="!canEdit"
placeholder="请输入虚拟销量"
/>
</uni-forms-item>
<uni-forms-item label="商品详情" required>
<piaoyiEditor
:values="richValues"
@changes="saveContens"
:maxlength="3000"
:readOnly="richReadOnly"
:photoUrl="photoUrl"
:api="richApi"
/>
</uni-forms-item>
</uni-forms>
<view @tap="onSubmit" v-if="canEdit">
<button class="ss-reset-button submit-button ui-Shadow-Main">提交</button>
</view>
</view>
<p-picker ref="pickerRef" :mode="pickerMode" :options-cols="optionsCols" @confirm="onRDPickerConfirm"></p-picker>
</pb-layout>
2024-06-03 01:09:01 +08:00
</template>
<script setup>
2024-06-11 00:34:43 +08:00
import { ref, computed } from 'vue'
import { onLoad, onPageScroll, onShow } from '@dcloudio/uni-app'
2024-06-11 18:33:56 +08:00
import _ from 'lodash'
import GoodsApi from '@/peach/api/trade/goods'
import piaoyiEditor from '@/uni_modules/piaoyi-editor/components/piaoyi-editor/piaoyi-editor.vue'
2024-06-03 18:35:53 +08:00
import peach from '@/peach'
2024-06-11 18:33:56 +08:00
import { baseUrl, apiPath } from '@/peach/config'
2024-06-12 18:33:39 +08:00
import { handleTree, floatToFixed2, convertToInteger } from '@/peach/utils'
2024-06-11 18:33:56 +08:00
import { validateSku } from './js/sku'
2024-06-03 01:09:01 +08:00
const bgStyle = {
2024-08-28 18:36:22 +08:00
backgroundImage: '',
backgroundColor: '#fff',
description: '',
2024-06-03 01:09:01 +08:00
}
2024-06-04 18:43:13 +08:00
const DELIVERY_TYPES = [
2024-08-28 18:36:22 +08:00
{
value: 3,
label: '到店核销',
},
{
value: 4,
label: '商家配送',
},
2024-06-04 18:43:13 +08:00
]
2024-06-04 01:13:55 +08:00
2024-06-03 01:09:01 +08:00
const selfStyles = {
2024-08-28 18:36:22 +08:00
backgroundColor: '#f9f9f9',
2024-06-03 01:09:01 +08:00
}
2024-06-04 18:43:13 +08:00
const pickerRef = ref()
2024-06-11 18:33:56 +08:00
const richValues = ref('')
const photoUrl = baseUrl + apiPath
const richApi = '/infra/file/upload'
2024-08-28 18:36:22 +08:00
const richReadOnly = ref(true)
2024-06-03 01:09:01 +08:00
const formData = ref({
2024-08-28 18:36:22 +08:00
picUrl: '',
sliderPicUrls: [],
name: '',
categoryId: '',
categoryText: '',
brandId: '',
keyword: '',
deliveryTypes: [],
deliveryText: '',
sort: 0,
giveIntegral: 0,
virtualSalesCount: 0,
subCommissionType: false,
introduction: '',
2024-06-03 18:35:53 +08:00
})
2024-06-03 01:09:01 +08:00
2024-06-03 18:35:53 +08:00
const rules = {
2024-08-28 18:36:22 +08:00
name: {
rules: [
{
required: true,
errorMessage: '请输入商品名称',
},
],
},
picUrl: {
rules: [
{
required: true,
errorMessage: '请上传商品封面图',
},
],
},
sliderPicUrls: {
rules: [
{
required: true,
errorMessage: '请上传商品轮播图',
},
],
},
categoryId: {
rules: [
{
required: true,
errorMessage: '请选择商品分类',
},
],
},
brandId: {
rules: [
{
required: true,
errorMessage: '请选择商品品牌',
},
],
},
skus: {
rules: [
{
required: true,
errorMessage: '请选择商品规格',
},
],
},
keyword: {
rules: [
{
required: true,
errorMessage: '请输入商品关键字',
},
],
},
introduction: {
rules: [
{
required: true,
errorMessage: '请输入商品简介',
},
],
},
deliveryTypes: {
rules: [
{
required: true,
errorMessage: '请选择商品物流',
},
],
},
2024-06-03 18:35:53 +08:00
}
2024-06-03 01:09:01 +08:00
2024-06-03 18:35:53 +08:00
const formRef = ref(null)
2024-06-04 18:43:13 +08:00
const pickerMode = ref('single')
const popMark = ref('')
2024-06-11 00:34:43 +08:00
const goodsTitle = ref('')
2024-06-04 18:43:13 +08:00
const categoryList = ref([])
2024-06-05 18:58:12 +08:00
const brandList = ref([])
2024-06-04 18:43:13 +08:00
const optionsCols = ref([])
2024-06-11 00:34:43 +08:00
const canEdit = computed(() => peach.$store('trade').canEdit)
2024-06-03 01:09:01 +08:00
2024-06-04 18:43:13 +08:00
function openPicker(mark, mode) {
2024-08-28 18:36:22 +08:00
if (!canEdit.value) return
pickerMode.value = mode
popMark.value = mark
if (mark === 'delivery') {
optionsCols.value = DELIVERY_TYPES
pickerRef.value.onOpen([0])
} else if (mark === 'category') {
optionsCols.value = categoryList.value
pickerRef.value.onOpen([0, 0])
} else if (mark === 'brand') {
optionsCols.value = brandList.value
pickerRef.value.onOpen([0])
}
2024-06-04 01:13:55 +08:00
}
2024-06-04 18:43:13 +08:00
function onRDPickerConfirm(e) {
2024-08-28 18:36:22 +08:00
if (popMark.value === 'delivery') {
formData.value.deliveryTypes = []
formData.value.deliveryText = DELIVERY_TYPES[e.value[0]].label
formData.value.deliveryTypes.push(DELIVERY_TYPES[e.value[0]].value)
}
if (popMark.value === 'category') {
formData.value.categoryId = categoryList.value[e.value[0]].children[e.value[1]].id
formData.value.categoryText =
categoryList.value[e.value[0]].name + '/' + categoryList.value[e.value[0]].children[e.value[1]].name
}
if (popMark.value === 'brand') {
formData.value.brandId = brandList.value[e.value[0]].id
formData.value.brandText = brandList.value[e.value[0]].name
}
2024-06-06 02:20:25 +08:00
}
2024-06-03 01:09:01 +08:00
2024-06-06 02:20:25 +08:00
function clickSetProperty() {
2024-08-28 18:36:22 +08:00
if (formData.value.skus) {
// 如果是多规格,处理格式问题,合并属性
let temp = formData.value.skus
.map((item) => {
return item.properties.map((sitem) => ({
id: sitem.propertyId,
children: [sitem.valueId],
}))
})
.flat(1)
// 去除重复数据
let result = temp.reduce((pre, cur) => {
let index = pre.findIndex((item) => item.id === cur.id)
if (index !== -1) {
pre[index].children.push(...new Set(cur.children))
} else {
pre.push(cur)
}
return pre
}, [])
peach.$store('trade').$patch({
selectedProperty: result,
})
}
2024-06-05 18:58:12 +08:00
2024-08-28 18:36:22 +08:00
peach.$router.go('/pages/product/sku')
2024-06-05 18:58:12 +08:00
}
function getProduct(id) {
2024-08-28 18:36:22 +08:00
GoodsApi.getProduct({ id }).then((res) => {
formData.value = res.data
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
// 内容为图片时,需要在末尾插入空行
richValues.value = res.data.description ? res.data.description + '<p><br/></p>' : ''
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
// 循环遍历 categoryList从二级分类找出和 formData.value.categoryId 相等的
2024-09-19 16:23:19 +08:00
2024-08-28 18:36:22 +08:00
let tempCategory = categoryList.value.find((item) => {
2024-09-19 16:23:19 +08:00
return item.children?.find((child) => {
2024-08-28 18:36:22 +08:00
return child.id === formData.value.categoryId
})
})
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
formData.value.categoryText =
tempCategory.name +
'/' +
tempCategory.children.find((item) => {
return item.id === formData.value.categoryId
}).name
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
// 循环遍历 brandList, 从一级分类找出和 formData.value.brandId 相等的
2024-09-19 16:23:19 +08:00
2024-08-28 18:36:22 +08:00
let tempBrand = brandList.value.find((item) => {
return item.id === formData.value.brandId
})
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
formData.value.brandText = tempBrand.name
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
// 从 DELIVERY_TYPES 找出和 formData.value.deliveryTypes 相等的
formData.value.deliveryText = DELIVERY_TYPES.find((item) => {
return item.value === formData.value.deliveryTypes[0]
}).label
2024-06-11 18:33:56 +08:00
2024-08-28 18:36:22 +08:00
formData.value.skus.forEach((item) => {
item.price = floatToFixed2(item.price)
item.marketPrice = floatToFixed2(item.marketPrice)
item.costPrice = floatToFixed2(item.costPrice)
})
2024-06-12 18:33:39 +08:00
2024-08-28 18:36:22 +08:00
peach.$store('trade').$patch({
goodsInfo: formData.value,
skus: formData.value.skus,
specType: formData.value.specType,
2024-06-06 02:20:25 +08:00
})
2024-08-28 18:36:22 +08:00
})
2024-06-03 01:09:01 +08:00
}
2024-06-12 18:33:39 +08:00
function saveContens(e) {
2024-08-28 18:36:22 +08:00
formData.value.description = e.html
2024-06-12 18:33:39 +08:00
}
2024-06-04 18:43:13 +08:00
function onSubmit() {
2024-08-28 18:36:22 +08:00
formData.value.skus = peach.$store('trade').skus
formRef.value
.validate()
.then(async (res) => {
// 校验 skus 是否正确填写完成
validateSku()
let tempObj = _.cloneDeep(formData.value)
tempObj.skus.forEach((item) => {
item.name = formData.value.name
item.price = convertToInteger(item.price)
item.marketPrice = convertToInteger(item.marketPrice)
item.costPrice = convertToInteger(item.costPrice)
})
tempObj.specType = peach.$store('trade').specType
let msg = ''
if (formData.value.id) {
tempObj.id = formData.value.id
await GoodsApi.editProduct(tempObj)
msg = '修改成功'
} else {
await GoodsApi.addProduct(tempObj)
msg = '添加成功'
}
uni.showToast({
title: msg,
icon: 'none',
duration: 4000,
})
setTimeout(() => {
peach.$router.redirect('/pages/index/product')
}, 1000)
})
.catch((err) => {
console.log('err', err)
if (err) {
uni.showToast({
title: err[0]?.errorMessage,
icon: 'none',
duration: 4000,
2024-06-11 18:33:56 +08:00
})
2024-08-28 18:36:22 +08:00
}
})
2024-06-04 18:43:13 +08:00
}
// 获得商品分类
async function getCategoryList() {
2024-08-28 18:36:22 +08:00
let { data } = await GoodsApi.getGoodsCategory()
categoryList.value = handleTree(data)
2024-06-04 18:43:13 +08:00
}
// 获得商品品牌
async function getBrandList() {
2024-08-28 18:36:22 +08:00
let { data } = await GoodsApi.getBrand()
brandList.value = data
2024-06-04 18:43:13 +08:00
}
2024-06-03 01:09:01 +08:00
2024-06-05 18:58:12 +08:00
onLoad(async (options) => {
2024-08-28 18:36:22 +08:00
await getCategoryList()
await getBrandList()
2024-06-11 00:34:43 +08:00
2024-08-28 18:36:22 +08:00
goodsTitle.value = options.title
2024-06-11 00:34:43 +08:00
2024-08-28 18:36:22 +08:00
if (options.id) {
getProduct(options.id)
peach.$store('trade').detailTag = options.mark
}
})
2024-06-11 18:33:56 +08:00
onShow(() => {
let isSave = peach.$store('trade').isSave
if (isSave) {
formData.value.skus = peach.$store('trade').skus
formData.value.specType = peach.$store('trade').specType
peach.$store('trade').isSave = false
} else {
peach.$store('trade').$patch({
skus: formData.value.skus || [],
specType: formData.value.specType,
})
}
})
2024-08-28 18:36:22 +08:00
onPageScroll((e) => {
if (canEdit.value && e.scrollTop > 200) {
richReadOnly.value = false
}
2024-06-03 18:35:53 +08:00
})
2024-06-03 01:09:01 +08:00
</script>
<style lang="scss" scoped>
2024-06-11 00:34:43 +08:00
@mixin ss-set-property {
2024-08-28 18:36:22 +08:00
width: 80px;
height: 60rpx;
line-height: normal;
background: var(--ui-BG-Main);
border-radius: 28rpx;
font-size: 26rpx;
font-weight: 500;
color: #fff;
2024-06-11 00:34:43 +08:00
}
2024-06-03 01:09:01 +08:00
.manage-goods {
2024-08-28 18:36:22 +08:00
.goods-form {
margin: 40rpx;
:deep() {
.uni-easyinput__content-input {
font-size: 28rpx !important;
color: #333333 !important;
line-height: normal !important;
}
.uni-easyinput__placeholder-class {
font-size: 14px;
}
.is-direction-left {
.uni-forms-item__label {
padding-left: 10px;
background-color: #f9f9f9;
border-radius: 10px 0 0 10px;
}
uni-icons {
margin-right: 10px;
}
.uni-easyinput__content {
border-radius: 0 10px 10px 0;
2024-06-06 02:20:25 +08:00
}
2024-08-28 18:36:22 +08:00
}
2024-06-06 02:20:25 +08:00
2024-08-28 18:36:22 +08:00
.is-direction-left {
.is-disabled {
color: #333333;
text-align: right;
2024-06-04 18:43:13 +08:00
}
2024-06-06 02:20:25 +08:00
2024-08-28 18:36:22 +08:00
.uni-forms-item__error {
left: -160rpx !important;
2024-06-05 18:58:12 +08:00
}
2024-08-28 18:36:22 +08:00
}
}
.btn-group {
height: 100%;
display: flex;
justify-content: flex-end;
align-items: center;
background-color: #f9f9f9;
border-radius: 0 10px 10px 0;
.ss-set-property {
@include ss-set-property;
}
}
.submit-button {
width: 100%;
height: 80rpx;
border-radius: 40rpx;
background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
color: $white;
2024-06-11 00:34:43 +08:00
}
2024-08-28 18:36:22 +08:00
}
2024-06-03 01:09:01 +08:00
}
</style>