前文:
今天踩了一下午的坑但是确实很简单的问题。
点击扫一扫 会在灰色区域展示 扫描的商品信息比如商品名称商品码等但是我们的需求是一物一码即使是同一个商品也是不同的商品码。
最开始我的想法是做判断因为我会在相对应的js文件中定义一个 productList:[ ],数组来存放数据
Pages({
productList: [用来存放通过后台接口得到的相关商品的数据信息]
})
由于我们是一物一码那唯一的判断条件就是商品码了
wzy.post("/wx/open/getProdcutNameByCode", product, true)
.then((res) => {
let products={
name: res.data.data,
code:product.code,
}
let productLength = this.data.productIist.length;
//如果列表没有直接推机关作风那唯一的判断条件就是商品码了
wzy.post("/wx/open/getProdcutNameByCode", product, true)
.then((res) => {
let products={
name: res.data.data,
code:product.code,
}
let productLength = this.data.productIist.length;
//如果列表没有直接推如果有循环如果
if (productLength==0){
this.data.productIist.push(products);
this.setData({
productIist: this.data.productIist
})
}else{
for (let i = 0; i < productLength;i++){
if (products.code == this.data.productIist[i].code){
global.jv.showPop('提示','同一商品不可重复扫描')
return
}
}
this.data.productIist.push(products);
this.setData({
productIist: this.data.productIist
})
}
}).catch((res) => {
console.log(res)
wzy.showPop('提示', '当前网络繁忙请重新扫描')
})
},
原来的思路是:
.then((res) => {
let products={
name: res.data.data,
code:product.code,
}
let productLength = this.data.productIist.length;
//如果列表没有直接推如果有循环如果
if (productLength==0){
this.data.productIist.push(products);
this.setData({
productIist: this.data.productIist
})
}else{
// 原来思路:把数组中的每一项code取出来与扫码得到的code进行对比如果不相等就push到数组中 从而在页面循环但是发现
// 当数组的length>1的情况下会发生即使你扫码得到的code不与原数组相同但是会重复多次次数由productIist.length决定
productIist.forEach(item=>{
if(item.code !==this.data.productIist.code ) {
this.data.productIist.push(products);
this.setData({
productIist: this.data.productIist
})
}
})
}).catch((res) => {
console.log(res)
wzy.showPop('提示', '当前网络繁忙请重新扫描')
})
},