资讯专栏INFORMATION COLUMN

微信小程序中遇到的多规格问题(一)

DrizzleX / 2968人阅读

摘要:刚进入公司的时候就有遇到过多规格的问题,当时就觉得很麻烦,就只是看了下,没有尝试,最近在写微信小程序的时候,又遇到了多规格问题,就自己尝试了下,在这里记录下效果图文件及部分思路文件规格颜色颜色颜色库存规格数量确定文件

刚进入公司的时候就有遇到过多规格的问题,当时就觉得很麻烦,就只是看了下,没有尝试,最近在写微信小程序的时候,又遇到了多规格问题,就自己尝试了下,在这里记录下
1.效果图
2.文件及部分思路

index.wxml文件


  
    规格:
    颜色    
    颜色    
    颜色    
  
  



  
    
      
    
    
      
        {{standardObject.price}}
        
      
      库存:{{standardObject.store_count}}
      规格: {{mergeStandard}}
    
  
  
    {{item[0].spec_name}}
    
      
        {{item.item}}        
      
    
    
  
  
    数量
    
      
    
  
  
    确定
  

index.wxss文件

.multipleStandard{
  display: flex;
  margin-top: 20rpx;
  padding: 15rpx 20rpx;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 0 10px #ccc;
}
.item{
  margin-right: 15rpx;
}
.arrows{
  width: 16rpx;
  height: 27rpx;
}

/* 规格弹窗 */
.selectStandard {
  width: 100%;
  height: 1000rpx;
  background-color: #fff;
  position: fixed;
  z-index: 333;
  bottom: -600px;
  border-top-left-radius: 20rpx;
  border-top-right-radius: 20rpx;
}

/* 头部选中的规格 */
.tophead {
  display: flex;
  margin: 30rpx 3%;
  width: 94%;
  align-items: center;
  text-align: center;
}

.topimg {
  width: 200rpx;
  height: 200rpx;
}

.topimg image {
  width: 100%;
  height: 100%;
  border-radius: 20rpx;
  background-color: red;
}

.topright {
  margin-left: 30rpx;
  font-size: 28rpx;
  text-align: left;
  width: 66%;
}
/* 关闭按钮 */
.selectClose {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.selectClose image {
  width: 30rpx;
  height: 30rpx;
}

.standard {
  margin: 0 3%;
  width: 94%;
  font-size: 28rpx;
}

.standardTitle {
  font-size: 30rpx;
  margin-top: 20rpx;
}
.standardItem {
  display: flex;
  flex-wrap: wrap;
    width: 100%;
}
.selectItem {
  border: solid 1px #666;
  margin: 20rpx;
  padding: 5rpx 23rpx;
  border-radius: 16rpx;
}
/* 规格选中时样式 */
.standardSelected{
  color: #fff;
  background: #f26740;
    background-color:#fe6732;
    border:solid 1px #fe6732;
    margin:20rpx;
    padding:5rpx 23rpx;
    border-radius:16rpx;
}
.standardNormal{
  color: #000;
}
.standardDisable{
  color: #eee;
}

/* 选择的数量 */
.selectCount {
  margin: 30rpx 3%;
  width: 94%;
  font-size: 28rpx;
}
.countright {
  float: right;
  display: flex;
  text-align: center;
  align-items: center;
}

.countname {
  margin-bottom: 20rpx;
  font-size: 30rpx;
}

.submitBtn {
  position: absolute;
  bottom: 0;
  width: 70%;
  margin: 0 15% 20rpx 15%;
  text-align: center;
  z-index: 66;
  height: 80rpx;
  background-color: #fe6732;
  border-radius: 40rpx;
  color: white;
  font-size: 32rpx;
  line-height: 80rpx;
}

index.js文件

const {
    $Message
} = require("../../component/iview/base/index");
Page({
  data: {
    animationData: {},
    isSelect: ["standardNormal", "standardSelected", "standardDisable"],  /* 用于区别当前的规格是否选中 */
    goods: {
      goods_name: "男鞋",
      store_count: 158,
      cost_price: "10.00",
      original_img: "/images/commodity.jpg",     
    },
    commodityStandard: [
      [
        {
          spec_name: "颜色",
          item_id: 535385,
          item: "白色",
          src: "",
          isClick: 0
        },
        {
          spec_name: "颜色",
          item_id: 535386,
          item: "黑色",
          src: "",
          isClick: 0
        }
      ],
      [
        {
          spec_name: "尺寸",
          item_id: 535692,
          item: "170",
          src: "",
          isClick: 0
        },
        {
          spec_name: "尺寸",
          item_id: 535693,
          item: "180",
          src: "",
          isClick: 0
        }
      ],
      [
        {
          spec_name: "重量",
          item_id: 552569,
          item: "11",
          src: "",
          isClick: 0
        },
        {
          spec_name: "重量",
          item_id: 552570,
          item: "15",
          src: "",
          isClick: 0
        }
      ]
    ],
    standardInfo: [
      {
        id: 1018269,
        key: "535385_535692_552569",
        price: "10.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018270,
        key: "535385_535692_552570",
        price: "20.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018271,
        key: "535385_535693_552569",
        price: "30.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018272,
        key: "535385_535693_552570",
        price: "40.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018273,
        key: "535386_535692_552569",
        price: "50.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018274,
        key: "535386_535692_552570",
        price: "60.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018275,
        key: "535386_535693_552569",
        price: "70.00",
        productprice: "0.00",
        store_count: 20
      },
      {
        id: 1018276,
        key: "535386_535693_552570",
        price: "80.00",
        productprice: "0.00",
        store_count: 18
      }
    ],
    selectedId: [],
    selectedStandard: [],
    standardObject: {},
    commodityNum: 1,
  },
  onLoad: function (options) {
    let goods = this.data.goods;
    let standardObject = this.data.standardObject;
    standardObject.price = goods.cost_price;
    standardObject.store_count = goods.store_count;
        let store_count = goods.store_count;
    this.setData({
      standardObject,
            store_count
    })
  },
  /* 规格选择 */
  standardSelect() {
    var that = this
    var animal1 = wx.createAnimation({
      timingFunction: "ease-in"
    }).translate(0, -600).step({
      duration: 300
    })
    that.setData({
      animationData: animal1.export(),
    })
  },
  /* 关闭规格选择 */
  handleClose() {
    var that = this
    var animal1 = wx.createAnimation({
      timingFunction: "ease-in"
    }).translate(0, 600).step({
      duration: 300
    })
    that.setData({
      animationData: animal1.export()
    })
  },
  /* 每个规格的点击事件 */
  handleStandardClick(e) {
    let id = e.currentTarget.dataset.id;
    // 总规格名称索引
    let standardIndex = e.currentTarget.dataset.standardIndex;
    // 单个规格名称索引
    let index = e.currentTarget.dataset.index;
    let commodityStandard = this.data.commodityStandard;
    let standardLength = commodityStandard[standardIndex].length;

    // 用于存储规格的id
    let selectedId = this.data.selectedId;
    // 用总规格名称索引来存储每个选中的规格id
    selectedId[standardIndex] = id;

    let selectedStandard = this.data.selectedStandard;

    // 在点击的时候,只需要对点击的这个规格所在的数组进行循环
    for (let i = 0; i < standardLength; i++) {
      // 找到对应的单个规格索引,并设置isClick及单个规格名称
      if (index == i) {
        commodityStandard[standardIndex][index].isClick = 1;
        selectedStandard[standardIndex] = commodityStandard[standardIndex][index].item;
      } else {
        commodityStandard[standardIndex][i].isClick = 0;
      }
    }

    // 将id用_连接起来
    let mergeId = selectedId.join("_");
    console.log(mergeId);
    let mergeStandard = selectedStandard.join("  ");
    console.log(mergeStandard);

    let standardInfo = this.data.standardInfo;
    let standardInfoLength = standardInfo.length;
    // 用于存储选中的规格
    let standardObject = {};

    for (let i = 0; i < standardInfoLength; i++) {
      if (standardInfo[i].key == mergeId) {
        standardObject = standardInfo[i];
        break;
      } else {
        standardObject = this.data.standardObject;
      }
    }

    this.setData({
      currentId: id,
      commodityStandard,
      selectedId,
      standardObject,
      mergeStandard,
      selectedStandard,
    })

  },
  /* 选择数量 */
  handleCommodityNumber(e) {
      let commodityNum = e.detail.value;
      if (commodityNum >= this.data.store_count) {
          commodityNum = this.data.store_count;
      }
      this.setData({
          commodityNum
      })
  },
  /* 保存选择的规格 */
  submitSelected() {
      let selectedStandard = this.data.selectedStandard;
      let length = selectedStandard.length;
      console.log(length)
      if (length == 0) {
          $Message({
              content: "请选择规格",
              type: "error"
          });
          return false;
      }
      for (let i = 0; i < length; i++) {
          if (length < this.data.commodityStandard.length) {
              $Message({
                  content: "请选择规格",
                  type: "error"
              });
              break;
          }
          if (selectedStandard[i] == undefined) {
              $Message({
                  content: "请选择规格",
                  type: "error"
              });
              break;
          }
      }
      $Message({
          content: "选择成功",
          type: "success"
      });
  },
})

我这里提示信息使用了插件iview,可以在官网直接下载后使用

iview weapp
3.部分思路及改进方法
开始想到的是将需合并的id的位置写死,比如,第一个位置就传第一个规格里选中的规格id,第二个位置就传第二个规格里选中的规格id,不过此时有问题,就是后台的id拼接是根据当前规格长度来拼接的,从最短的开始往长的拼接,然后,长度相等的时候,我这边的话,后台是从第一个开始拼接的,而且你选规格时,也可能是随机点的,此时若是采用第一种位置写死的方法就会有问题,因为会找不到对应的合并后的规格id

因为我这里一开始用的数据就刚好满足我的设想,但是后面换了个数据后就出现问题了

index.wxml文件也做部分修改,可以直接查找下,类名为standardItem


  
    {{item.item}}        
  

*增加另一种模拟数据

goods: {
  goods_name: "男鞋",
  store_count: 95,
  market_price: "10.00",
  shop_price: "101.00",
  cost_price: "10.00",
  original_img: "/images/commodity.jpg",
  store_id: 170,
},
commodityStandard: [
  [
    {
      spec_name: "颜色",
      item_id: 532825,
      item: "白色",
      src: "",
      isClick: 0
    },
    {
      spec_name: "颜色",
      item_id: 532826,
      item: "黑色",
      src: "",
      isClick: 0
    },
    {
      spec_name: "颜色",
      item_id: 532827,
      item: "红色",
      src: "",
      isClick: 0
    }
  ],
  [
    {
      spec_name: "大小",
      item_id: 532828,
      item: "160",
      src: "",
      isClick: 0
    },
    {
      spec_name: "大小",
      item_id: 532829,
      item: "150",
      src: "",
      isClick: 0
    }
  ],
  [
    {
      spec_name: "重量",
      item_id: 552581,
      item: "10",
      src: "",
      isClick: 0
    }
  ]
],
/* 这里合并规格的信息 */
spec_goods_price: [
  {
    id: 1018286,
    key: "552581_532828_532825",
    price: "10.00",
    productprice: "0.00",
    store_count: 9
  },
  {
    id: 1018287,
    key: "552581_532828_532826",
    price: "20.00",
    productprice: "0.00",
    store_count: 10
  },
  {
    id: 1018288,
    key: "552581_532828_532827",
    price: "30.00",
    productprice: "0.00",
    store_count: 10
  },
  {
    id: 1018289,
    key: "552581_532829_532825",
    price: "40.00",
    productprice: "0.00",
    store_count: 10
  },
  {
    id: 1018290,
    key: "552581_532829_532826",
    price: "50.00",
    productprice: "0.00",
    store_count: 7
  },
  {
    id: 1018291,
    key: "552581_532829_532827",
    price: "60.00",
    productprice: "0.00",
    store_count: 10
  }
],

index.js文件

onLoad: function (options) {
    let goods = this.data.goods;
    /* 用于存储对应的价格及库存 */
    let standardObject = this.data.standardObject;
    standardObject.price = goods.cost_price;
    standardObject.store_count = goods.store_count;
    
    let spec_goods_price =this.data.spec_goods_price;
    if (spec_goods_price) {
      this.checkPrice(spec_goods_price);
    }
    this.setData({
      standardObject,
    })
},
handleStandardClick: function (e) {
    // 总规格名称索引
    let standardIndex = e.currentTarget.dataset.standardIndex;
    let id = e.currentTarget.dataset.id;
    /* 存储选中的规格名称 */
    let name = e.currentTarget.dataset.name;
    let selectedStandard = this.data.selectedStandard;
    selectedStandard[standardIndex] = name;
    let mergeStandard = selectedStandard.join("  ");
    let Specifications = this.data.Specifications;
    Specifications[standardIndex] = id;
    console.log(mergeStandard)
    this.setData({
      Specifications,
      mergeStandard,
      selectedStandard,
    })
    this.checkPrice(this.data.spec_goods_price);
},
/*在还未选择完规格时,暂时选用第一个合并后的规格价格及库存*/
checkPrice: function (spec_goods_price) {
    let standardObject = this.data.standardObject;
    if (!this.checkSpecifications(spec_goods_price)) {
      standardObject.price = spec_goods_price[0].price;
      standardObject.store_count = spec_goods_price[0].store_count;
      this.setData({
        standardObject,
      })
    }
},
/* 保存及校验是否选好了规格 */
submitSelected: function (e) {
    let spec_goods_price = this.data.spec_goods_price;
    let i = 0;
    let optionid = "";
    if (spec_goods_price) {
        optionid = this.checkSpecifications();
        if (optionid) {
            $Message({
              content: "选择成功",
              type: "success"
            });
        } else {
            $Message({
              content: "请选择规格",
              type: "error"
            });
        }
    }
},
checkSpecifications(spec_goods) {
    let spec_goods_price = spec_goods || this.data.spec_goods_price;
    let Specifications = this.data.Specifications;
    let SpecificationsLength = spec_goods_price[0].key.split("_").length;
    let standardObject = this.data.standardObject;
    if (Specifications.length != SpecificationsLength) {
      return false;
    } else {
      for (let i = 0; i < spec_goods_price.length; i++) {
        /* 若selectSpecifications全为true,则选中了对应的合并后的规格 */
        let selectSpecifications = true;
        for (let j = 0; j < Specifications.length; j++) {
          if (spec_goods_price[i].key.indexOf(Specifications[j]) == -1) {
            selectSpecifications = false;
            break;
          }
        }
        if (selectSpecifications) {
          standardObject.price = spec_goods_price[i].price;
          standardObject.store_count = spec_goods_price[i].store_count;
          this.setData({
            standardObject,
          })
          return spec_goods_price[i].id;
        }
      }
    }
    return false;
},

利用indexOf来判断合并后的key值,就不用通过写死位置存储规格,然后这里用了动画,可以自己查看官方完档微信创建动画

这里的规格啥的,格式可能会是多样的,我这里的话,就是返回这样的格式,其实我还想实现就是根据库存来判断是否可以点击,比如选尺码28,然后可能颜色为黑色的就库存不足,此时,是不能让他点击的,日后会在做下这个功能,欢迎大家在评论区指正,共同进步^_^

正在努力学习中,若对你的学习有帮助,留下你的印记呗(点个赞咯^_^)

往期好文推荐:

判断ios和Android及PC端

纯css实现瀑布流(multi-column多列及flex布局)

实现文字的省略号

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98988.html

相关文章

  • 信小程序之购物车和父子组件传值及calc的注意事项

    摘要:在做微信小程序时,觉得小组里对购物车的实现不是很完美,就自己尝试的写了下,然后用到了父子组件传值,父子组件传值的话,和框架上是非常相似的,以及这个函数,有个注意点,自己不怎么用,一时间有差点忘了,这里记录下效果图子组件实现要实现图中删除的效 在做微信小程序时,觉得小组里对购物车的实现不是很完美,就自己尝试的写了下,然后用到了父子组件传值,父子组件传值的话,和vue框架上是非常相似的,以...

    helloworldcoding 评论0 收藏0
  • 监控信小程序wx.request请求失败

    摘要:微信小程序运维中心提供了错误日志记录,但功能还是比较有限。有时候一个微信小程序可能会用到多个第三方服务,从多个域名获取数据。要使用监控,你需要去网站注册账号并创建一个微信小程序监控项目,然后按照提示接入插件。 在微信小程序里,与后台服务器交互的主要接口函数是wx.request(),用于发起 HTTPS 网络请求。其重要性不言而喻。然而,却经常遇到请求失败的问题,笔者特意谷歌wx.re...

    lpjustdoit 评论0 收藏0
  • Java 初学者做的第信小程序总结--关于Java基础

    摘要:官方资料微信公众平台注册小程序。官网开发文档社区开发工具部署微信小程序微信小程序本身不需要部署,在微信开发工具中直接上传代码就行。 为什么 学习 Java 三年,目前已经工作了2年,因为自学,基础差,所以打算年末总结一下常见的基础知识和面试点; 也可以通过独立做一个项目整合自己工作期间学习的知识,加深印象。 但是想着回家或是平时手机用的多,做一款APP和小程序很方便查看。 项目展示 本...

    mudiyouyou 评论0 收藏0
  • 教你如何键反编译获取任何信小程序源代码(图形化界面,傻瓜式操作)

    摘要:很想一窥源码,查看究竟,看看大厂的前端大神们是如何规避了小程序的各种奇葩的坑。头发减减可是问题是呐好看的小程序,又获得不到源代码。所以啊,才能容易的反编译出来,如果是那就很难了一键获取微信小程序源代码   1 Tips: 2   一键获取微信小程序源码, 使用了C#加nodejs制作 直接解压在D盘根目录下后就可以使用 将小程序文件放到 wxapkg目录下3 这个目录下有一些demo...

    RyanQ 评论0 收藏0
  • Laravel 教程 - 实战 iBrand 开源电商 API 系统

    摘要:最佳实践良好的编码规范单元测试持续集成文档,从一开始就形成良好的编码习惯。真实的电商业务所有的业务需求来自真实的客户,并且线上良好运营中。 重要通知: Laravel + 小程序的开源电商版本源码已经在 github 上拉,欢迎提交 issue 和 star :) 开源电商 Server 端: Laravel API源码 开源电商 client 端:小程序源码 iBrand 简介...

    iOS122 评论0 收藏0

发表评论

0条评论

DrizzleX

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<