123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <template>
- <!-- 规格弹窗 -->
- <su-popup :show="show" round="10" @close="emits('close')">
- <!-- SKU 信息 -->
- <!-- {{state.selectedSku}} -->
- <!-- {{goodsInfo}} -->
- <view class="ss-modal-box bg-white ss-flex-col">
- <view class="modal-header ss-flex ss-col-center">
- <view class="header-left ss-m-r-30">
- <image class="sku-image" :src="state.selectedSku.picUrl || goodsInfo.picUrl" mode="aspectFill" />
- </view>
- <view class="header-right ss-flex-col ss-row-between ss-flex-1">
- <view class="goods-title ss-line-2">{{ goodsInfo.name }}</view>
- <view class="header-right-bottom ss-flex ss-col-center ss-row-between">
- <view class="ss-flex">
- <view class="price-text">
- <image src="@/static/icon/points.png" v-if="goodsInfo.spuPayType == 2"
- style="width: 30rpx; height: 30rpx"></image>
-
- <text v-if="goodsInfo.highPrecision">
- {{ fen2yuan6(state.selectedSku.highPrecisionPrice || goodsInfo.highPrecisionPrice) }}
- </text>
- <text v-else>
- {{ fen2yuan(state.selectedSku.price || goodsInfo.price) }}
- </text>
- <!-- {{ fen2yuan(state.selectedSku.price || goodsInfo.price) }} -->
- </view>
- <view class="origin-price-text ss-m-l-10" v-if="
- state.selectedSku.promotionFee >= 0 ||
- goodsInfo.promotionFee >= 0
- ">
- {{$t('common.points')}}:{{
- fen2yuan(
- state.selectedSku.promotionFee || goodsInfo.promotionFee,
- )
- }}
- </view>
- </view>
- <view class="stock-text ss-m-l-20">
- {{ formatStock('exact', state.selectedSku.stock || goodsInfo.stock) }}
- </view>
- </view>
- </view>
- </view>
-
- <!-- 属性选择 -->
- <view class="modal-content ss-flex-1">
- <scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
-
- <view class="sku-item ss-m-b-20" v-for="property in propertyList" :key="property.id">
- <view class="label-text ss-m-b-20">{{ property.name }}</view>
- <view class="ss-flex ss-col-center ss-flex-wrap">
- <button class="ss-reset-button spec-btn" v-for="value in property.values" :class="[
- {
- 'ui-BG-Main-Gradient':
- state.currentPropertyArray[property.id] === value.id,
- },
- {
- 'disabled-btn': value.disabled === true,
- },
- ]" :key="value.id" :disabled="value.disabled === true" @tap="onSelectSku(property.id, value.id)">
- {{ value.name }}
- </button>
- </view>
- </view>
- <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40"
- v-if="goodsInfo.spuPayType == 2">
- <view class="label-text">{{$t('selectSku.purchase_amount')}} {{$t('selectSku.available_points',{points:points2point(userWallet.integralDO.currentQuota)}) }}</view>
- <view class="ss-flex ss-col-center">
- <button class="ss-reset-button all-btn " @click="useAllPonints">{{ $t('commmon.all' )}}</button>
- <image src="@/static/icon/points.png" v-if="goodsInfo.spuPayType == 2"
- style="width: 30rpx; height: 30rpx"></image>
- <input v-model="state.selectedSku.use_points" class="uni-input input-points"
- style="width: 100rpx; text-align: center" type="number" placeholder="0"
- @input="inputPoints" :disabled="!state.selectedSku.id" />
- </view>
- </view>
- <view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40" v-else>
- <view class="label-text">{{ $t('selectSku.purchase_quantity' )}}</view>
- <su-number-box :min="1" :max="state.selectedSku.stock" :step="1"
- v-model="state.selectedSku.goods_num" @change="onNumberChange($event)" />
- </view>
- </scroll-view>
- </view>
- <!-- 操作区 -->
- <view class="modal-footer border-top">
- <view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center"
- v-if="goodsInfo.spuType && goodsInfo.spuPayType == 1">
- <button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart">{{$t('common.add_to_cart')}}</button>
- <button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">{{$t('common.buy_now')}}</button>
- </view>
- <view class="buy-box ss-flex ss-col-center ss-p-r-20" v-else>
- <button class="ss-reset-button disabled-btn one-buy-btn ui-Shadow-Main " @tap="onBuy">
- <template v-if="goodsInfo.spuPayType != 1 && !goodsInfo.spuType">
- {{$t('common.redeem_now')}}
- </template>
- <template v-else-if="!goodsInfo.spuType">
- {{$t('common.buy_now')}}
- </template>
- <template v-else>
- {{$t('common.redeem_now')}}
- </template>
- </button>
- </view>
- </view>
- </view>
- </su-popup>
- </template>
- <script setup>
- import {
- computed,
- reactive,
- watch,
- onMounted,
- nextTick
- } from 'vue';
- import sheep from '@/sheep';
- import {
- formatStock,
- convertProductPropertyList,
- fen2yuan,
- points2point,
- fen2yuan6
- } from '@/sheep/hooks/useGoods';
- import PayWalletApi from '@/sheep/api/pay/wallet';
- import { t } from '@/locale'
- const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
- const props = defineProps({
- goodsInfo: {
- type: Object,
- default () {},
- },
- show: {
- type: Boolean,
- default: false,
- },
- });
- const userWallet = computed(() => sheep.$store('user').userWallet);
- const state = reactive({
- selectedSku: {}, // 选中的 SKU
- currentPropertyArray: [], // 当前选中的属性,实际是个 Map。key 是 property 编号,value 是 value 编号
- });
- const propertyList = convertProductPropertyList(props.goodsInfo.skus);
- // SKU 列表
- const skuList = computed(() => {
- let skuPrices = props.goodsInfo.skus;
- for (let price of skuPrices) {
- price.value_id_array = price.properties.map((item) => item.valueId);
- }
- return skuPrices;
- });
- function calcNums(){
- if(props.goodsInfo.highPrecision){
- // console.log(state.selectedSku.use_points +"/" + (state.selectedSku.highPrecisionPrice / 100))
- state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / (state.selectedSku.highPrecisionPrice / 100) );
- }else{
- state.selectedSku.goods_num = parseInt(state.selectedSku.use_points / (fen2yuan(state.selectedSku.price)) );
- }
- // console.log("计算后的数量是",state.selectedSku.goods_num)
- }
- // 使用全部积分
- async function useAllPonints(){
- const {code,data} = await PayWalletApi.getDuserInfo();
- const userCanUsePoints = parseFloat(points2point(data.integralDO.currentQuota));
- console.log(userCanUsePoints)
- state.selectedSku.use_points = parseInt(userCanUsePoints)
- calcNums()
- }
- function inputPoints(e) {
- const points = e.detail.value;
- console.log(typeof points);
- if(points == ''){
- return false
- }
- // 用户可用的积分
- const userCanUsePoints = parseFloat(points2point(userWallet.value.integralDO.currentQuota));
- calcNums()
- nextTick(()=>{
- state.selectedSku.use_points = parseInt(points);
- calcNums()
- })
-
- if (points <= 0) {
- sheep.$helper.toast(t('selectSku.min_points_required'))
- if (userCanUsePoints>=1) {
- nextTick(() => {
- state.selectedSku.use_points = 1;
- calcNums()
- })
- }else{
- nextTick(() => {
- state.selectedSku.use_points = 0;
- calcNums()
- })
- }
- }
- if (points > userCanUsePoints) {
- sheep.$helper.toast(t('selectSku.insufficient_points'));
- // console.log('输入的积分大于可用积分了', points, userCanUsePoints);
- nextTick(() => {
- state.selectedSku.use_points = parseInt(userCanUsePoints);
- // 重新计算 对应的商品数量
- calcNums()
- });
- }
- }
- watch(
- () => state.selectedSku,
- (newVal) => {
- emits('change', newVal);
- }, {
- immediate: true, // 立即执行
- deep: true, // 深度监听
- },
- );
- // 输入框改变数量
- function onNumberChange(e) {
- if (e === 0) return;
- if (state.selectedSku.goods_num === e) return;
- state.selectedSku.goods_num = e;
- }
- // 加入购物车
- function onAddCart() {
- if (state.selectedSku.id <= 0) {
- sheep.$helper.toast(t('selectSku.select_specification'));
- return;
- }
- if (state.selectedSku.stock <= 0) {
- sheep.$helper.toast(t('selectSku.insufficient_stock'));
- return;
- }
- emits('addCart', state.selectedSku);
- }
- // 立即购买
- async function onBuy() {
- if (props.goodsInfo.spuPayType == 2) {
- // 下单时再查一次可用积分
- const {code,data} = await PayWalletApi.getDuserInfo();
- const userCanUsePoints = parseFloat(points2point(data.integralDO.currentQuota));
- if(userCanUsePoints<state.selectedSku.use_points){
- sheep.$helper.toast(t('selectSku.min_points_required'));
- return false
- }
-
- // return false
- if (state.selectedSku.goods_num < 1 || !state.selectedSku.use_points) {
- sheep.$helper.toast(t('selectSku.amount_less_than_min'));
- return;
- }
- // 下单时检测如果库存不足
- if (state.selectedSku.goods_num > state.selectedSku.stock) {
- state.selectedSku.use_points = (state.selectedSku.stock * fen2yuan(state.selectedSku.price)).toFixed(2)
- state.selectedSku.goods_num = state.selectedSku.stock
- }
- }
- if (state.selectedSku.id <= 0) {
- sheep.$helper.toast(t('selectSku.select_specification'));
- return;
- }
- if (state.selectedSku.stock <= 0) {
- sheep.$helper.toast(t('selectSku.insufficient_stock'));
- return;
- }
- emits('buy', state.selectedSku);
- state.selectedSku.use_points = 0;
- }
-
- // 改变禁用状态:计算每个 property 属性值的按钮,是否禁用
- function changeDisabled(isChecked = false, propertyId = 0, valueId = 0) {
- let newSkus = []; // 所有可以选择的 sku 数组
- if (isChecked) {
- // 情况一:选中 property
- // 获得当前点击选中 property 的、所有可用 SKU
- for (let price of skuList.value) {
- if (price.stock <= 0) {
- continue;
- }
- if (price.value_id_array.indexOf(valueId) >= 0) {
- newSkus.push(price);
- }
- }
- } else {
- // 情况二:取消选中 property
- // 当前所选 property 下,所有可以选择的 SKU
- newSkus = getCanUseSkuList();
- }
- // 所有存在并且有库存未选择的 SKU 的 value 属性值 id
- let noChooseValueIds = [];
- for (let price of newSkus) {
- noChooseValueIds = noChooseValueIds.concat(price.value_id_array);
- }
- noChooseValueIds = Array.from(new Set(noChooseValueIds)); // 去重
- if (isChecked) {
- // 去除当前选中的 value 属性值 id
- let index = noChooseValueIds.indexOf(valueId);
- noChooseValueIds.splice(index, 1);
- } else {
- // 循环去除当前已选择的 value 属性值 id
- state.currentPropertyArray.forEach((currentPropertyId) => {
- if (currentPropertyId.toString() !== '') {
- return;
- }
- // currentPropertyId 为空是反选 填充的
- let index = noChooseValueIds.indexOf(currentPropertyId);
- if (index >= 0) {
- // currentPropertyId 存在于 noChooseValueIds
- noChooseValueIds.splice(index, 1);
- }
- });
- }
- // 当前已选择的 property 数组
- let choosePropertyIds = [];
- if (!isChecked) {
- // 当前已选择的 property
- state.currentPropertyArray.forEach((currentPropertyId, currentValueId) => {
- if (currentPropertyId !== '') {
- // currentPropertyId 为空是反选 填充的
- choosePropertyIds.push(currentValueId);
- }
- });
- } else {
- // 当前点击选择的 property
- choosePropertyIds = [propertyId];
- }
- for (let propertyIndex in propertyList) {
- // 当前点击的 property、或者取消选择时候,已选中的 property 不进行处理
- if (choosePropertyIds.indexOf(propertyList[propertyIndex]['id']) >= 0) {
- continue;
- }
- // 如果当前 property id 不存在于有库存的 SKU 中,则禁用
- for (let valueIndex in propertyList[propertyIndex]['values']) {
- propertyList[propertyIndex]['values'][valueIndex]['disabled'] =
- noChooseValueIds.indexOf(
- propertyList[propertyIndex]['values'][valueIndex]['id'],
- ) < 0; // true 禁用 or false 不禁用
- }
- }
- }
- // 当前所选属性下,获取所有有库存的 SKU 们
- function getCanUseSkuList() {
- let newSkus = [];
- for (let sku of skuList.value) {
- if (sku.stock <= 0) {
- continue;
- }
- let isOk = true;
- state.currentPropertyArray.forEach((propertyId) => {
- // propertyId 不为空,并且,这个 条 sku 没有被选中,则排除
- if (propertyId.toString() !== '' && sku.value_id_array.indexOf(propertyId) < 0) {
- isOk = false;
- }
- });
- if (isOk) {
- newSkus.push(sku);
- }
- }
- return newSkus;
- }
- // 选择规格
- function onSelectSku(propertyId, valueId) {
- // console.log(propertyId,valueId)
- // 清空已输入金额
- state.selectedSku.use_points = 0;
- // 清空已选择
- let isChecked = true; // 选中 or 取消选中
- if (
- state.currentPropertyArray[propertyId] !== undefined &&
- state.currentPropertyArray[propertyId] === valueId
- ) {
- // 点击已被选中的,删除并填充 ''
- isChecked = false;
- state.currentPropertyArray.splice(propertyId, 1, '');
- } else {
- // 选中
- state.currentPropertyArray[propertyId] = valueId;
- }
- // 选中的 property 大类
- let choosePropertyId = [];
- state.currentPropertyArray.forEach((currentPropertyId) => {
- if (currentPropertyId !== '') {
- // currentPropertyId 为空是反选 填充的
- choosePropertyId.push(currentPropertyId);
- }
- });
- // 当前所选 property 下,所有可以选择的 SKU 们
- let newSkuList = getCanUseSkuList();
- // 判断所有 property 大类是否选择完成
- if (choosePropertyId.length === propertyList.length && newSkuList.length) {
- newSkuList[0].goods_num = state.selectedSku.goods_num || 1;
- state.selectedSku = newSkuList[0];
- } else {
- state.selectedSku = {};
- }
- // 改变 property 禁用状态
- changeDisabled(isChecked, propertyId, valueId);
- }
- changeDisabled(false);
- onMounted(() => {
- // 如果商品的属性只有一条,则默认选中
- if (propertyList.length == 1 && propertyList[0].values.length == 1) {
- onSelectSku(0, 0);
- }
-
- });
- // TODO 非繁人:待讨论的优化点:1)单规格,要不要默认选中;2)默认要不要选中第一个规格
- </script>
- <style lang="scss" scoped>
- .origin-price-text {
- font-size: 22rpx;
- font-weight: 400;
- color: $gray-c;
- font-family: OPPOSANS;
- background: #ffca3e;
- padding: 2px 8px;
- border-radius: 4px;
- display: inline-block;
- color: #597533;
- }
- // 购买
- .buy-box {
- padding: 10rpx 0;
- .add-btn {
- width: 356rpx;
- height: 80rpx;
- border-radius: 40rpx 0 0 40rpx;
- background-color: var(--ui-BG-Main-light);
- color: var(--ui-BG-Main);
- }
- .buy-btn {
- width: 356rpx;
- height: 80rpx;
- border-radius: 0 40rpx 40rpx 0;
- background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
- color: #fff;
- }
- .one-buy-btn {
- width: 98%;
- height: 80rpx;
- border-radius: 40rpx;
- background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
- color: #fff;
- }
- .score-btn {
- width: 100%;
- margin: 0 20rpx;
- height: 80rpx;
- border-radius: 40rpx;
- background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
- color: #fff;
- }
- }
- .ss-modal-box {
- border-radius: 30rpx 30rpx 0 0;
- max-height: 1000rpx;
- .modal-header {
- position: relative;
- padding: 80rpx 20rpx 40rpx;
- .sku-image {
- width: 160rpx;
- height: 160rpx;
- border-radius: 10rpx;
- }
- .header-right {
- height: 160rpx;
- }
- .close-icon {
- position: absolute;
- top: 10rpx;
- right: 20rpx;
- font-size: 46rpx;
- opacity: 0.2;
- }
- .goods-title {
- font-size: 28rpx;
- font-weight: 500;
- line-height: 42rpx;
- }
- .score-img {
- width: 36rpx;
- height: 36rpx;
- margin: 0 4rpx;
- }
- .score-text {
- font-size: 30rpx;
- font-weight: 500;
- color: $red;
- font-family: OPPOSANS;
- }
- .price-text {
- font-size: 30rpx;
- font-weight: 500;
- color: $red;
- font-family: OPPOSANS;
- &::before {
- // content: '¥';
- // font-size: 30rpx;
- // font-weight: 500;
- // color: $red;
- }
- }
- .stock-text {
- font-size: 26rpx;
- color: #999999;
- }
- }
- .modal-content {
- padding: 0 20rpx;
- .modal-content-scroll {
- max-height: 600rpx;
- .label-text {
- font-size: 26rpx;
- font-weight: 500;
- }
- .buy-num-box {
- height: 100rpx;
- }
- .spec-btn {
- height: 60rpx;
- min-width: 100rpx;
- padding: 0 30rpx;
- background: #f4f4f4;
- border-radius: 30rpx;
- color: #434343;
- font-size: 26rpx;
- margin-right: 10rpx;
- margin-bottom: 10rpx;
- }
- .all-btn{
- height: 60rpx;
- min-width: 80rpx;
- padding: 0 30rpx;
- border-radius: 30rpx;
- font-size: 26rpx;
- margin-right: 10rpx;
- border: 2rpx solid var(--ui-BG-Main);
- color: var(--ui-BG-Main);
- }
- .disabled-btn {
- font-weight: 400;
- color: #c6c6c6;
- background: #f8f8f8;
- }
- }
- }
- }
- </style>
|