微信小程序的加入購(gòu)物車的選擇規(guī)格以及數(shù)據(jù)進(jìn)行本地存儲(chǔ)

html

<view class='Selecte'>
<text wx:for="{{properties}}" wx:key="item" class='Shoptool'>{{item.name}}</text> 

<text class='textSelect' bindtap='show'>
  <text class='txt'>選擇規(guī)格:選擇服務(wù) 測(cè)試規(guī)格</text> 
  <text class='textleft'>></text>
</text>

<view class="shopcar" hidden="{{flag}}">

   <view class="top">

       <image src="{{basicInfo.pic}}"/> 

       <view class="title">
          <text class="name">{{basicInfo.name}}</text>
          <text class="price" >¥299</text>
      </view>

      <view class="x" bindtap='hide'>X</view>

  </view>

  <view class="classify">
       <view wx:for="{{properties}}" wx:key="index" class="classifywrap" data-index='{{index}}' wx:for-index='index'>
       <view class="otitle">{{item.name}}</view>

       <view class="gg">
          <view wx:for="{{item.childsCurGoods}}" wx:key="index" data-in='{{index}}' data-index='{{index1}}' class="ggview{{item.oflag ? 'ggactive' : ''}}" wx:for-index='index1' bindtap='ChangColor'>{{item.name}}</view>
       </view>

     </view>
   </view>

   <view class="num">
      <view class="ootitle">購(gòu)買數(shù)量</view>
      <view class="onum">
          <text bindtap='can'>-</text>
          <text>{{count}}</text>
          <text bindtap='add'>+</text>
      </view>
   </view>

   <view class="cart">
       <text bindtap='addCart'> 加入購(gòu)物車</text>
    </view>

 </view>
</view>
<view class="item">
    <view wx:for="{{titleData}}" wx:key="index" class="tabclass {{num == item.id ? 'active' : ''}}" data-index='{{index}}' bindtap='titleflag'>{{item.name}}</view>

    </view>
       <view hidden='{{ohide}}'>
            <template is="wxParse" data="{{wxParseData:article.nodes}}"  />
      </view>

      <view hidden='{{!ohide}}'>暫無(wú)評(píng)論 </view>
      <view class="car">
           <image src='../../static/icon/在線客服.png' class='iconimgage'/>
           <image src='../../static/icon/購(gòu)物車.png' class='iconimgage' bindtap='Carting'/>
           <text class='count' hidden='{{!count}}'>{{count}}</text>
           <image src='../../static/icon/收藏.png' class='iconimgage'/>
           <text>立即購(gòu)買</text>
           <text bindtap='Cart'> 加入購(gòu)物車</text>
      </view>

js

    data: {
    titleData: [
      {
        id: 0,
        name: '商品介紹'
      },
      {
        id: 1,
        name: '商品評(píng)價(jià)'
      }
    ],
    ohide: false,
    num: 0,
    NUM: 0,
    properties: '',
    oflag: false,
    count: 1,
    cart:false
  },
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },

  /**
   * 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
   */
  onLoad: function (options) {
    // console.log(options.id)
    var that = this;
    wx.request({
      url: 'https://api.it120.cc/small4/shop/goods/detail?id=' + options.id,
      method: 'POST',
      data: {
        // 'id': options.id
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        // console.log(res.data.data)
        // 解析HTML
        var article = res.data.data.content
        // console.log(article)
        WxParse.wxParse('article', 'html', article, that, 5);
        this.setData({
          Shopdetail: res.data.data.pics,
          properties: res.data.data.properties,
          basicInfo: res.data.data.basicInfo
        })
      }
    })
   
    var value = wx.getStorageSync('CartData');  // 異步存儲(chǔ)方法  數(shù)據(jù)同步存儲(chǔ)的方法
     // 1. 判斷一進(jìn)頁(yè)面的時(shí)候你的同步存儲(chǔ)數(shù)據(jù)是否有數(shù)據(jù)
    if (value === "") {  //同步存儲(chǔ)數(shù)據(jù)等于空的話 data=false
    wx.setStorage({
      key: 'CartData',
      data: false
    })
    } else { // 3 乾颁。否則 的話 用wx.getStorage拿到你點(diǎn)擊加入購(gòu)物車  這個(gè)res.data==false判斷時(shí)保存的數(shù)據(jù)  再將數(shù)據(jù)用wx.setStorage將數(shù)據(jù)保存
    wx.getStorage({
      key: 'CartData',
      success: (res) => {
        console.log(res.data)
        wx.setStorage({
          key: 'CartData',
          data: res.data       
        })
      }
    })
  }
    
  },
  // 點(diǎn)擊規(guī)格讓要選擇的規(guī)格出現(xiàn)英岭,
  show: function (e) {
    this.setData({
      flag: false
    })
  },
  // 點(diǎn)擊x讓剛出現(xiàn)的選擇規(guī)格消失
  hide: function (e) {
    this.setData({
      flag: true
    })
  },
  //切換標(biāo)題的樣式
  titleflag: function (e) {
    // console.log(e)
    this.setData({
      num: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        ohide: false
      })
    } else if (e.currentTarget.dataset.index == 1) {
      this.setData({
        ohide: true
      })
    }
  },
  // 點(diǎn)擊選擇規(guī)格時(shí) 傳了兩個(gè)index值
  ChangColor:function(e) {
    // console.log(e.currentTarget.dataset)
    // 規(guī)格的index
    let ind = e.currentTarget.dataset.in;
    // 顏色的index
    let index = e.currentTarget.dataset.index;
    // console.log(e)
    // 利用規(guī)格的index值給當(dāng)前渲染在頁(yè)面的總源頭數(shù)據(jù)的每個(gè)兒子
    for (var i = 0; i < this.data.properties[ind].childsCurGoods.length; i++) {
      // 讓當(dāng)前所有的頁(yè)面的總源頭數(shù)據(jù)下的兒子的每個(gè)元素都處于關(guān)閉狀態(tài)
      this.data.properties[ind].childsCurGoods[i].oflag = false;
    }
    // 讓當(dāng)前點(diǎn)擊的頁(yè)面的總源頭數(shù)據(jù)下的兒子的index元素都處于open的狀態(tài)
    this.data.properties[ind].childsCurGoods[index].oflag = true;
    // this.setData 設(shè)置數(shù)據(jù)
    this.setData({
      properties: this.data.properties
    })
  },
  // 加法
  add: function (e) {
    // console.log(this.data.count)
    this.data.count++; // 在data中定義的count值++
    //  然后在設(shè)置一下count值
    this.setData({
      count: this.data.count
    })
  },
  // 減法  
  can: function (e) {
    this.data.count--;
    // console.log(this.data.count)
    if (this.data.count <= 1) {
      this.data.count = 1
    }
    this.setData({
      count: this.data.count
    })
  },
  // 點(diǎn)擊加入購(gòu)物車
  addCart: function (e) {
    this.setData({
      cart:true
    })
    // console.log(e)
    let num = 0 // 相當(dāng)于一個(gè)開關(guān)
    let properties = []
    for (let i = 0; i < this.data.properties.length; i++) {
      properties.push(this.data.properties[i].name)  //定義的properties  就是為了裝尺寸和顏色的
      for (let j = 0; j < this.data.properties[i].childsCurGoods.length; j++) {
        if (this.data.properties[i].childsCurGoods[j].oflag) {  //判斷規(guī)格數(shù)據(jù)中的每個(gè)數(shù)據(jù)是否是選中的狀態(tài)  是的話吭狡,就將選中的元素添加在childsCurGoods數(shù)組中
          properties.push(this.data.properties[i].childsCurGoods[j].name);
          // console.log(childsCurGoods)  ["1.8M", "珊瑚粉"]
          num++
        }
      }
    }
    // 將你要傳的數(shù)據(jù)放到這個(gè)對(duì)象當(dāng)中
    let obj = {
      'name': this.data.basicInfo.name,
      'src': this.data.basicInfo.pic,
      "value": properties,
      'count': this.data.count,
      'checked': false
    }
  //  本地存儲(chǔ) 
    // console.log(properties)
    // 當(dāng)你點(diǎn)擊加入購(gòu)物車的時(shí)候 你需要先將object用wx.getStorage獲取到
    wx.getStorage({
      key: 'CartData',   // 它獲取的是一進(jìn)頁(yè)面時(shí)的數(shù)據(jù)同步時(shí)為空的數(shù)據(jù) 缔俄,所以data=false
      success: function (res) {   // 獲取成功的時(shí)候  使用res.data進(jìn)行判斷
        // console.log(res)  因?yàn)橐贿M(jìn)頁(yè)面弛秋,數(shù)據(jù)同步也是空的俐载, data: false 
        if (res.data == false) {  // 數(shù)據(jù)同步時(shí)的為空要走這個(gè)判斷條件 res.data ==false 將第一次點(diǎn)擊添加的數(shù)據(jù)進(jìn)行一次保存
          wx.setStorage({
            key: "CartData", //只是一個(gè)key名
            data: [obj]  // 要保存的數(shù)據(jù) Object/ string
          })
        }else{  // 4 状婶。res.data==  數(shù)據(jù)時(shí) 
          let flagarr=false; //定義兩個(gè)開關(guān)
          let indexarr=false;
          // console.log(res.data); 數(shù)據(jù)
          for(var i=0;i<res.data.length;i++){
            console.log(i)
            // console.log(res.data[i].name)  商品的名字  例 簡(jiǎn)約知全棉四件套
            // 相當(dāng)于一個(gè)數(shù)組去重
            if (res.data[i].name == obj.name) {  //判斷res.data[i].name 是不是等于obj.name
              // console.log(obj.value)   規(guī)格數(shù)據(jù)
              // console.log(!obj.value[3])   false
              // console.log(res.data[i].value[1])  1.8 / 1.5
              // console.log(obj.value[3])  珊瑚粉
              if (res.data[i].value[1] == obj.value[1] && res.data[i].value[3]  ==obj.value[3] ) { //判斷 res.data[i].value[1]的數(shù)據(jù)  等于等于 obj.value[1]   與 res.data[i].value[3]  的數(shù)據(jù)  等于等于 obj.value[3]    執(zhí)行以下
                 flagarr=i;  // 0 是 關(guān)閉狀態(tài)
                     indexarr=i+1;  //定義的時(shí)候indexarr = false = 0  現(xiàn)在 indexarr = true = 1  indexarr為true的時(shí)候令杈,
                    break; // 使用break跳出本次循環(huán) 進(jìn)行下一個(gè)indexarr 為true的判斷
                }
              }
            }
            if(!indexarr){ // true 時(shí)
              // console.log(1)
              res.data.push(obj) // res.data 將obj 添加逗噩,
              wx.setStorage({
              key: "CartData", //只是一個(gè)key名
              data:res.data  // 添加了obj的res.data
              })
              flagarr = false;
              indexarr = false;  //關(guān)閉
            }else{ // 關(guān)閉時(shí)  
              //  console.log(1) 添加第三次執(zhí)行 數(shù)量
              let count =res.data[flagarr].count+obj.count;
              // console.log(num) //  數(shù)量 2
              res.data[flagarr].count=count //數(shù)據(jù)重復(fù)出現(xiàn)的數(shù)量加加后賦值給保存的數(shù)據(jù)的數(shù)量
              // console.log(res.data[flagarr].count)  //數(shù)量 2
              wx.setStorage({
                key: 'CartData',
                data: res.data
              })
            }
          }
        }
      })
    if (num == this.data.properties.length) {  //num==2  當(dāng)num 的開關(guān)的數(shù)值等于 properties的長(zhǎng)度的話  執(zhí)行一個(gè)成功的彈框
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000,
        mask: true
      })
      this.setData({
        flag: true
      })
    } else { // num 不等于properties的長(zhǎng)度話,就執(zhí)行一個(gè)請(qǐng)選擇規(guī)格的彈框
      wx.showToast({
        title: '請(qǐng)選擇規(guī)格',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    }
  },
  // 點(diǎn)擊加入購(gòu)物車 顯示規(guī)格款
  Cart: function (e) {
    this.setData({
      flag: false
    })
  },
  // 點(diǎn)擊購(gòu)物車小圖標(biāo)示绊,到購(gòu)物車頁(yè)面
  Carting:function(e){
    wx.switchTab({
      url: '../cart/cart'
    })
  },
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末展哭,一起剝皮案震驚了整個(gè)濱河市役衡,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌盗尸,老刑警劉巖柑船,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件帽撑,死亡現(xiàn)場(chǎng)離奇詭異泼各,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)亏拉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門扣蜻,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)逆巍,“玉大人,你說(shuō)我怎么就攤上這事莽使∪窦” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵芳肌,是天一觀的道長(zhǎng)灵再。 經(jīng)常有香客問(wèn)我,道長(zhǎng)亿笤,這世上最難降的妖魔是什么翎迁? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮净薛,結(jié)果婚禮上汪榔,老公的妹妹穿的比我還像新娘。我一直安慰自己肃拜,他們只是感情好痴腌,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般拷恨。 火紅的嫁衣襯著肌膚如雪蹋艺。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天戚嗅,我揣著相機(jī)與錄音,去河邊找鬼枢舶。 笑死懦胞,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的凉泄。 我是一名探鬼主播躏尉,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼后众!你這毒婦竟也來(lái)了胀糜?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蒂誉,失蹤者是張志新(化名)和其女友劉穎教藻,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體右锨,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡括堤,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悄窃。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡讥电,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出轧抗,到底是詐尸還是另有隱情恩敌,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布横媚,位于F島的核電站纠炮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏灯蝴。R本人自食惡果不足惜抗碰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绽乔。 院中可真熱鬧弧蝇,春花似錦、人聲如沸折砸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)睦授。三九已至两芳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間去枷,已是汗流浹背怖辆。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留删顶,地道東北人竖螃。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像逗余,于是被迫代替她去往敵國(guó)和親特咆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容