使用APICloud AVM框架封装SKU(商品规格选择)组件

简介: 本文介绍使用APICloud AVM框架封装SKU(商品规格选择)组件,该SKU组件可实现商品图片与颜色属性进行联动,通过颜色、版本来控制价格,总价通过购买数量与所选商品价格进行自动计算;可进行缺货设

本文介绍使用APICloud AVM框架封装SKU(商品规格选择)组件,该SKU组件可实现商品图片与颜色属性进行联动,通过颜色、版本来控制价格,总价通过购买数量与所选商品价格进行自动计算;可进行缺货设定。

上述功能点是通过商品数据结构和代码逻辑进行配合来实现的。

商品数据结构如下:

goodsList:[

{id:'100016015112',

image:'https://m.360buyimg.com/mobilecms/s750x750_jfs/t1/210630/17/8651/208682/618a5bd6Eddc8ea0e/b5e55e1a03bc0126.jpg!q80.dpg.web',

color:'亮黑色',

status:true,

guige:[

{label:'8G+128G',price:'3999',status:false},

{label:'8G+256G',price:'5999',status:true},

{label:'16G+512G',price:'6999',status:true},

{label:'16G+1024G',price:'9999',status:false}

]},

{id:'100016015113',

image:'https://img14.360buyimg.com/n4/jfs/t1/216079/14/3895/201095/618a5c0cEe0b9e2ba/cf5b98fb6128a09e.jpg',

color:'釉白色',

status:true,

guige:[

{label:'8G+128G',price:'3799',status:true},

{label:'8G+256G',price:'5799',status:true},

{label:'16G+512G',price:'6799',status:true},

{label:'16G+1024G',price:'9799',status:false}

]},

{id:'100016015132',

image:'https://img14.360buyimg.com/n4/jfs/t1/215845/12/3788/221990/618a5c4dEc71cb4c7/7bd6eb8d17830991.jpg',

color:'秘银色',

status:true,

            guige:[

{label:'8G+128G',price:'3599',status:true},

{label:'8G+256G',price:'5599',status:true},

{label:'16G+512G',price:'6599',status:true},

{label:'16G+1024G',price:'9599',status:false}

]},

{id:'200016015117',

image:'https://img14.360buyimg.com/n4/jfs/t1/203247/8/14659/237368/618a5c87Ecc968774/b0bb25331e5e2d1a.jpg',

color:'夏日胡杨',

            status:false,

guige:[

{label:'8G+128G',price:'3899',status:false},

{label:'8G+256G',price:'5899',status:false},

{label:'16G+512G',price:'6899',status:false},

{label:'16G+1024G',price:'9899',status:false}

]},

{id:'100013415456',

image:'https://img14.360buyimg.com/n4/jfs/t1/160950/40/25098/234168/618a5cb9E65ba975e/7f8f93ea7767a51b.jpg',

color:'冬日暖阳',

status:true,

            guige:[

{label:'8G+128G',price:'3199',status:true},

{label:'8G+256G',price:'5199',status:true},

{label:'16G+512G',price:'6199',status:true},

{label:'16G+1024G',price:'9199',status:false}

]}

]

每个商品的版本有多条数据,每条数据都对应不同的价格,同时会有字段标识是否有货。

每一个商品也会有字段标识是否有货。

当切换商品属性时,需要通过函数进行判断,以保证在商品缺货的情况下,不能被选中。

setGoods(e){

// console.log(JSON.stringify(e));

let item = e.currentTarget.dataset.item;

if(item.status){

this.data.selectGoods = item;

this.data.guigeList = item.guige;

for (let index = 0; index < item.guige.length; index++) {

const element = item.guige[index];

         //保证默认选中的商品是有货的

if(element.status){

this.data.selectGuige = element;

break;

}

}

}

},

组件文件

easy-sku.stml

¥{selectGuige.price?selectGuige.price*count:0}

商品编号:{selectGoods.id}

颜色

{item.color}

版本

{item.label}

购买数量

+

export default {

name: 'easy-sku',

installed(){

this.data.selectGoods=this.props.goodsList[0];

// this.data.selectGuige=this.props.goodsList[0].guige[1];

this.data.guigeList = this.props.goodsList[0].guige;

},

props:{

goodsList:Array,

},

data() {

return{

ico_close:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAjUlEQVR42mNgGNlg1qxZbTNnzrxFgvqnQJyLUwHQsKtA/B+InxEyDKjmI0gt0MD9hBQ+ImQozDAgvkyUV/AZSrJh+Awl2zBshlJsGBZDKTcMmjR+Ihn4kVqGHQS5jiJDkQ1D8j55hmIzjGxD8RlGsqFABe8IGYbF0Pv4FAUAXbiIhCS1ac6cOXYjvEwFAL7uy3s7L72GAAAAAElFTkSuQmCC',

selectGoods:{},

selectGuige:{},

count:1,

guigeList:[]

}

},

methods: {

setGoods(e){

// console.log(JSON.stringify(e));

let item = e.currentTarget.dataset.item;

if(item.status){

this.data.selectGoods = item;

this.data.guigeList = item.guige;

for (let index = 0; index < item.guige.length; index++) {

const element = item.guige[index];

if(element.status){

this.data.selectGuige = element;

break;

}

}

}

},

setGuige(e){

// console.log(JSON.stringify(e));

let item = e.currentTarget.dataset.item;

if(item.status){

this.data.selectGuige = item;

}

},

countAdd(){

this.data.count++;

},

countDel(){

if(this.data.count>0){

this.data.count--;

}

},

cancel(){

this.fire('cancel','');

},

submit(){

this.fire('submit',{goods:this.data.selectGoods,guige:this.data.selectGuige,count:this.data.count});

}

}

}

.easy-sku_container {

position: absolute;

height: 100%;

width: 100%;

background-color: rgba(0,0,0,0.1);

}

.easy-sku_box{

align-items: center;

justify-content: space-between;

position: absolute;

bottom: 0;

width: 100%;

height: 70%;

background-color: #ffffff;

border-top-left-radius: 30px;

border-top-right-radius: 30px;

padding: 15px;

}

.easy-sku_base{

width: 100%;

padding: 15px;

}

.easy-sku_base-pic{

width: 80px;

height: 80px;

}

.easy-sku_base{

flex-flow: row nowrap;

justify-content: space-between;

align-items: flex-end;

}

.easy-sku_base-info{

flex: 1;

padding-left: 10px;

}

.easy-sku_base-info-price{

color: #fa2c19;

font-size: 16px;

}

.easy-sku_base-info-num{

color: #666666;

font-size: 12px;

}

.easy-sku_close-box{

height: 100%;

}

.easy-sku_close-ico{

width: 20px;

}

.easy-sku_scroll-box{

width: 100%;

flex: 1;

}

.easy-sku_guige-box{

width: 100%;

}

.easy-sku_guige-title{

padding: 10px 0;

}

.easy-sku_guige-title-label{

font-size: 16px;

font-weight: bolder;

}

.easy-sku_guige-item-box{

flex-flow: row wrap;

justify-content: flex-start;

align-items: center;

}

.easy-sku_guige-item--default{

background-color: #f2f2f2;

border: 0.5px solid #f2f2f2;

border-radius: 18px;

padding: 8px 18px;

margin-right: 10px;

margin-bottom: 10px;

}

.easy-sku_guige-item-label--default{

font-size: 14px;

color: #333333;

}

.easy-sku_guige-item--select{

background-color: #fee0dd;

border: 0.5px solid #fa2c19;

border-radius: 18px;

padding: 8px 18px;

margin-right: 10px;

margin-bottom: 10px;

}

.easy-sku_guige-item-label-select{

font-size: 14px;

color: #fa2c19;

}

.easy-sku_guige-item-label-disable{

font-size: 14px;

color: #b5b5b5;

/ text-decoration: line-through; /

}

.easy-sku_count-box{

width: 100%;

flex-flow: row nowrap;

justify-content: space-between;

align-items: center;

margin-top: 10px;

}

.easy-sku_count-result-tool{

flex-flow: row nowrap;

justify-content: space-between;

align-items: center;

}

.easy-sku_count-result-item{

font-size: 25px;

padding: 0 5px;

color: #333333;

}

.easy-sku_count-result-item--disable{

font-size: 25px;

padding: 0 5px;

color: #b5b5b5;

}

.easy-sku_count-result-input{

background-color: #f0f0f0;

border: 0;

height: 18px;

width: 40px;

text-align: center;

border-radius: 5px;

}

.easy-sku_count-label{

font-size: 16px;

color: #333333;

}

.easy-sku_bottom{

width: 100%;

}

.easy-sku_btn{

background-color: #fa2c19;

border-radius: 21px;

font-size: 15px;

color: #fff;

padding: 10px;

}

示例文件

demo-easy-sku.stml

选择商品

<easy-sku

oncancel="closeSKU"

onsubmit="getSKU"

:goodsList="goodsList"

v-if="isShow"

import '../../components/easy-sku.stml'

export default {

name: 'demo-easy-sku',

apiready(){//like created

},

data() {

return{

isShow:false,

goodsList:[

{id:'100016015112',

image:'https://m.360buyimg.com/mobilecms/s750x750_jfs/t1/210630/17/8651/208682/618a5bd6Eddc8ea0e/b5e55e1a03bc0126.jpg!q80.dpg.web',

color:'亮黑色',

status:true,

guige:[

{label:'8G+128G',price:'3999',status:false},

{label:'8G+256G',price:'5999',status:true},

{label:'16G+512G',price:'6999',status:true},

{label:'16G+1024G',price:'9999',status:false}

]},

{id:'100016015113',

image:'https://img14.360buyimg.com/n4/jfs/t1/216079/14/3895/201095/618a5c0cEe0b9e2ba/cf5b98fb6128a09e.jpg',

color:'釉白色',

status:true,

guige:[

{label:'8G+128G',price:'3799',status:true},

{label:'8G+256G',price:'5799',status:true},

{label:'16G+512G',price:'6799',status:true},

{label:'16G+1024G',price:'9799',status:false}

]},

{id:'100016015132',

image:'https://img14.360buyimg.com/n4/jfs/t1/215845/12/3788/221990/618a5c4dEc71cb4c7/7bd6eb8d17830991.jpg',

color:'秘银色',

status:true,guige:[

{label:'8G+128G',price:'3599',status:true},

{label:'8G+256G',price:'5599',status:true},

{label:'16G+512G',price:'6599',status:true},

{label:'16G+1024G',price:'9599',status:false}

]},

{id:'200016015117',

image:'https://img14.360buyimg.com/n4/jfs/t1/203247/8/14659/237368/618a5c87Ecc968774/b0bb25331e5e2d1a.jpg',

color:'夏日胡杨',status:false,

guige:[

{label:'8G+128G',price:'3899',status:false},

{label:'8G+256G',price:'5899',status:false},

{label:'16G+512G',price:'6899',status:false},

{label:'16G+1024G',price:'9899',status:false}

]},

{id:'100013415456',

image:'https://img14.360buyimg.com/n4/jfs/t1/160950/40/25098/234168/618a5cb9E65ba975e/7f8f93ea7767a51b.jpg',

color:'冬日暖阳',

status:true,guige:[

{label:'8G+128G',price:'3199',status:true},

{label:'8G+256G',price:'5199',status:true},

{label:'16G+512G',price:'6199',status:true},

{label:'16G+1024G',price:'9199',status:false}

]}

],

}

},

methods: {

openSKU(){

this.data.isShow=true;

},

closeSKU(){

this.data.isShow=false;

},

getSKU(e){

console.log(JSON.stringify(e));

api.toast({

msg:'颜色:'+e.detail.goods.color+'/规格:'+e.detail.guige.label+'/数量:'+e.detail.count+'/总价:'+e.detail.count*e.detail.guige.price,

location:'middle'

})

this.data.isShow = false;

}

}

}

.page {

height: 100%;

background-color: #f6f6f6;

}

.item{

background-color: #ffffff;

margin: 15px;

padding: 15px;

border-radius: 5px;

}

点击立即使用AVM框架

目录
相关文章
|
前端开发
UniApp 中的 image 属性讲解
UniApp 中的 image 属性讲解
1721 2
|
IDE Linux 开发工具
如何在Linux运行RStudio Server并实现Web浏览器远程访问
如何在Linux运行RStudio Server并实现Web浏览器远程访问
908 0
|
11月前
|
JavaScript 前端开发 定位技术
Vue项目中的虚拟滚动:提升页面渲染性能的最佳实践
本文介绍虚拟滚动技术及其在Vue项目中的应用,通过vue-virtual-scroller实现大数据量下长列表的高性能渲染,提升页面流畅度与用户体验,适用于地图轨迹等业务场景。
2063 0
|
IDE 开发工具 索引
在Python中安装第三方库
在Python中安装第三方库
3513 156
|
11月前
|
机器学习/深度学习 监控 算法
基于 YOLOv8 的多车型交通车辆实时检测识别项目 [目标检测完整源码]
基于YOLOv8的多车型交通车辆实时检测系统,支持12类车辆识别,集成PyQt5图形界面,可处理图片、视频、摄像头等多源输入。具备高精度、易部署、可扩展等特点,适用于智慧交通、车流统计与AI教学实践,实现从算法到应用的完整落地。
777 20
基于 YOLOv8 的多车型交通车辆实时检测识别项目 [目标检测完整源码]
|
存储 缓存 JavaScript
【Uniapp 专栏】深入剖析 Uniapp 的运行机制原理
【5月更文挑战第12天】Uniapp是一款基于Vue.js的跨平台前端框架,通过抽象不同平台的差异,实现一套代码多平台运行,提升开发效率。其架构包括视图层(基于Vue.js组件)、逻辑层(JavaScript处理业务逻辑)和数据层(管理应用数据)。运行时,Uniapp会根据目标平台转换代码并适配。关键技术包括Web标准、原生插件和运行时环境。优化措施包含代码分包、数据懒加载和缓存机制。借助HBuilderX等工具,开发者能高效地进行开发和测试。Uniapp的运行机制融合多种技术,为跨平台应用开发提供便利。
1656 2
【Uniapp 专栏】深入剖析 Uniapp 的运行机制原理
|
Java 程序员 Linux
Maven的基本安装与使用
Maven的基本安装与使用
757 6
|
监控 安全 Java
五大权限系统模型该如何选择?
五大权限系统模型该如何选择?
1528 0
|
小程序 JavaScript
微信小程序使用echarts图表(ec-canvas)
这篇文章介绍了在微信小程序中使用`ec-canvas`集成echarts图表的方法,包括解决加载时报错的问题、配置图表组件、以及在小程序页面中引入和使用这些图表组件的步骤。
3102 1
微信小程序使用echarts图表(ec-canvas)
|
数据挖掘
uniapp uview扩展u-picker支持日历期间 年期间 月期间 时分期间组件
uniapp uview扩展u-picker支持日历期间 年期间 月期间 时分期间组件
1719 10