jiuyiUniapp/jiuyi2/components/shop/detail/makeOrder.vue

255 lines
5.1 KiB
Vue
Raw Normal View History

2025-01-21 22:33:44 +08:00
<script setup>
2025-01-23 00:57:47 +08:00
/**
* 下单组件
*/
2025-01-21 22:33:44 +08:00
import {
ref,
reactive,
getCurrentInstance,
computed,
defineEmits,
onMounted,
defineProps,
2025-02-17 19:26:33 +08:00
defineExpose,
watch,
2025-01-21 22:33:44 +08:00
} from 'vue'
//
import util from '@/common/js/util.js'
//
import api from '@/api/index.js'
2025-01-23 00:57:47 +08:00
// 地址
import JyCommodityAddress from '@/components/public/jy-commodity-address'
2025-01-21 22:33:44 +08:00
const {
proxy
} = getCurrentInstance()
//
const props = defineProps({
// 商品信息
detail: {
type: Object,
default: () => ({})
},
2025-01-23 00:57:47 +08:00
// 模式 detail详情 collect收藏列表
mode: {
type: String,
default: 'detail'
},
2025-01-21 22:33:44 +08:00
})
2025-01-23 00:57:47 +08:00
// 提交
const emit = defineEmits(['confirm'])
2025-01-21 22:33:44 +08:00
// 地址
2025-02-17 19:26:33 +08:00
const address = ref({})
2025-01-21 22:33:44 +08:00
// 已选择的规格下标
const spaceIndex = ref(0)
2025-01-23 00:57:47 +08:00
// 上次选择的规格下标
const spaceIndexLast = ref(0)
2025-01-21 22:33:44 +08:00
// 数量
const payNum = ref(1)
2025-01-23 00:57:47 +08:00
// 上次数量
const payNumLast = ref(1)
2025-01-21 22:33:44 +08:00
// 当前选择的规格
const currentSpec = computed(() => {
let spec = props.detail.specs || []
return spec[spaceIndex.value] || {}
})
// 应付总价
const total = computed(() => {
let price = parseFloat(props.detail.price) * 100
let result = parseInt(price * payNum.value) / 100
return result
})
2025-02-17 19:26:33 +08:00
watch(() => address.value, (nValue) => {
console.log('address', nValue)
})
2025-01-21 22:33:44 +08:00
onMounted(() => {
// 获取默认收货地址
2025-01-23 00:57:47 +08:00
if (props.mode === 'detail') getDefaultAddress()
2025-01-21 22:33:44 +08:00
})
// 获取默认收货地址
function getDefaultAddress() {
2025-02-16 22:35:46 +08:00
util.isLogin().then(res => {
api.shop.getDefaultAddress({}).then(res => {
if (res.code === 200) {
2025-02-17 19:26:33 +08:00
if (res.data) Object.assign(address.value, {}, res.data)
2025-02-16 22:35:46 +08:00
return
}
util.alert({
content: res.msg,
showCancel: false,
})
2025-01-21 22:33:44 +08:00
})
})
}
/**
* 跳转
* @param {Object} url 跳转路径
*/
function link(url) {
uni.navigateTo({
url,
})
}
/**
* 选择规格
* @param {Object} index 选择规格下标
*/
function handleSpec(index) {
if (spaceIndex.value !== index) spaceIndex.value = index
}
2025-01-23 00:57:47 +08:00
/**
* 打开弹窗
* @param {Object} event 携带参数
*/
function open(event) {
// 同步选择值
if (event) {
spaceIndex.value = event.spaceIndex || 0
payNum.value = event.payNum || 1
}
// 打开弹窗
proxy.$refs.payment.open()
}
// 关闭弹窗
function close() {
proxy.$refs.payment.close()
}
// 确认
function handleConfirm() {
// 验证必填项
2025-02-17 19:26:33 +08:00
if (props.mode == 'detail' && !address.value.id) {
2025-01-23 00:57:47 +08:00
util.alert('请选择收货地址')
return
}
2025-01-23 21:29:16 +08:00
//
let param = {
2025-01-23 00:57:47 +08:00
spec: currentSpec.value,
spaceIndex: spaceIndex.value,
payNum: payNum.value,
2025-01-23 21:29:16 +08:00
}
// 如果有地址信息
2025-02-17 19:26:33 +08:00
if (address.value.id) param.address = address.value
2025-01-23 21:29:16 +08:00
emit('confirm', param)
2025-01-23 00:57:47 +08:00
// 同步选择值
spaceIndexLast.value = spaceIndex.value
payNumLast.value = payNum.value
//
close()
}
/**
* 弹窗状态改变
* @param {Object} ev
*/
function handlePopChange(ev) {
// 关闭弹窗时 同步选择值
if (!ev.show) {
spaceIndex.value = spaceIndexLast.value
payNum.value = payNumLast.value
}
}
defineExpose({
open,
close,
})
2025-01-21 22:33:44 +08:00
</script>
<template>
<!-- 规格 -->
2025-01-23 00:57:47 +08:00
<uni-popup type="bottom" ref="payment" @change="handlePopChange">
2025-01-21 22:33:44 +08:00
<view class="buy popBot plr20 bfff">
2025-01-23 00:57:47 +08:00
<view class="address mtb40" v-if="mode === 'detail'">
2025-02-17 19:26:33 +08:00
<JyCommodityAddress v-model:address="address" />
2025-01-21 22:33:44 +08:00
</view>
2025-01-23 00:57:47 +08:00
2025-01-21 22:33:44 +08:00
<!-- 商品图 价格 明细 数量 -->
2025-02-15 16:10:05 +08:00
<view class="jy-card-commodity-content df fdr mtb40">
2025-01-21 22:33:44 +08:00
<!-- 商品图 -->
<image class="wh200 br10" :src="currentSpec.image" mode="aspectFill" />
2025-02-15 16:10:05 +08:00
2025-01-21 22:33:44 +08:00
<!-- 价格 明细 数量 -->
<view class="info f1 df fdc jcsb ml30">
<!-- 价格 -->
2025-02-15 16:10:05 +08:00
<view class="price df fdr aic">
2025-01-21 22:33:44 +08:00
<text class="cFF9B27 f28">单价</text>
<text class="cFF9B27 f24"></text>
<text class="cFF9B27 f50">{{detail.price}}</text>
</view>
<!-- 已选 -->
<view class="content-info-num">
<text class="f26 c333">已选 {{currentSpec.sku}}</text>
</view>
<!-- 计数器 -->
<view class="w200">
<uni-number-box v-model="payNum" :step="1" />
</view>
</view>
</view>
2025-01-23 00:57:47 +08:00
2025-01-21 22:33:44 +08:00
<!-- 规格 -->
<view class="spec">
2025-02-15 16:10:05 +08:00
<view class="selection df fdr fww">
2025-01-21 22:33:44 +08:00
<!-- disabled 销量为零不能选 -->
2025-02-15 16:10:05 +08:00
<text class="option mtb20 mr20 f26" :class="{'active': spaceIndex === index}"
v-for="(item,index) in detail.specs" :key="item.id"
@click="handleSpec(index)">{{item.sku}}</text>
2025-01-21 22:33:44 +08:00
</view>
</view>
2025-01-23 00:57:47 +08:00
<view class="btn lg primary mtb30" @click="handleConfirm">
2025-02-15 16:10:05 +08:00
<text class="tac cfff" v-if="mode == 'detail'">立即下单 {{total}}</text>
<text class="tac cfff" v-if="mode == 'collect'">确定</text>
2025-01-21 22:33:44 +08:00
</view>
</view>
</uni-popup>
</template>
2025-01-23 00:57:47 +08:00
<style lang="scss" scoped>
// 规格
.spec {
// 选项
.option {
padding: 5rpx 15rpx;
2025-02-15 16:10:05 +08:00
color: #333;
2025-01-23 00:57:47 +08:00
background-color: #F7F7F7;
border-radius: 10rpx;
2025-02-21 17:57:08 +08:00
// transition: .3s;
2025-01-23 00:57:47 +08:00
border: 2rpx solid #F7F7F7;
2025-02-15 16:10:05 +08:00
.text {}
// .text {
// color: #FF9B27;
// }
2025-01-23 00:57:47 +08:00
// 被选中
&.active {
2025-02-15 16:10:05 +08:00
color: #FF9B27;
2025-01-23 00:57:47 +08:00
background-color: #FFFBF8;
border-color: #FF9B27;
}
// 不能选
&.disabled {
2025-02-21 17:57:08 +08:00
background-color: #F7F7F7;
2025-01-23 00:57:47 +08:00
.txt {
color: #999;
}
}
}
}
2025-01-21 22:33:44 +08:00
</style>