feat: 商品详情页 sku模块 渲染数据

This commit is contained in:
jqtmviyu 2025-05-05 17:33:22 +08:00
parent f2ebf64622
commit e597bea065
4 changed files with 2044 additions and 1 deletions

View File

@ -0,0 +1,145 @@
import { Component } from '@uni-helper/uni-app-types'
/** SKU 弹出层 */
export type SkuPopup = Component<SkuPopupProps>
/** SKU 弹出层实例 */
export type SkuPopupInstance = InstanceType<SkuPopup>
/** SKU 弹出层属性 */
export type SkuPopupProps = {
/** 双向绑定true 为打开组件false 为关闭组件 */
modelValue: boolean
/** 商品信息本地数据源 */
localdata: SkuPopupLocaldata
/** 按钮模式 1:都显示 2:只显示购物车 3:只显示立即购买 */
mode?: 1 | 2 | 3
/** 该商品已抢完时的按钮文字 */
noStockText?: string
/** 库存文字 */
stockText?: string
/** 点击遮罩是否关闭组件 */
maskCloseAble?: boolean
/** 顶部圆角值 */
borderRadius?: string | number
/** 最小购买数量 */
minBuyNum?: number
/** 最大购买数量 */
maxBuyNum?: number
/** 每次点击后的数量 */
stepBuyNum?: number
/** 是否只能输入 step 的倍数 */
stepStrictly?: boolean
/** 是否隐藏库存的显示 */
hideStock?: false
/** 主题风格 */
theme?: 'default' | 'red-black' | 'black-white' | 'coffee' | 'green'
/** 默认金额会除以100即100=1元若设置为0则不会除以100即1=1元 */
amountType?: 1 | 0
/** 自定义获取商品信息的函数已知支付宝不支持支付宝请改用localdata属性 */
customAction?: () => void
/** 是否显示右上角关闭按钮 */
showClose?: boolean
/** 关闭按钮的图片地址 */
closeImage?: string
/** 价格的字体颜色 */
priceColor?: string
/** 立即购买 - 按钮的文字 */
buyNowText?: string
/** 立即购买 - 按钮的字体颜色 */
buyNowColor?: string
/** 立即购买 - 按钮的背景颜色 */
buyNowBackgroundColor?: string
/** 加入购物车 - 按钮的文字 */
addCartText?: string
/** 加入购物车 - 按钮的字体颜色 */
addCartColor?: string
/** 加入购物车 - 按钮的背景颜色 */
addCartBackgroundColor?: string
/** 商品缩略图背景颜色 */
goodsThumbBackgroundColor?: string
/** 样式 - 不可点击时,按钮的样式 */
disableStyle?: object
/** 样式 - 按钮点击时的样式 */
activedStyle?: object
/** 样式 - 按钮常态的样式 */
btnStyle?: object
/** 字段名 - 商品表id的字段名 */
goodsIdName?: string
/** 字段名 - sku表id的字段名 */
skuIdName?: string
/** 字段名 - 商品对应的sku列表的字段名 */
skuListName?: string
/** 字段名 - 商品规格名称的字段名 */
specListName?: string
/** 字段名 - sku库存的字段名 */
stockName?: string
/** 字段名 - sku组合路径的字段名 */
skuArrName?: string
/** 字段名 - 商品缩略图字段名(未选择sku时) */
goodsThumbName?: string
/** 被选中的值 */
selectArr?: string[]
/** 打开弹出层 */
onOpen: () => void
/** 关闭弹出层 */
onClose: () => void
/** 点击加入购物车时需选择完SKU才会触发*/
onAddCart: (event: SkuPopupEvent) => void
/** 点击立即购买时需选择完SKU才会触发*/
onBuyNow: (event: SkuPopupEvent) => void
}
/** 商品信息本地数据源 */
export type SkuPopupLocaldata = {
/** 商品 ID */
_id: string
/** 商品名称 */
name: string
/** 商品图片 */
goods_thumb: string
/** 商品规格列表 */
spec_list: SkuPopupSpecItem[]
/** 商品SKU列表 */
sku_list: SkuPopupSkuItem[]
}
/** 商品规格名称的集合 */
export type SkuPopupSpecItem = {
/** 规格名称 */
name: string
/** 规格集合 */
list: { name: string }[]
}
/** 商品SKU列表 */
export type SkuPopupSkuItem = {
/** SKU ID */
_id: string
/** 商品 ID */
goods_id: string
/** 商品名称 */
goods_name: string
/** 商品图片 */
image: string
/** SKU 价格 * 100, 注意:需要乘以 100 */
price: number
/** SKU 规格组成, 注意:需要与 spec_list 数组顺序对应 */
sku_name_arr: string[]
/** SKU 库存 */
stock: number
}
/** 当前选择的sku数据 */
export type SkuPopupEvent = SkuPopupSkuItem & {
/** 商品购买数量 */
buy_num: number
}
/** 全局组件类型声明 */
declare module 'vue' {
export interface GlobalComponents {
'vk-data-goods-sku-popup': SkuPopup
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,474 @@
<!-- 步进器 -->
<template>
<view class="vk-data-input-number-box">
<view
class="u-icon-minus"
:class="{ 'u-icon-disabled': disabled || inputVal <= min }"
:style="{
background: bgColor,
height: inputHeight + 'rpx',
color: color,
fontSize: size + 'rpx',
minHeight: '1.4em',
}"
@click="emptyClick"
@touchstart.prevent="btnTouchStart('minus')"
@touchend.stop.prevent="clearTimer"
>
<view :style="'font-size:' + (Number(size) + 10) + 'rpx'" class="num-btn"></view>
</view>
<input
v-model="inputVal"
:disabled="disabledInput || disabled"
:cursor-spacing="getCursorSpacing"
:class="{ 'u-input-disabled': disabled }"
class="u-number-input"
type="number"
:style="{
color: color,
fontSize: size + 'rpx',
background: bgColor,
height: inputHeight + 'rpx',
width: inputWidth + 'rpx',
}"
@blur="onBlur"
@click="showInput = true"
/>
<view
class="u-icon-plus"
:class="{ 'u-icon-disabled': disabled || inputVal >= max }"
:style="{
background: bgColor,
height: inputHeight + 'rpx',
color: color,
fontSize: size + 'rpx',
minHeight: '1.4em',
}"
@click="emptyClick"
@touchstart.prevent="btnTouchStart('plus')"
@touchend.stop.prevent="clearTimer"
>
<view :style="'font-size:' + (Number(size) + 10) + 'rpx'" class="num-btn"></view>
</view>
</view>
</template>
<script>
/* eslint-disable */
/**
* numberBox 步进器此为uview组件改造
* @description 该组件一般用于商城购物选择物品数量的场景注意该输入框只能输入大于或等于0的整数不支持小数输入
* @tutorial https://www.uviewui.com/components/numberBox.html
* @property {Number} value 输入框初始值默认1
* @property {String} bg-color 输入框和按钮的背景颜色默认#F2F3F5
* @property {Number} min 用户可输入的最小值默认0
* @property {Number} max 用户可输入的最大值默认99999
* @property {Number} step 步长每次加或减的值默认1
* @property {Number} stepFirst 步进值首次增加或最后减的值(默认step值和一致
* @property {Boolean} disabled 是否禁用操作禁用后无法加减或手动修改输入框的值默认false
* @property {Boolean} disabled-input 是否禁止输入框手动输入值默认false
* @property {Boolean} positive-integer 是否只能输入正整数默认true
* @property {String | Number} size 输入框文字和按钮字体大小单位rpx默认26
* @property {String} color 输入框文字和加减按钮图标的颜色默认#323233
* @property {String | Number} input-width 输入框宽度单位rpx默认80
* @property {String | Number} input-height 输入框和按钮的高度单位rpx默认50
* @property {String | Number} index 事件回调时用以区分当前发生变化的是哪个输入框
* @property {Boolean} long-press 是否开启长按连续递增或递减(默认true)
* @property {String | Number} press-time 开启长按触发后每触发一次需要多久单位ms(默认250)
* @property {String | Number} cursor-spacing 指定光标于键盘的距离避免键盘遮挡输入框单位rpx默认200
* @event {Function} change 输入框内容发生变化时触发对象形式
* @event {Function} blur 输入框失去焦点时触发对象形式
* @event {Function} minus 点击减少按钮时触发(按钮可点击情况下)对象形式
* @event {Function} plus 点击增加按钮时触发(按钮可点击情况下)对象形式
* @example <vk-data-input-number-box :min="1" :max="100"></vk-data-input-number-box>
*/
export default {
name: "vk-data-input-number-box",
emits: ["update:modelValue", "input", "change", "blur", "plus", "minus"],
props: {
//
value: {
type: Number,
default: 1
},
modelValue: {
type: Number,
default: 1
},
//
bgColor: {
type: String,
default: "#FFFFFF"
},
//
min: {
type: [Number, null],
default: 0
},
//
max: {
type: [Number, null],
default: 99999
},
//
step: {
type: [Number, null],
default: 1
},
//
stepFirst: {
type: [Number, null],
default: 0
},
// step
stepStrictly: {
type: Boolean,
default: false
},
//
disabled: {
type: Boolean,
default: false
},
// inputrpx
size: {
type: [Number, String, null],
default: 26
},
//
color: {
type: String,
default: "#323233"
},
// inputrpx
inputWidth: {
type: [Number, String, null],
default: 80
},
// inputrpx
inputHeight: {
type: [Number, String, null],
default: 50
},
// index使numberbox使forindex
index: {
type: [Number, String],
default: ""
},
// disabledOR
// disabledfalsedisabledInputtrue
disabledInput: {
type: Boolean,
default: false
},
//
cursorSpacing: {
type: [Number, String, null],
default: 100
},
//
longPress: {
type: Boolean,
default: true
},
//
pressTime: {
type: [Number, String, null],
default: 250
},
// 0()
positiveInteger: {
type: Boolean,
default: true
}
},
watch: {
valueCom(v1, v2) {
// valueinputVal
if (!this.changeFromInner) {
this.inputVal = v1;
// inputValthis.handleChange()changeFromInnertrue
// this.$nextTick
// changeFromInnerfalse
this.$nextTick(function () {
this.changeFromInner = false;
});
}
},
inputVal(v1, v2) {
//
if (v1 == "") return;
let value = 0;
// minmax使
let tmp = this.isNumber(v1);
if (tmp && v1 >= this.min && v1 <= this.max) value = v1;
else value = v2;
// 0
if (this.positiveInteger) {
// 0
if (v1 < 0 || String(v1).indexOf(".") !== -1) {
value = v2;
// input使$nextTick
this.$nextTick(() => {
this.inputVal = v2;
});
}
}
// change
this.handleChange(value, "change");
},
min(v1) {
if (v1 !== undefined && v1 != "" && this.valueCom < v1) {
this.$emit("input", v1);
this.$emit("update:modelValue", v1);
}
},
max(v1) {
if (v1 !== undefined && v1 != "" && this.valueCom > v1) {
this.$emit("input", v1);
this.$emit("update:modelValue", v1);
}
}
},
data() {
return {
inputVal: 1, // 使propsvalueprops
timer: null, //
changeFromInner: false, //
innerChangeTimer: null, //
showInput: false
};
},
created() {
this.inputVal = Number(this.valueCom);
},
computed: {
valueCom() {
// #ifdef VUE2
return this.value;
// #endif
// #ifdef VUE3
return this.modelValue;
// #endif
},
getCursorSpacing() {
// px
return Number(uni.upx2px(this.cursorSpacing));
}
},
methods: {
// PCH5click
emptyClick() {},
//
btnTouchStart(callback) {
// clearTimer
this[callback]();
//
if (!this.longPress) return;
clearInterval(this.timer); //
this.timer = null;
this.timer = setInterval(() => {
//
this[callback]();
}, this.pressTime);
},
//
clearTimer() {
this.$nextTick(() => {
clearInterval(this.timer);
this.timer = null;
});
},
//
minus() {
this.computeVal("minus");
},
//
plus() {
this.computeVal("plus");
},
//
calcPlus(num1, num2) {
let baseNum, baseNum1, baseNum2;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2; //
return ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision);
},
//
calcMinus(num1, num2) {
let baseNum, baseNum1, baseNum2;
try {
baseNum1 = num1.toString().split(".")[1].length;
} catch (e) {
baseNum1 = 0;
}
try {
baseNum2 = num2.toString().split(".")[1].length;
} catch (e) {
baseNum2 = 0;
}
baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
let precision = baseNum1 >= baseNum2 ? baseNum1 : baseNum2;
return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
},
computeVal(type) {
uni.hideKeyboard();
if (this.disabled) return;
let value = 0;
// stepFirst
//
if (type === "minus") {
if (this.stepFirst > 0 && this.inputVal == this.stepFirst) {
value = this.min;
} else {
value = this.calcMinus(this.inputVal, this.step);
}
} else if (type === "plus") {
if (this.stepFirst > 0 && this.inputVal < this.stepFirst) {
value = this.stepFirst;
} else {
value = this.calcPlus(this.inputVal, this.step);
}
}
if (this.stepStrictly) {
let strictly = value % this.step;
if (strictly > 0) {
value -= strictly;
}
}
if (value > this.max) {
value = this.max;
} else if (value < this.min) {
value = this.min;
}
// stepFirst
this.inputVal = value;
this.handleChange(value, type);
},
//
onBlur(event) {
let val = 0;
let value = event.detail.value;
// 0-90min
// props min0
if (!/(^\d+$)/.test(value) || value[0] == 0) val = this.min;
val = +value;
// stepFirst
if (this.stepFirst > 0 && this.inputVal < this.stepFirst && this.inputVal > 0) {
val = this.stepFirst;
}
// stepFirst
if (this.stepStrictly) {
let strictly = val % this.step;
if (strictly > 0) {
val -= strictly;
}
}
if (val > this.max) {
val = this.max;
} else if (val < this.min) {
val = this.min;
}
this.$nextTick(() => {
this.inputVal = val;
});
this.handleChange(val, "blur");
},
handleChange(value, type) {
if (this.disabled) return;
//
if (this.innerChangeTimer) {
clearTimeout(this.innerChangeTimer);
this.innerChangeTimer = null;
}
// inputv-model
this.changeFromInner = true;
// changeFromInner
// value
this.innerChangeTimer = setTimeout(() => {
this.changeFromInner = false;
}, 150);
this.$emit("input", Number(value));
this.$emit("update:modelValue", Number(value));
this.$emit(type, {
// Number
value: Number(value),
index: this.index
});
},
/**
* 验证十进制数字
*/
isNumber(value) {
return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
}
}
};
</script>
<style lang="scss" scoped>
.vk-data-input-number-box {
display: inline-flex;
align-items: center;
box-sizing: border-box;
}
.u-number-input {
position: relative;
text-align: center;
padding: 0;
margin: 0rpx;
display: flex;
align-items: center;
justify-content: center;
border: 2rpx solid #f4f4f4;
border-left: 0;
border-right: 0;
box-sizing: border-box;
}
.u-icon-plus,
.u-icon-minus {
width: 60rpx;
display: flex;
justify-content: center;
align-items: center;
border: 2rpx solid #f4f4f4;
box-sizing: border-box;
}
.u-icon-plus {
border-radius: 0 8rpx 8rpx 0;
}
.u-icon-minus {
border-radius: 8rpx 0 0 8rpx;
}
.u-icon-disabled {
color: #c8c9cc !important;
background-color: #f2f3f5 !important;
}
.u-input-disabled {
color: #c8c9cc !important;
background-color: #f2f3f5 !important;
}
.num-btn {
font-weight: 550;
line-height: 50rpx;
}
</style>

View File

@ -32,7 +32,7 @@
<view class="action">
<view class="item arrow">
<text class="label">选择</text>
<text class="text ellipsis"> 请选择商品规格 </text>
<text class="text ellipsis" @tap="isShowSkuPopup = true"> 请选择商品规格 </text>
</view>
<view class="item arrow" @tap="showPopup('address')">
<text class="label">送至</text>
@ -115,6 +115,14 @@
<AddressPanel v-if="popupType === 'address'" @close="handlerClose" />
<ServicePanel v-if="popupType === 'service'" @close="handlerClose" />
</uni-popup>
<!-- SKU 商品规格弹出层 -->
<vk-data-goods-sku-popup
ref="skuPopup"
:modelValue="isShowSkuPopup"
:localdata="localdata"
@onClose="isShowSkuPopup = false"
/>
</template>
</template>
@ -126,6 +134,7 @@ import { ref } from 'vue'
import ServicePanel from './components/ServicePanel.vue'
import AddressPanel from './components/AddressPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import type { SkuPopupLocaldata } from '@/components/vk-data-goods-sku-popup/vk-data-goods-sku-popup'
//
const { safeAreaInsets } = uni.getSystemInfoSync()
@ -143,6 +152,33 @@ const getGoods = async () => {
const res = await getGoodsAPI(query.id)
console.log(res.result)
goods.value = res.result
// SKU
localdata.value = {
_id: res.result.id,
name: res.result.name,
goods_thumb: res.result.mainPictures[0],
spec_list: res.result.specs.map((item) => {
return {
name: item.name,
list: item.values.map((value) => {
return {
name: value.name,
}
}),
}
}),
sku_list: res.result.skus.map((item) => {
return {
_id: item.id,
goods_id: res.result.id,
goods_name: res.result.name,
image: item.picture,
price: item.price * 100,
sku_name_arr: item.specs.map((spec) => spec.valueName),
stock: item.inventory,
}
}),
}
}
//
@ -182,6 +218,10 @@ onLoad(async () => {
await getGoods()
isLoading.value = false
})
// SKU
const isShowSkuPopup = ref(false)
const localdata = ref<SkuPopupLocaldata>({} as SkuPopupLocaldata)
</script>
<style lang="scss">