欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

微信小程序之條件判斷 - 新聞資訊 - 云南小程序開發(fā)|云南軟件開發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開發(fā)/軟件開發(fā)

知識(shí)

不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!

您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >

微信小程序之條件判斷

發(fā)表時(shí)間:2021-3-31

發(fā)布人:葵宇科技

瀏覽次數(shù):45

前文:

今天踩了一下午的坑,但是確實(shí)很簡(jiǎn)單的問(wèn)題。

我說(shuō)一下需求:掃描商品的二維碼,從而判斷,同一個(gè)二維碼不可多次掃描;

點(diǎn)擊掃一掃 會(huì)在灰色區(qū)域展示 掃描的商品信息,比如商品名稱,商品碼等,但是我們的需求是一物一碼,即使是同一個(gè)商品也是不同的商品碼。

錯(cuò)誤示例:

最開始我的想法是做判斷,因?yàn)槲視?huì)在相對(duì)應(yīng)的js文件中定義一個(gè) productList:[ ],數(shù)組來(lái)存放數(shù)據(jù),

Pages({ 
productList: [用來(lái)存放,通過(guò)后臺(tái)接口得到的相關(guān)商品的數(shù)據(jù)信息]

}) 
由于我們是一物一碼,那唯一的判斷條件就是商品碼了

  1. wzy.post("/wx/open/getProdcutNameByCode", product, true)
  2. .then((res) => {
  3. let products={
  4. name: res.data.data,
  5. code:product.code,
  6. }
  7. let productLength = this.data.productIist.length;
  8. //如果列表沒(méi)有直接推,如果有循環(huán),如果
  9. if (productLength==0){
  10. this.data.productIist.push(products);
  11. this.setData({
  12. productIist: this.data.productIist
  13. })
  14. }else{
  15. for (let i = 0; i < productLength;i++){
  16. if (products.code == this.data.productIist[i].code){
  17. global.jv.showPop('提示','同一商品不可重復(fù)掃描')
  18. return
  19. }
  20. }
  21. this.data.productIist.push(products);
  22. this.setData({
  23. productIist: this.data.productIist
  24. })
  25. }
  26. }).catch((res) => {
  27. console.log(res)
  28. wzy.showPop('提示', '當(dāng)前網(wǎng)絡(luò)繁忙,請(qǐng)重新掃描')
  29. })
  30. },

原來(lái)的思路是:

  1. .then((res) => {
  2. let products={
  3. name: res.data.data,
  4. code:product.code,
  5. }
  6. let productLength = this.data.productIist.length;
  7. //如果列表沒(méi)有直接推,如果有循環(huán),如果
  8. if (productLength==0){
  9. this.data.productIist.push(products);
  10. this.setData({
  11. productIist: this.data.productIist
  12. })
  13. }else{
  14. // 原來(lái)思路:把數(shù)組中的每一項(xiàng)code取出來(lái)與掃碼得到的code進(jìn)行對(duì)比,如果不相等就push到數(shù)組中 從而在頁(yè)面循環(huán),但是發(fā)現(xiàn)
  15. // 當(dāng)數(shù)組的length>1的情況下,會(huì)發(fā)生即使你掃碼得到的code不與原數(shù)組相同但是會(huì)重復(fù)多次,次數(shù)由productIist.length決定
  16.             productIist.forEach(item=>{
  17. if(item.code !==this.data.productIist.code ) {
  18. this.data.productIist.push(products);
  19. this.setData({
  20. productIist: this.data.productIist
  21. })
  22. }
  23. })
  24. }).catch((res) => {
  25. console.log(res)
  26. wzy.showPop('提示', '當(dāng)前網(wǎng)絡(luò)繁忙,請(qǐng)重新掃描')
  27. })
  28. },

所以 在上面的正確的示例中 使用for循環(huán) 并把判斷也寫進(jìn)for循環(huán)中 如果數(shù)組中的code與掃描的code相等 就會(huì)彈出提示框,并且不會(huì)執(zhí)行下面代碼,但是當(dāng)條件不相符的時(shí)候,便可以愉快的執(zhí)行下面的代碼了。

相關(guān)案例查看更多