jiuyiUniapp/jiuyi2/pages/release/commodity.vue

351 lines
8.0 KiB
Vue
Raw Normal View History

2024-12-27 15:03:48 +08:00
<script setup>
// 发布商品
2025-01-10 10:51:21 +08:00
import {
ref,
reactive
} from 'vue'
//
import {
onLoad
} from '@dcloudio/uni-app'
2025-01-12 02:05:25 +08:00
// api
import api from '@/api/index.js'
// 工具库
import util from '@/common/js/util.js'
2025-01-10 10:51:21 +08:00
// 表单
const form = reactive({
2025-02-06 23:34:13 +08:00
id: '',
2025-01-14 20:37:57 +08:00
categoryId: '',
sliderImage: [],
spec: [],
2025-01-10 10:51:21 +08:00
})
2025-01-14 20:37:57 +08:00
// 分类
const category = reactive([])
// 分类下标
const categoryIndex = ref('')
2025-01-10 10:51:21 +08:00
2025-02-06 23:34:13 +08:00
onLoad((option) => {
if (option.id) {
form.id = option.id
Promise.all([getProDetail(), getCategory()]).then(rs => {
const detail = rs[0]
const cate = rs[1]
// 商品图片
form.sliderImage = detail.sliderImage.split(',')
// 下标
categoryIndex.value = cate.findIndex(item => item.id === detail.categoryId)
// 商品id
form.id = detail.id
// 商品规格
form.categoryId = detail.categoryId
// 商品价格
form.price = detail.price
// 商品名称
form.name = detail.name
// 商品成本价
form.cost = detail.cost
// 商品佣金
form.commission = detail.commission
// 规格
form.spec = detail.specs.map(item => {
return {
image: item.image,
sku: item.sku,
stock: item.stock,
}
})
})
} else {
// 添加商品规格
handlePushSpec()
// 获取商品分类
getCategory()
}
2025-01-10 10:51:21 +08:00
})
2025-02-06 23:34:13 +08:00
// 获取商品详情
function getProDetail() {
return new Promise((resolve, reject) => {
api.shop.productDetail({
query: {
productionId: form.id
},
}).then(rs => {
if (rs.code === 200) {
resolve(rs.data)
return
}
util.alert({
content: rs.msg,
showCancel: false,
})
})
})
}
2025-01-14 20:37:57 +08:00
// 获取商品分类
function getCategory() {
return new Promise((resolve, reject) => {
api.shop.getCategory({
query: {
categoryCode: '0'
},
}).then(rs => {
if (rs.code === 200) {
category.push(...rs.data)
resolve(category)
return
}
util.alert({
content: rs.msg,
showCancel: false,
})
})
})
}
// 添加商品规格
function handlePushSpec() {
form.spec.push({
// 图片
image: '',
// 名称
sku: '',
// 库存
stock: '',
})
}
/**
* 删除规格
* @param {Object} index 下标
*/
function handleRemoveSpec(index) {
form.spec.splice(index, 1)
}
// 上传轮播图
function uploadImage() {
util.upload_image({
type: 1,
success: rs => {
form.sliderImage.push(rs.value)
}
})
}
// 上传规格图片
function uploadSpecImage(item) {
util.upload_image({
type: 1,
success: rs => {
item.image = rs.value
}
})
}
/**
* 选择分类
* @param {Object} ev
*/
function handleCate(ev) {
let index = ev.detail.value
if (index == categoryIndex.value) return
categoryIndex.value = index
form.categoryId = category[categoryIndex.value].id
}
2025-01-10 10:51:21 +08:00
// 发布商品
function handleSubmit() {
const data = {
...form
}
2025-01-14 20:37:57 +08:00
// 轮播图
data.sliderImage = data.sliderImage.join(',')
//
api.shop.saveProduct({
data,
}).then(rs => {
if (rs.code == 200) {
2025-02-04 13:45:10 +08:00
util.alert({
content: '商品发布成功,请等待后台审核',
showCancel: false,
}).then(() => {
uni.navigateBack()
})
2025-01-14 20:37:57 +08:00
return
}
util.alert({
2025-02-04 13:45:10 +08:00
content: rs.msg,
2025-01-14 20:37:57 +08:00
showCancel: false,
})
})
2025-01-10 10:51:21 +08:00
}
2024-12-27 15:03:48 +08:00
</script>
<template>
<view class="app">
2024-12-29 19:06:34 +08:00
<view class="container">
<view class="main area">
<view class="title mtb20">商品图片</view>
<view class="imgList mt20">
2025-01-14 20:37:57 +08:00
<view class="imgs" v-for="(item,index) in form.sliderImage">
<image class="wh120 br10" :src="item" mode="aspectFill" />
<view class="close">
<uni-icons type="clear" color="#f00" size="40rpx" />
</view>
</view>
<view class="imgs" @click="uploadImage">
2024-12-29 19:06:34 +08:00
<image class="wh120" src="/static/shop-upload-image.png" mode="aspectFit" />
</view>
</view>
</view>
<view class="main area">
2025-01-14 20:37:57 +08:00
<view class="line">
<view class="title mtb20">商品标题</view>
<textarea v-model="form.name" class="textarea mtb20" placeholder="最多输入60字符30个汉字" />
</view>
<view class="line ptb20">
2025-02-06 23:34:13 +08:00
<picker :range="category" range-key="name" :value="categoryIndex" @change="handleCate">
2025-01-14 20:37:57 +08:00
<view class="rows">
<view class="title w150">类目</view>
<view class="col f1">
<text v-if="category[categoryIndex]">{{category[categoryIndex].name}}</text>
<text v-else class="placeholderStyle">点击选择</text>
</view>
<uni-icons type="right" />
</view>
</picker>
</view>
2024-12-29 19:06:34 +08:00
</view>
2025-01-14 20:37:57 +08:00
<view class="main area" v-for="(item,index) in form.spec" :key="index">
<view class="line rows">
<view class="name mtb30 f32">规格{{index + 1}}</view>
<view class="" @click="handleRemoveSpec(index)">
<uni-icons type="trash" size="40rpx" color="#aaa" />
2024-12-29 19:06:34 +08:00
</view>
</view>
<view class="line rows ptb20">
2025-01-14 20:37:57 +08:00
<view class="title w150">名称</view>
2024-12-29 19:06:34 +08:00
<view class="col f1">
2025-01-14 20:37:57 +08:00
<input type="text" v-model="item.sku" placeholder="输入规格" placeholder-class="placeholderStyle" />
2024-12-29 19:06:34 +08:00
</view>
</view>
<view class="line rows ptb20">
2025-01-14 20:37:57 +08:00
<view class="title w150">图片</view>
<view class="imgList mt20 f1">
<view class="imgs" v-if="item.image" @click="uploadSpecImage(item)">
<image class="wh120 br20" :src="item.image" mode="aspectFill" />
</view>
<view class="imgs" v-else @click="uploadSpecImage(item)">
<image class="wh120" src="/static/shop-upload-image.png" mode="aspectFit" />
</view>
2024-12-29 19:06:34 +08:00
</view>
</view>
<view class="line rows ptb20">
<view class="title w150">库存</view>
<view class="col f1">
2025-02-04 13:45:10 +08:00
<input type="text" v-model="item.stock" placeholder="输入库存"
placeholder-class="placeholderStyle" />
2024-12-29 19:06:34 +08:00
</view>
</view>
</view>
2025-01-14 20:37:57 +08:00
<view class="main fmid mtb20 mlr20 ptb20 c666 f34 br20 bfff" @click="handlePushSpec">
<uni-icons type="plus" size="40rpx" />
<view class="ml10">添加规格</view>
</view>
2024-12-29 19:06:34 +08:00
<view class="main area">
<view class="line rows ptb20">
2025-01-14 20:37:57 +08:00
<view class="title w150">商品价格</view>
2024-12-29 19:06:34 +08:00
<view class="col f1">
2025-01-14 20:37:57 +08:00
<input type="text" v-model="form.price" placeholder="输入价格"
placeholder-class="placeholderStyle" />
2024-12-29 19:06:34 +08:00
</view>
</view>
<view class="line rows ptb20">
2025-01-14 20:37:57 +08:00
<view class="title w150">商品成本价</view>
2024-12-29 19:06:34 +08:00
<view class="col f1">
2025-01-14 20:37:57 +08:00
<input type="text" v-model="form.cost" placeholder="输入价格"
placeholder-class="placeholderStyle" />
2025-01-10 10:51:21 +08:00
</view>
</view>
<view class="line rows ptb20">
<view class="title w150">出让佣金</view>
<view class="col f1">
2025-01-14 20:37:57 +08:00
<input type="text" v-model="form.commission" placeholder="输入价格"
placeholder-class="placeholderStyle" />
2024-12-29 19:06:34 +08:00
</view>
</view>
</view>
2025-01-14 20:37:57 +08:00
<view class="main area" v-if="0">
2024-12-29 19:06:34 +08:00
<view class="line rows ptb20">
<view class="title w150">代金券</view>
<view class="col f1">
<text class="placeholderStyle">点击选择</text>
</view>
</view>
</view>
2025-01-14 20:37:57 +08:00
<view class="main area" v-if="0">
2024-12-29 19:06:34 +08:00
<view class="title rows mtb20">
<image src="/static/commodity-release-video.png" mode="aspectFit" class="wh45" />
<view class="f1 ml20">添加链接到视频</view>
</view>
<view class="rows mtb20">
<view class="mr10 f28">再第几秒展示</view>
<input type="text" placeholder="输入秒数" placeholder-class="placeholderStyle" />
</view>
</view>
</view>
<view class="fill" style="height: 210rpx;"></view>
<view class="footer plr30 bfff shadow">
2025-01-14 20:37:57 +08:00
<view class="btn lg primary" @click="handleSubmit">立即上架</view>
2024-12-29 19:06:34 +08:00
</view>
2024-12-27 15:03:48 +08:00
</view>
</template>
<style lang="scss">
//
2024-12-29 19:06:34 +08:00
.container {
color: #333;
// 内容
.area {
overflow: hidden;
margin: 20rpx;
padding: 0 30rpx;
background-color: #fff;
border-radius: 20rpx;
}
.line+.line {
border-top: 2rpx solid #eee;
}
// 标题
.title {
font-size: 28rpx;
}
//
.textarea {
width: 100%;
height: 200rpx;
}
}
2024-12-27 15:03:48 +08:00
</style>