Promises-小程序购物车结算

  //结算提交
  checkOut : function(){
      var price = this.data.sum;
      var user = wx.getStorageSync('userInfo');
      if (!user){
        wx.showModel({
          title:'提示',
          content:'请登录',
          success : function(){
            
          },
        })
      }
        
      let goods_id=0;
      var shop = this.data.shoppingCartInfo;
      
      wx.request({
        url: `${app.globalData.API_URL}`+'/order',
        data:{
          goods_price : this.data.sum,
          user_id : user.mid
        },
        method: 'POST',
        header :{
          'content-type':'application/json'
        },
        success : function(res){
          var order_id = res.data;
//引入异步
          var promises = new promise(function(resolve,reject){
            resolve(shop);
        })
          promises.then(function(value){
      //循环遍历 value.forEach(function(value){ console.log(value,'foreach'); wx.request({ url: `${app.globalData.API_URL}/ordergoods`, data: { order_id : order_id, goods_id : value.goodId, goods_name : value.goodName, goods_price : value.goodPrice, goods_num : value.goodSum, spec_key : value.goodImg }, method: 'POST', success: function(res){ console.log('成功'); }, fail: function(res) { }, }); }); }).catch(function(error){ console.error(error); }); } }) }

  

posted @ 2017-11-08 10:42  wcu1117  阅读(270)  评论(0编辑  收藏  举报