var e = getApp(), r = e.request,rq=r,t = require("../../../../utils/util.js"), o = require("../../../../utils/common.js"),oo=e.globalData,os=oo.setting;
var regeneratorRuntime = require('../../../../utils/runtime.js');
var ut= require('../../../../utils/util.js');
var py = require("../../../../utils/pay.js");


Page({
    data: {
        url: os.url,
        resourceUrl: os.imghost,
        iurl: os.imghost,
        order: null,
        optionIsGoup: !1,
        user_note: "",
        conf:null,
    },
    onLoad: function(e) {
        var th=this;
        var r = void 0 !== e.isGoup && e.isGoup;

        getApp().getConfig2(function (e) {
			
            th.setData({conf: e,sales_rules:e.sales_rules, userInfo: getApp().globalData.userInfo});
            //--- 看后台是不是有开通等级卡 ---
        
        },1)

        this.setData({
            optionIsGoup: r
        }), this.requestOrderDetail(e.order_id), wx.removeStorageSync("order:order_detail:update");
    },
    onShow: function() {
        this.setData({paying:0});
        ut.fy_back('',0);
        py.set_fir();

        wx.getStorageSync("order:order_detail:update") && (wx.removeStorageSync("order:order_detail:update"), 
        this.requestOrderDetail(this.data.order.order_id));
    },
    requestOrderDetail: function(e) {
        var o = this,oid=e,th=this;
        r.get(o.data.url + "/api/weshop/order/get/"+os.stoid+"/"+e, {
            success: function(e) {
                var r = e.data.data;
                r.addTimeFormat = t.format(r.add_time);
                if (r.order_status == 0 && r.pay_status==0 ){
                    r.order_status_detail="待支付"; r.pay_btn=1;
                }
                if (r.order_status == 1 && r.pay_status == 1 && r.shipping_status== 0){
                   //自提的话
                   if (r.exp_type==1)
                   {
                    r.order_status_detail = "待自提";
                   }
                   else
                   {
                     r.order_status_detail = "待发货";
                   }
                  }
               
                if (r.order_status == 1 && r.pay_status == 1 && r.shipping_status == 1){
                    r.order_status_detail = "待收货";r.receive_btn=1;
                    if (r.exp_type == 0) r.shipping_btn=1;
                }
                if (r.order_status == 1 && r.pay_status == 1 && r.shipping_status == 2){
                  r.order_status_detail = "待收货"; r.receive_btn = 1;
                  if (r.exp_type == 0) r.shipping_btn = 1;
                }
                if (r.order_status == 2){
                   r.order_status_detail = "待评价"; if (r.exp_type == 0) r.shipping_btn = 1;
                }
                if (r.order_status == 3)
                   r.order_status_detail = "已取消";
                if (r.order_status == 4){
                  r.order_status_detail = "已完成"; if (r.exp_type == 0) r.shipping_btn = 1;
                }
                if (r.order_status == 5)
                   r.order_status_detail = "已作废";
                if (r.order_status == 6)
                  r.order_status_detail = "退款完成";
				if (r.pt_status == 1 && r.pt_prom_id>0)
				  r.order_status_detail = "组团中";  
				 

               r['is_all_return'] = 0;

               var discount_price=r['discount_amount'];
               var dis_json=r['prom_pt_json'];

               if(dis_json && discount_price){
                   var prom_pt_json=JSON.parse(dis_json);
                   var ladder_amount=0;
                   for (var ll_item of prom_pt_json) {
                       if(ll_item['ladder_prom_id']){
                           ladder_amount += ll_item['dis'];
                       }
                   }
                   r['ladder_amount']=ladder_amount;
                   r['discount_amount']=Math.floor((discount_price-ladder_amount)*100)/100;
               }




               //------获取订单商品-------
               rq.get("/api/weshop/ordergoods/list",{
                    data: { store_id: os.stoid, order_id: oid},
                    success:async function (eb) {
                         r.order_goods=eb.data.data.pageData;
                         var glist =  r.order_goods;
                         for(var i in glist){
                            if(glist[i].is_gift){
                                r.is_prom=1;
                            }
                         }

                          //------------对比一下有没有退款记录------------
                          await getApp().request.promiseGet("/api/weshop/order/returngoods/page",{
                              data: { order_id: r.order_id, store_id: os.stoid,pageSize: 20 }
                          }).then(rs=>{
                              var ttd=rs;
                              //--看一下订单的总数量--
                              var gtype_num = r.order_goods.length;
                              if(ttd.data.data.pageData == undefined) return false;
                              var goodslist = r.order_goods;

                              //----没有相关的退款记录----
                              if (ttd.data.data.total==0){
                                  r['is_all_return'] = 0;

                                  goodslist.forEach(function (ee, ii) {
                                      if (r['order_status'] == 1 && r['pay_status'] == 1 && gtype_num>1){
                                          //是不是可以单个商品退款
                                          if(th.check_for_return_btn(r) ) {
                                              goodslist[ii]['return_btn']=1;
                                          }
                                      }
                                  });
                              }else{
                                  //--------整单退--------
                                  if (ttd.data.data.pageData[0]['goods_id_list'] != null
                                      && ttd.data.data.pageData[0]['goods_id_list'] != ''){


                                      var eea = ttd.data.data.pageData[0];
                                      //1.退款正在进行中，
                                      //2.退款被拒绝就要显示可以退款
                                      //3.退款已经完成
                                      r['is_all_return']=1;
                                      r['is_all_return_status'] = ttd.data.data.pageData[0].status;

                                      if (r['order_status'] == 1 && r['pay_status'] == 1) {
                                          switch (eea.status) {
                                              case 0:
                                              case 1:
                                                  r.order_status_detail="退款中";
                                                  r['return_btn'] = 2; break;
                                              case 2:
                                                  r.order_status_detail="退款完成";
                                                  r['return_btn'] = 4; break;
                                              case 3:
                                                  r['return_btn'] = 3; break;
                                              default:
                                                  r['return_btn'] = 0;
                                          }
                                      }
                                  }else{
                                      //1.退款正在进行中，
                                      //2.退款被拒绝就要显示可以退款
                                      //3.退款已经完成
                                      r['is_all_return'] = 0;
                                      var rt_ok_num=0;

                                      //if(r['order_status'] == 1 && r['pay_status'] == 1) {
                                          goodslist.forEach(function (eeb, iii) {
                                              if(r['order_status'] == 1 && r['pay_status'] == 1)
                                                  goodslist[iii]['return_btn'] = 1; //申请退款
                                              ttd.data.data.pageData.forEach(function (eea, ii) {
                                                  var st = eea.status;
                                                  if (eea.goods_id == eeb.goods_id){

                                                      switch (eea.status){
                                                          case 0:
                                                          case 1://退款处理中
                                                              r['has_rt'] = 1; //有部分退
                                                              goodslist[iii]['return_btn'] = 2; break;
                                                          case 2://退款完成
                                                              r['has_rt'] = 1; //有部分退
                                                              goodslist[iii]['return_btn'] = 4;rt_ok_num++; break;
                                                          case 3://已拒绝，重新退款
                                                              goodslist[iii]['return_btn'] = 3; break;
                                                          default:
                                                              if(r['order_status'] == 1 && r['pay_status'] == 1)
                                                                   goodslist[iii]['return_btn'] = 1; //申请退款
                                                      }
                                                      return;
                                                  }
                                              });
                                          });
                                      //}
                                  }
                              }

                              //--如果是全部退款，也就可以删除--
                              if(rt_ok_num==goodslist.length){
                                  r['is_all_return_status']=2;
                              }
                              r['order_goods'] = goodslist;
                              var g_num = 0;
                              goodslist.forEach(function (ee, ii) {
                                  if(ee.return_btn!=4)
                                     g_num += ee.goods_num;
                              });
                              r['g_num'] = g_num;
                          })


                      //--------获取门店-----------
                      rq.get("/api/weshop/pickup/get/" + os.stoid + "/" + r.pickup_id, {
                           success: function (ebb) {
                                r.pickname = ebb.data.data.pickup_name;
                                o.setData({
                                  order: r,
                                });
                           }
                      })


                    }
                })               
            }
        });
    },
    /*-----联系客户------*/
    contactService: function() {
        getApp().con_Service();
    },

    //--------取消订单---------
    cancelOrder: function(e) {
      var t = this,th=t, o = e.currentTarget.dataset.id,order=this.data.order;
      var is_skill=0;
      var order_goods=order.order_goods;
      var is_zsorder=order.is_zsorder;

      //--判断是不是秒杀--
      order_goods.forEach(function (val,ind) {
        if(val.prom_type==1){
          is_skill=1; return false;
        }
      })

        wx.showModal({
            title: "是否取消订单？",
            success: function(e) {
              if(!e.confirm) return false;
              //如果是秒杀和拼单的商品，取消订单要返回redis,并取消订单
              //if(is_skill==1 || is_zsorder>=2){
             getApp().request.post("/api/weshop/order/cancelOrder/"+os.stoid+"/"+o+"/"+getApp().globalData.user_id,{
               data:{},
               success: function(t) {
                 if(t.data.code==0)
                   th.requestOrderDetail(o), wx.setStorageSync("order:order_list:update", !0);
               },
             })
              /*----
              }else{
                  getApp().request.put("/api/weshop/order/updatebyId", {
                    data: {order_id: o, order_status: 3},
                    success: function(e) {
                      th.requestOrderDetail(o), wx.setStorageSync("order:order_list:update", !0);
                    }
                  });
              }--*/
            }
        });
    },
    
    //--------确认收货---------
    confirmOrder: function(e) {
        var th=this;
        var t = this, o = this.data.order.order_id,order=this.data.order;
        var is_zsorder=order.is_zsorder;
        wx.showModal({
            title: "是否确认收货?",
            success: function(t) {
                t.confirm && rq.post("/api/weshop/order/confirmOrder/"+os.stoid+"/"+o+"/"+getApp().globalData.user_id,{
                    success:function (e) {
                        if(e.data.code==0){
                            th.requestOrderDetail(o)
                        }
                    }
                })
            }
        });
    },

    //--------立即支付----------
    async jumpToCart4(e) {

        if(this.data.paying) return false;
        this.setData({paying:1});

        var th=this;
        if(this.data.optionIsGoup) 
		     wx.navigateTo({
                 url: "/pages/team/team_confirm/team_confirm?orderSn=" + this.data.order.order_sn + "&orderPay=true"
             }) 		   
		var order=this.data.order;
		var order_goods=order.order_goods;
		var user_id=order.user_id;
		
		var wlist="";	
		for(var i in order_goods){		
			var good=order_goods[i];			
			//线下取价功能已经关闭或者过期
			if(!th.data.is_open_offline && good.offline_cut>0){
				 wx.showModal({
				   title: '提示',
				   content: '线下取价功能已经关闭或者过期！'
				 });
                th.setData({paying:0});
				 return false;
		}	
		
			//要每件每件的商品进行检查，看有么有超出库存，超出限购
			var good= order_goods[i],goodsbuynum=0,promgoodsbuynum=0,gg=null;
			//获取单品的现在的活动状态
			await getApp().request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + good.goods_id, {		
			}).then(res=>{
				gg=res.data.data;
			})		
			var limit = gg.viplimited;
			var store_count = gg.store_count;
			good.erpwareid=gg.erpwareid;
			
			//---要获得商品，该用户买了多少件,同步应用---
			await getApp().request.promiseGet("/api/weshop/ordergoods/getUserBuyGoodsNum", {
			  data: {
			    store_id: os.stoid,
			    user_id: getApp().globalData.user_id,
			    goods_id: good.goods_id,
			    prom_type: good.prom_type,
			    prom_id: good.prom_id,
                timetype:good.viplimited_timetype
			  },
			}).then(res => {
			   var buy_num_data = res.data.data;
			   if (buy_num_data.promgoodsbuynum) promgoodsbuynum = buy_num_data.promgoodsbuynum;
			   goodsbuynum = buy_num_data.goodsbuynum;
			})
			
			var buyed_mum2=goodsbuynum+good.goods_num;		
			if (buyed_mum2 > limit && limit > 0) {
			 
			   var content=gg.goods_name+'购买数量超出商品限购,请取消订单';
			   th.toast(content);
               th.setData({paying:0});
			    return false;
			}
            //当是组合优惠的时候
            if(good.prom_type==7){
                //如果有组合购
                var isok = 1;
                var url="/api/weshop/prom/zhbuy/get/"+os.stoid+"/"+good.prom_id+"/"+getApp().globalData.userInfo.user_id;
                await getApp().request.promiseGet(url, {}).then(res => {
                    if(res.data.code==0 && res.data.data){
                        if(res.data.data.is_show!=1){
                            isok=0;
                        }
                        //如果活动已经结束
                        if(res.data.data.is_end==1){
                            isok=0;
                        }
                        //已经结束
                        if(ut.gettimestamp()>res.data.data.end_time){
                            isok=0;
                        }
                        //还未开始
                        if(ut.gettimestamp()<res.data.data.start_time){
                            isok=0;
                        }

                    }else{
                        //未找到商品的活动
                        isok = 0;
                    }

                })
                if (!isok){
                    var  content=gg.goods_name+'活动已经结束,请取消订单';
                    th.toast(content);
                    th.setData({paying:0});
                     return false;
                }

                var url1 = "/api/weshop/prom/zhbuyGoods/page";
                var req_data = {
                    page: 1,
                    pageSize: 1,
                    store_id: os.stoid,
                    zh_id: good.prom_id,
                    goods_id:good.goods_id
                }
                await getApp().request.promiseGet(url1, {
                    data: req_data
                }).then(res => {
                    if (ut.ajax_ok(res)) {
                        var gdlist = res.data.data.pageData[0];
                        good.buyqty=gdlist.buyqty;
                    }
                })

                var num=good['buyqty'];
                console.log(1111);
                console.log(num);
                //---- 要计算商品的限购 -----
                if(good['buyqty']>0){
                    if(good.goods_num+promgoodsbuynum>good['buyqty']){

                        var  content= good['goods_name']+"超出活动限购\n";
                        th.toast(content);
                        th.setData({paying:0});
                        return false;
                    }
                }
            }

            //如果优惠促销和搭配购的时候
			if((good.prom_type==3 || good.prom_type==5) && good.is_collocation!=1 && good.is_gift!=1){
                var p_ok = 1,is_g_prom=0;
                //因为有全场优惠活动，商品参加的活动还未开始
                var url="/api/weshop/activitylist/getGoodActInfo";
                var req_data={
                    store_id:os.stoid,goodsidlist:good.goods_id,is_detail:1,user_id:user_id
                };
                await getApp().request.promiseGet(url, {data:req_data}).then(async function (res) {
                    if(res.data.code==0 && res.data.data && res.data.data.length){
                        for(let i in res.data.data){
                            let item=res.data.data[i];
                            if(item.prom_type==3) {
                                is_g_prom=1; break;
                            }
                        }
                    }
                })
                if(is_g_prom){
                    await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromList/" + os.stoid + "/" + good.goods_id + "/0"+"/"+getApp().globalData.user_id, {}).then(res => {
                        if (res.data.code == 0) {
                            var r_data = res.data.data;
                            if (!r_data.collocationPromList && good.prom_type == 5) {

                                var content = good.goods_name + '未找到活动,请取消订单重新购买';
                                th.toast(content)
                                p_ok = 0;
                            }
                            if (!r_data.promGoodsLists && good.prom_type == 3) {

                                var content = good.goods_name + '未找到活动,请取消订单重新购买';
                                th.toast(content);
                                p_ok = 0;
                            }
                        } else {

                            var content = good.goods_name + '未找到活动,请取消订单重新购买'
                            th.toast(content);
                            th.setData({paying:0});
                            p_ok = 0;
                        }
                    })
                }else{
                    await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromNormal/" + os.stoid + "/" + good.goods_id, {}).then(res => {
                        if (res.data.code == 0) {
                            var r_data = res.data.data;
                            if (!r_data.collocationPromList && good.prom_type == 5) {

                                var content = good.goods_name + '未找到活动,请取消订单重新购买';
                                th.toast(content)

                                p_ok = 0;
                            }
                            if (!r_data.promGoodsLists && good.prom_type == 3) {

                                var content = good.goods_name + '未找到活动,请取消订单重新购买';
                                th.toast(content);
                                p_ok = 0;
                            }
                        } else {

                            var content = good.goods_name + '未找到活动,请取消订单重新购买'
                            th.toast(content);
                            p_ok = 0;
                        }
                    })

                }
                if (!p_ok){
                    th.setData({paying:0});return false;
                }
			}
								
			//商品的普通购买 ，不要进行判断
			if((good.prom_type==1 || good.prom_type==2 || good.prom_type==6  || good.prom_type==4) && !good.is_gift && !good.is_collocation && !good.is_integral_normal && !good.is_pd_normal){
				   if(gg.prom_type!=good.prom_type && gg.prom_type>0)	{
					   var content=gg.goods_name+'商品的活动发生了变化,请取消订单重新购买';
					   th.toast(content);
                       th.setData({paying:0});
					    return false;
				   }
			  }else{
				  if((gg.prom_type==1 || gg.prom_type==3 || gg.prom_type==5 || gg.prom_type==6 || gg.prom_type==4)
					  && !good.is_collocation  && !good.is_gift && !good.is_integral_normal && !good.is_pd_normal) {
				  				  var prom=null;
				  				  //---如果是活动的时候---
				  				  var prom=null,th=this;
				  				  if(gg.prom_type==1){	
				  				  			  await getApp().request.promiseGet("/api/ms/flash_sale/getNew/"+os.stoid+"/"+user_id+"/"+gg.prom_id,{
				  				  			  }).then(res=>{
				  				  				  if(res.data.code==0){
				  				  					  prom=res.data.data;
				  				  					  prom.price=prom.user_price;
				  				  				  }
				  				  			  })
				  				  }	  
								  
								  if (gg.prom_type == 2) {
								      await getApp().request.promiseGet("/api/weshop/goods/groupBuy/getActInfo/" + os.stoid + "/"+ gg.goods_id + "/" + gg.prom_id, {}).then(res => {
								          if (res.data.code == 0) {
								              prom = res.data.data;
								              prom.price=prom.price;
								          }
								      })
								  }
								  
								  
				  				  if(gg.prom_type==6){
				  				  			await getApp().request.promiseGet("/api/weshop/teamlist/get/"+os.stoid+"/"+gg.prom_id,{
				  				  			}).then(res=>{
				  				  				  if(res.data.code==0){
				  				  					  prom=res.data.data;
				  				  				  }
				  				  			})	  
				  				  }	 
				  				  
				  				  if(gg.prom_type==4){
				  				  			await getApp().request.promiseGet("/api/weshop/integralbuy/pageIntegralBuyGoodsList?stype=1&stypeup=1&timetype=2",{
				  								data:{store_id:os.stoid,goods_id:gg.goods_id,user_id: getApp().globalData.user_id,}
				  				  			}).then(res=>{
				  				  				  if(res.data.code==0 && res.data.data && res.data.data.pageData && res.data.data.pageData.length){
				  				  					  prom=res.data.data.pageData[0];
				  				  				  }
				  				  			})	  
				  				  }	
				  				  								
				  				  if(prom){
				  					  var t_now=ut.gettimestamp();
				  					  if(prom.is_end==0 && prom.start_time<t_now && prom.end_time>t_now){
				  						  
				  						   var  content=gg.goods_name+'商品的活动发生了变化,请取消订单重新购买';
				  						   th.toast(content);
                                           th.setData({paying:0});
				  						   return false;
				  					  }
				  				  } 				  
				  }else{
                          //不是赠品的时候
				  	      if(!good.is_gift && good.prom_type!=3){
							  var t_ok=1;
							  await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromList/" + os.stoid+"/"+good.goods_id+"/0"+"/"+getApp().globalData.user_id, {}).then(res => {
								  if(res.data.code==0){
									  var r_data=res.data.data;
									  //-- 参加了全局的优惠活动 --
									  if(r_data.promGoodsLists){
										 
										  var content=gg.goods_name+'商品的活动发生了变化,请取消订单重新购买';
										  th.toast(content);
										  t_ok=0;
									  }
								  }
							  })
							  if(!t_ok) {
                                  th.setData({paying:0});return false;
                              }
						  }

						  //-- 如果有参加线下取价 --		  
						  if(good.offline_cut>0) wlist+=encodeURIComponent(gg.erpwareid)+",";	
				   }
				  //赠品和搭配购不判断商品金额
				  var isok=1;
				  var card_field=th.data.card_field; // 会员等级
				  //如果会员是等级会员,商品有等级价,且不是活动商品
		          if(!good.is_gift && !good.is_collocation) {
		             if (card_field && gg[card_field] > 0) {
		                 if (good.goods_price != gg[card_field] && good.offline_cut<=0) isok = 0;
		                 if (good.goods_price >gg[card_field] && good.offline_cut>0) isok = 0;  //如果线下价格比较贵，则不通过
	             					 				  
		             } else {
		                 if (good.goods_price != gg.shop_price && good.offline_cut<=0) isok = 0;
		                 if (good.goods_price> gg.shop_price && good.offline_cut>0) isok = 0; //如果线下价格比较贵，则不通过
		             }
		              var is_h=0;
		              if (!isok) {
		                 
		                  var content=gg.goods_name + '商品的价格发生了变化,请取消订单重新购买22';
		                  th.toast(content);
                          th.setData({paying:0});
		                   return false;
		              }
		          }
				
				  //--如果是线下库存的时候--		
				  if(th.data.conf.sales_rules==2){
					   var ob={};
					   await th.check_down_line_next(gg,good.goods_num,order.pickup_id,function(obj){
						   ob=obj;
					   })
					   if(ob.code==-1){
						   var content=gg.goods_name+'门店库存不足,请取消订单';
						   th.toast(content);
                           th.setData({paying:0});
						    return false;
					   }				   
					   
				  }else{
					  if(good.goods_num>store_count){
						  
						  var content=gg.goods_name+'商品的库存不足,请取消订单';
						  th.toast(content);
                          th.setData({paying:0});
						   return false;		
					  }
					  
				  }			  
			  }
			  
			  console.log("------------------------------------");
			   //---如果是活动的时候---
			   var prom=null,goodsinfo=good,th=this;
			   if(goodsinfo.prom_type==1){	
			  			  await getApp().request.promiseGet("/api/ms/flash_sale/getNew/"+os.stoid+"/"+user_id+"/"+goodsinfo.prom_id,{
			  			  }).then(res=>{
			  				  if(res.data.code==0){
			  					  prom=res.data.data;
                                  prom.price=prom.user_price;
			  				  }
			  			  })
			   }	
			   
			   if (goodsinfo.prom_type == 2) {
			       await getApp().request.promiseGet("/api/weshop/goods/groupBuy/getActInfo/" + os.stoid + "/"+ good.goods_id + "/" + goodsinfo.prom_id, {}).then(res => {
			           if (res.data.code == 0) {
			               prom = res.data.data;
			               prom.price=prom.price;
			           }
			       })
			   }
				 
				 	 
			   if(goodsinfo.prom_type==6 && !good.is_pd_normal){
			  			await getApp().request.promiseGet("/api/weshop/teamlist/get/"+os.stoid+"/"+goodsinfo.prom_id,{
			  			}).then(res=>{
			  				  if(res.data.code==0){
			  					  prom=res.data.data;
			  				  }
			  			})	  
			   }
			  				
			  if(goodsinfo.prom_type==4 && !good.is_integral_normal){
			  			 await getApp().request.promiseGet("/api/weshop/integralbuy/pageIntegralBuyGoodsList?stype=1&stypeup=1&timetype=2",{
			  			 	data:{store_id:os.stoid,goods_id:goodsinfo.goods_id,user_id: getApp().globalData.user_id,}
			  			 }).then(res=>{
			  			 	  if(res.data.code==0 && res.data.data && res.data.data.pageData && res.data.data.pageData.length){
			  			 		  prom=res.data.data.pageData[0];
			  			 	  }
			  			 })	  
			  }		
			  							
			   //----------如果有活动，并且在进行中，就不计算线下库存---------------	 
			   if(prom){
			  			  var now=ut.gettimestamp();
			  			  if(prom.is_end==1 && prom.end_time<now){			   
			  				
			  				var content=goodsinfo.goods_name+'商品的活动已经结束,请取消订单重新购买';
			  				th.toast(content);
                            th.setData({paying:0});
			  				return false;		
			  			  }
			  			  
			  			  buyed_mum2=promgoodsbuynum+good.goods_num;
			  			  if (buyed_mum2 > prom.buy_limit && prom.buy_limit > 0) {
			  			     var content=goodsinfo.goods_name+'购买数量超出商品活动限购,请取消订单重新购买';
			  			     th.toast(content);
                              th.setData({paying:0});
			  			      return false;
			  			  }
			  			  //看一下会员的秒杀价格是不是发生了变化
                          if(goodsinfo.prom_type==1 && goodsinfo.goods_price!=prom.price){
                               var content=goodsinfo.goods_name+'秒杀活动的价格发生了变化';
                               th.toast(content);
                               th.setData({paying:0});
                               return false;
                          }
						  
						  //看一下会员的团购价格是不是发生了变化
						  if(goodsinfo.prom_type==2 && goodsinfo.goods_price!=prom.price){
						      var content=goodsinfo.goods_name+'秒杀活动的价格发生了变化';
						      th.toast(content);
                              th.setData({paying:0});
						       return false;
						  }

			  			  if(goodsinfo.prom_type==4){
			  				  if (good.goods_num > prom.limitqty-prom.buy_num) {
			  				   
			  				      var content=goodsinfo.goods_name+'购买数量超出商品活动库存,请取消订单';
			  				      th.toast(content);
                                  th.setData({paying:0});
			  				       return false;
			  				  }				  
			  			  }else if(goodsinfo.prom_type == 1 || goodsinfo.prom_type == 2 || goodsinfo.prom_type == 6 ) {
								if(order.add_time+5*60<ut.gettimestamp()) {
									var content = gg.goods_name + '该订单已经超时无法支付,请取消订单';
									th.toast(content);
                                    th.setData({paying:0});
									return false;
								}
						}
											  
			   }
			  
		}
		
		//-- 要判断一下线下取价价格是不是发生改变 --
		if(wlist!="" && th.data.is_open_offline && th.data.is_open_offline>0){
			wlist=ut.sub_last(wlist);
			var keyid=order.pickup_id;
			var user_info=getApp().globalData.userInfo;
			if(!keyid) {
                th.setData({paying:0});return false;
            }
			var offarr=null;	
			await getApp().request.promiseGet("/api/weshop/goods/listWarePrice", {
			       data:{
					   VIPId:encodeURIComponent(user_info.erpvipid),
				  	   store_id:os.stoid,
			           PickupId:keyid,
				  	   WareIds:wlist},			 
					},
			).then(res=>{
				if(res.data.code==0 && res.data.data && res.data.data.length>0){
					offarr=res.data.data;						  
				}
			})				
			if(!offarr && offarr.length<=0 ){
				wx.showModal({ title: '获取线下取价失败',});
			}
			var newarr=ut.convert_arr_key(offarr,'WareId');	
			//-- 循环判断线下的价格 --	
			for(var k in order_goods){
				var good=order_goods[k];
				//判断线下取价是不是一样
				if(newarr[good.erpwareid] && 
				   parseFloat(newarr[good.erpwareid].WarePrice)!=parseFloat(good.goods_price)){		
					
					var content=good.goods_name + '商品的价格发生了变化,请取消订单重新购买';
					th.toast(content);
                    th.setData({paying:0});
					 return false;	
				}
			}	
				
		}

		/*---
		o.jumpToCart4({
            order_sn: this.data.order.order_sn,
            order_amount: this.data.order.order_amount,
			type: 1 //正常单
        }, 1);--*/

        py.pay(this.data.order.order_sn, function() {
            th.setData({paying:0});
            wx.showToast({title: '支付成功'})
			wx.navigateBack();
        },function () {
            th.setData({paying:0});
        },os.stoid,1);


    },
    checkTeam: function() {
        wx.navigateTo({
            url: "/pages/team/team_detail/team_detail?foundId=" + this.data.order.orderTeamFound.found_id
        });
    },

    //------图片失败，默认图片---------
    bind_bnerr: function (e) {
      var _errImg = e.target.dataset.errorimg;
      var _errObj = {};
      _errObj[_errImg] = "public/images/default_goods_image_240.gif";
      this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;       
    },

    zfwk:function(){
      var url = "/packageE/pages/cart/cart_wk/cart_wk?order_id=" + this.data.order.order_id;
      wx.redirectTo({
          url:url,
      })
    },

  //--------跳转到退款详情-----------
  gotoreturn: function (t) {
    var e = t.currentTarget.dataset.recid;
    var oid = t.currentTarget.dataset.oid;
    var wh = "";
    if (e != undefined && e != null && e != "") {
      wh = "&goods_id=" + e;
    }
    wx.navigateTo({
      url: "/packageF/pages/user/return_goods_info/return_goods_info?order_id=" + oid + wh,
    });
  },

  //---判断是不是有退款-----
  checkReturnGoodsStatus: function (t) {
    var e = t.currentTarget.dataset.recid;
    var oid = t.currentTarget.dataset.oid;
    r.get("/api/weshop/order/returngoods/page", {
      data: {
        goods_id: e,
        order_id: oid,
        store_id: os.stoid
      },
      success: function (t) {
        if (t.data.code == 0) {
          var r = t.data.data.total;
          if (0 == r) return wx.navigateTo({
            url: "/packageF/pages/user/return_goods/return_goods?rec_id=" + e + "&order_id=" + oid,
          });
          //--如果拒绝，就重新申请退款--
          if (t.data.data.pageData[0].status == 3) {
            return wx.navigateTo({
              url: "/packageF/pages/user/return_goods/return_goods?rec_id=" + e + "&order_id=" + oid,
            });
          }

          wx.navigateTo({
            url: "/packageF/pages/user/return_goods_info/return_goods_info?id=" + r
          });
        }
      }
    });
  },

    //-----删除订单-----
    deleteOrderData: function(t) {
        var th=this;
        var order_id=t.currentTarget.dataset.order_id;		
	    var	up_data={
		  order_id: order_id,isdel:1
		};	
		if(th.data.order.order_status!=6){
			up_data.order_status=5;
		}		
        wx.showModal({
            title: "是否删除订单？",
            success: function(tt) {
                tt.confirm && rq.put("/api/weshop/order/updatebyId", {
                    data: up_data,
                    success: function (t) {
                        getApp().my_warnning("删除成功",1,th);
                        setTimeout(function () {
                             getApp().goto("/pages/user/order_list/order_list");
                        },1000);
                    }
                })
            }
        });
    },

    //--判断是不是可以单个商品退--
    check_for_return_btn:function (item) {
        if(item.coupon_no) return false;        //有用优惠券
        if(item.order_prom_id) return false;    //有订单优惠
        if(item.order_status>=2) return false;  //订单状态>2
        var istrue=true;
        //----判断是不是有优惠活动和赠送商品----
        for (var g in item.order_goods) {
            var g_item=item.order_goods[g];
            if ((g_item.prom_type == 3 && g_item.discount_amount> 0)
                || (g_item.prom_type== 3 && g_item.goods_price <= 0)
            ) {
                istrue=false; break
            }
            if (g_item.prom_type== 5) {
                istrue=false; break
            }
        }
        if(!istrue) return false;
        var conf=this.data.conf;
        //判断是不是原路退回
        if(conf.refund_type==1) return true;
        else{
            if(item.order_amount==0 || item.user_money==0) return true;
        }
        return false;
    },
	
	

	//---检验线下库存的数量的子函数---
	async check_down_line_next(gg,num,pick_id,func){
	 
	  var lock=0,pick_no,plist;
	  //先读取门店的lock
	  await getApp().request.promiseGet("/api/weshop/order/ware/lock/page",{
		  data:{store_id:os.stoid,wareId:gg.goods_id,storageId:pick_id}
	  }).then(res=>{
		  if(res.data.code==0 && res.data.data.total>0){
			  for(var i in res.data.data.pageData)
			     lock+=res.data.data.pageData[i].outQty;
		  }
	  })
	  //先获取门店的编号
	  await getApp().request.promiseGet("/api/weshop/pickup/get/"+os.stoid+"/"+pick_id,{
		  data:{storeId:os.stoid,goodsId:gg.goods_id,pickupId:pick_id}
	  }).then(res=>{
		  if(res.data.code==0){
			  pick_no=res.data.data.pickup_no;
		  }
	  })	   
	  //读取线下的门店库存 
	  await getApp().request.promiseGet("/api/weshop/goods/getWareStorages",{
		  data:{storageNos:pick_no,wareIds:encodeURIComponent(gg.erpwareid),storeId:os.stoid}
	  }).then(res=>{
		  if(res.data.code==0 && res.data.data && res.data.data.pageData && res.data.data.pageData.length>0){
			  plist=res.data.data.pageData[0];		 
		  }
	  })	  
	  var ob={}; ob.code=1;	  
	  if(!plist){
	  	   ob.code=-1; ob.CanOutQty=0;  
	  	   func(ob);return false;    
	  }	
	 
	  if(num>plist.CanOutQty-lock){
		 ob.code=-1; 
		 ob.CanOutQty=plist.CanOutQty-lock
		 if(ob.CanOutQty<0) ob.CanOutQty=0;  
	  }
	  func(ob)  
	},
	
	
	//再来一单的功能的实现
	moreOrder:function(e){
		  var th=this;
		  var item=th.data.order;

          if(this.data.moring) return false;
          this.data.moring=1;

		  th.check_more_order(item);  
	},
	
	check_more_order:async function(item){
		  var pickup=null,th=this;
		  var user_id=getApp().globalData.userInfo.user_id;
          wx.showLoading();
		  //--------获取门店-----------
		  await getApp().request.promiseGet("/api/weshop/pickup/get/" + os.stoid + "/" + item.pickup_id, {
		       
		  }).then(res=>{		
			   pickup = res.data.data;
		  })
          if(!pickup){
                wx.hideLoading();
                getApp().confirmBox("找不到门店");
                this.data.moring=0;
                return false;
          }

            if(pickup.isstop){
                wx.hideLoading();
                getApp().confirmBox("门店已停用");
                this.data.moring=0;
                return false;
            }

          if(!pickup.is_pos){
                wx.hideLoading();
                getApp().confirmBox("门店不允许销售");
                this.data.moring=0;
                return false;
          }
				
		  //--------获取商品----------
		  var goods_list=item.order_goods;	  
		  //--- 返回的内容 ---
		  var back_goods_arr=[];
		  var err_text="";	  
		  for(var i in goods_list){
				var g_item=goods_list[i];
				var b_item={};
			   //--如果是赠品，不加入购物车--
			  if(g_item['is_gift']) continue;		  
			  if(g_item['prom_type']==4 ){
			       err_text+= g_item['goods_name']+"积分购商品不加入购物车\n";
			       continue;
			  }
			  if(g_item['prom_type']==5){
				  err_text+= g_item['goods_name']+"搭配购商品不加入购物车\n";
				  continue;
			  }
			  if(g_item['prom_type']==6 ){
				  err_text+= g_item['goods_name']+"拼团商品不加入购物车\n";
				  continue;
			  }	
			  
			  //--获取商品--		
			  var good=null;				
			  await getApp().request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + g_item.goods_id, {
				  
			  }).then(res=>{			  
				  good=res.data.data;
				})
				
				//调用接口判断订单优惠，
				const res = await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromList/" + os.stoid + "/" + g_item.goods_id + "/0"+"/"+getApp().globalData.user_id, {})
				if (res.data.code == 0 && res.data.data) {
						var r_data = res.data.data;
						if (r_data.ladderLists) {
								var act_id = r_data.ladderLists[0].form_id;
								//-- 判断会员能不能参与阶梯促销 --
								const data = await getApp().request.promiseGet("/api/weshop/prom/ladderForm/getNew/" + os.stoid + "/" + user_id + "/" + act_id, {})
								if (data.data.code == 0 && data.data.data && data.data.data.isuse && data.data.data.is_end == 0) {
										b_item.prom_type = 10;
										b_item.prom_id = data.data.data.id;
								}
						}
				}
		  
			  if(!good){
				  err_text+= g_item['goods_name']+"未找到商品\n";
				  continue;
			  }		    
			  if (good.is_on_sale != 1) {
				  err_text+= g_item['goods_name']+"已经下架\n";
				  continue;		    
			  }
								
			  var timestamp = ut.gettimestamp();		  
			  if (good.on_time > timestamp) {
			    err_text+= g_item['goods_name']+"还未上架\n";
			    continue;	
			  }		  		  
			  if (good.down_time > 0) {
			    if (good.down_time < timestamp) {
			        err_text+=g_item['goods_name']+"已经下架\n";
			        continue;
			    }
			  }
			  
			  //如果商品的配送方式和门店不匹配
		      if(good['distr_type']>0 && pickup['distr_type']>0 && good['distr_type']!=pickup['distr_type'] ){
			     err_text+= good['goods_name']+"配送方式不一致\n";
			     continue;
		      }
			  
			  var prom=null;
			 
			  b_item.goods_id=g_item.goods_id;
			  b_item.goods_name=g_item.goods_name;
			  b_item.goods_num=g_item.goods_num;
			  b_item.guide_id=g_item.guide_id;
			  b_item.guide_type=g_item.guide_type;
			  b_item.pickup_id=item.pickup_id;
			  b_item.price=good.shop_price;
			  b_item.goods_sn=good.goods_sn;
			  b_item.sku=good.sku;
			  b_item.is_integral_normal=g_item.is_integral_normal;
			  
			  //-- 如果会员是等级卡的时候,同时商品也有设置相应的卡价格 --
			  var card_field=th.data.card_field;
			  if(card_field && good[card_field]>0){
			  	 b_item.price=good[card_field]; 
			  }

              var url="/api/weshop/activitylist/getGoodActInfo";
              var req_data={
                  store_id:g_item.store_id,goodsidlist:g_item.goods_id,is_detail:1,user_id:user_id
              };
              await getApp().request.promiseGet(url, {data:req_data}).then(res=>{
                  if(res.data.code==0){
                      var list = res.data.data;
                      for(let i in list){
                          let item=list[i];
                          if(item.prom_type==1){ good.prom_type=1; good.prom_id=item.act_id; }
                      }
                  }
              })

              if(!prom) {
                  //--判断商品当前的活动情况--
                  switch (good.prom_type) {
                      case 1:
                          var flash = null;
                          await getApp().request.promiseGet("/api/ms/flash_sale/getNew/" + os.stoid + "/" + user_id + "/" + good.prom_id, {}).then(res => {
                              if (res.data.code == 0) {
                                  flash = res.data.data;
                              }
                          })
                          //----已经结束-----
                          if (flash && flash.is_end == 0) {

                              if( flash.end_time > timestamp && flash.start_time < timestamp){
                                  prom = flash;
                                  prom.price = prom.user_price;
                                  b_item.price = prom.price;
                                  good.prom_type = 1;
                                  good.prom_id = prom.id;
                              }else{
                                  if(g_item.prom_type==0 && !flash.is_shop_buy ){
                                      err_text+=g_item['goods_name']+"秒杀活动不允许普通价\n";
                                      continue;
                                  }
                              }

                          }
                          break;
					  case 2:
					      var group = null;
					      await getApp().request.promiseGet("/api/weshop/goods/groupBuy/getActInfo/" + os.stoid + "/" + good.goods_id + "/" + good.prom_id, {}).then(res => {
					          if (res.data.code == 0) {
					              group = res.data.data;
					          }
					      })
					      //----已经结束-----
					      if (group && group.is_end == 0 && group.end_time > timestamp && group.start_time < timestamp) {
					          prom = group;
					          prom.price = prom.price;
					          b_item.price = prom.price;
					      }
					      break;		  
            case 6:
                b_item.is_pd_normal = 1;
                break;
            case 7:
                //如果有组合购
                var url = "/api/weshop/prom/zhbuy/get/" + os.stoid + "/" + good.prom_id + "/" + getApp().globalData.userInfo.user_id;
                await getApp().request.promiseGet(url, {}).then(res => {
                    if (res.data.code == 0 && res.data.data) {
                        if (res.data.data.is_show == 1 && res.data.data.is_end == 0
                            && ut.gettimestamp() < res.data.data.end_time
                            && ut.gettimestamp() > res.data.data.start_time
                        ) {
                            b_item.prom_type = 7;
                            b_item.prom_id = res.data.data.id;
                            good.prom_type = 7;
                            good.prom_id = res.data.data.id;
                        }
                    }
                })
								break;
						case 10:
							let user_id = getApp().globalData.userInfo.user_id;
							var url = `/api/weshop/prom/ladderForm/getNew/${os.stoid}/${user_id}/${good.prom_id}`;
							await getApp().request.promiseGet(url, {}).then(res => {
									if (res.data.code == 0 && res.data.data && res.data.data.isuse && res.data.data.is_end == 0) {
											b_item.prom_type = 10;
											b_item.prom_id = res.data.data.id;
									}
								})
							break;
            }
        }
			  
			  //如果有优惠促销的时候,要看下商品的优惠活动有没有过期
			  if((g_item.prom_type==3 || good.prom_type==3) && good.whsle_id<=0){
                  var is_g_prom=0;
                  //因为有全场优惠活动，商品参加的活动还未开始
                  var url="/api/weshop/activitylist/getGoodActInfo";
                  var req_data={
                      store_id:os.stoid,goodsidlist:good.goods_id,is_detail:1,user_id:user_id
                  };
                  await getApp().request.promiseGet(url, {data:req_data}).then(async function (res) {
                      if(res.data.code==0 && res.data.data && res.data.data.length){
                          for(let i in res.data.data){
                              let item=res.data.data[i];
                              if(item.prom_type==3) {
                                  is_g_prom=1; break;
                              }
                          }
                      }
                  })
                  if(is_g_prom) {
                      await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromList/" + os.stoid + "/" + good.goods_id + "/1"+"/"+getApp().globalData.user_id, {}).then(res => {
                          if (res.data.code == 0) {
                              var r_data = res.data.data;
                              if (r_data.promGoodsLists) {
                                  b_item.prom_type = 3;
                                  b_item.prom_id = r_data.promGoodsLists[0].prom_id;
                              }
                          }
                      })
                  }else{
                      await getApp().request.promiseGet("/api/weshop/goods/getGoodsPromNormal/" + os.stoid + "/" + good.goods_id, {}).then(res => {
                          if (res.data.code == 0) {
                              var r_data = res.data.data;
                              if (r_data.promGoodsLists) {
                                  b_item.prom_type = 3;
                                  b_item.prom_id = r_data.promGoodsLists[0].prom_id;
                              }
                          }
                      })
                  }
			  }
			  
			  			  
			  //判断是不是线下库存的购买，  是不是秒杀活动		  
			  if(th.data.sales_rules==2 && !prom){
				  
				  //-- 看一下购物车上有多少商品 --
				  var cart_num=0;				  
				  await getApp().request.promiseGet("/api/weshop/cart/page", {				  	
					 data: {
						store_id: os.stoid,
						user_id: getApp().globalData.user_id,
						state:0, is_gift:0,
						goods_id:good.goods_id,
						pick_id: b_item.pickup_id
					 }
				  }).then(res=>{
					  var num = 0;
					  if(res.data.code==0 && res.data.data && res.data.data.pageData) {
						  for (var i = 0; i < res.data.data.pageData.length; i++) {
							  num += res.data.data.pageData[i].goods_num;
						  }
					  }
					  cart_num=num;						 
				  })
				  		  
				  var lock=0,plist=null;

                  if(b_item.prom_type!=7) {
                      //先读取门店的lock,
                      await getApp().request.promiseGet("/api/weshop/order/ware/lock/page", {
                          data: {store_id: os.stoid, wareId: good.goods_id, storageId: b_item.pickup_id, pageSize: 1000}
                      }).then(res => {
                          if (res.data.code == 0 && res.data.data.total > 0) {
                              for (var i in res.data.data.pageData)
                                  lock += res.data.data.pageData[i].outQty
                          }
                      })

                      //---通过接口获取门店的线下库存信息--
                      await getApp().request.promiseGet("/api/weshop/goods/getWareStorages", {
                          data: {
                              storageNos: pickup.pickup_no,
                              wareIds: encodeURIComponent(good.erpwareid),
                              storeId: os.stoid
                          }
                      }).then(res => {
                          if (res.data.code == 0 && res.data.data.total > 0) {
                              plist = res.data.data.pageData[0];
                          }
                      })

                      if (!plist) {
                          err_text += good['goods_name'] + "库存不足\n";
                          continue;
                      }
                      if (plist.CanOutQty - lock <= 0) {
                          err_text += good['goods_name'] + "库存不足\n";
                          continue;
                      }
                      if (b_item.goods_num > (plist.CanOutQty - lock)) {
                          b_item.goods_num = plist.CanOutQty - lock;
                      }
                  }
				  var req_data={
				      store_id: os.stoid,
				      user_id: getApp().globalData.user_id,
				      goods_id: good.goods_id,
                      timetype:good.viplimited_timetype
				  };
                  if(b_item.prom_type==7){
                      req_data.prom_type=7;
                      req_data.prom_id=b_item.prom_id;
                  }
				  
				  var buynum=0;
                  var promnum=0;
				  //---要获得商品，该用户买了多少件,同步应用---
				  await getApp().request.promiseGet("/api/weshop/ordergoods/getUserBuyGoodsNum", {
				    data: req_data,
				  }).then(res => {					  
					  if(res.data.code==0){
						 var buy_num_data = res.data.data;
						 buynum = buy_num_data.goodsbuynum;
                          if(buy_num_data.promgoodsbuynum){
                              promnum=buy_num_data.promgoodsbuynum;
                          }
					  }					 				     
				  })
				  
				  //---- 要计算商品的限购 -----
				  if(good['viplimited']>0){ 				  												  
					  if(cart_num+buynum>good['viplimited']){
						   err_text+= good['goods_name']+"超出限购\n";
						   continue;
					  }				  
					  good['viplimited']-=(cart_num+buynum);
					  if(b_item.goods_num>good['viplimited']){
						  b_item.goods_num=good['viplimited'];
					  }			  				  		    				   
				  }
				  //不需要计算组合购的限购
				  
				  back_goods_arr.push(b_item);  //返回商品元素	
									
			  }else{
				  //-- 看一下购物车上有多少商品 --
				  var cart_num=0;				  
				  await getApp().request.promiseGet("/api/weshop/cart/page", {				  	
						 data: {
							store_id: os.stoid,
							user_id: getApp().globalData.user_id,
							state:0, is_gift:0,
							goods_id:good.goods_id			  					
						 }
				  }).then(res=>{
						  var num = 0;
						  if(res.data.code==0 && res.data.data && res.data.data.pageData) {
							  for (var i = 0; i < res.data.data.pageData.length; i++) {
								  num += res.data.data.pageData[i].goods_num;
							  }
						  }
						  cart_num=num;						 
				  })

				  if(good.store_count<=0){
					  err_text+= good['goods_name']+"库存不足\n";
					  continue; 
				  }
				  if(b_item.goods_num>good.store_count)
				      b_item.goods_num=good.store_count;
					  				  
			      var buynum=0;             //商品已经购买多少件
			      var promgoodsbuynum=0;	//活动已经购买多少件	
				  var redis_num = 0;        //redis库存
					
			      var req_data={
			          store_id: os.stoid,
			          user_id: getApp().globalData.user_id,
			          goods_id: good.goods_id,
                      timetype:good.viplimited_timetype
			      };
			      
			      if(prom){
			      		req_data.prom_type= good.prom_type;
			      		req_data.prom_id= good.prom_id;
						b_item.prom_type=good.prom_type;
						b_item.prom_id=good.prom_id;		
											
						//------判断活动是否抢光-----
						await getApp().request.promiseGet("/api/weshop/activitylist/getActLen/" +
						    os.stoid + "/" + good.prom_type + "/" + good.prom_id, {
						  1: 1
						}).then(res => {
						  redis_num = res.data.data;
						});										
						if(redis_num<=0){
                            //不允许商品普通价格修改
                            if(!prom.is_shop_buy && prom.is_shop_buy!=undefined) {
                                err_text += good['goods_name'] + "活动不足\n";
                                continue;
                            }
						}			
						
			      }
			      			 				  
			      //---要获得商品，该用户买了多少件,同步应用---
			      await getApp().request.promiseGet("/api/weshop/ordergoods/getUserBuyGoodsNum", {
			        data: req_data,
			      }).then(res => {					  
			      		if(res.data.code==0){
			      			var buy_num_data = res.data.data;
			      			if (buy_num_data.promgoodsbuynum) promgoodsbuynum = buy_num_data.promgoodsbuynum;
			      			buynum = buy_num_data.goodsbuynum;
			      		 }					 				     
			      })	
				  									  
				  //---- 要计算商品的限购 -----	  
				  if(good['viplimited']>0){ 				  												  
					  if(cart_num+buynum>good['viplimited']){
						   err_text+= good['goods_name']+"超出限购\n";
						   continue;
					  }				  
					  good['viplimited']-=(cart_num+buynum);
					  if(b_item.goods_num>good['viplimited']){
						  b_item.goods_num=good['viplimited'];
					  }
					  		    				   
				  }
				  
				  //-- 判断活动的库存 --
				  if(prom){				  
					   if(prom['goods_num']<=prom['buy_num']){
					      err_text+= good['goods_name']+"超出活动库存\n";continue;
					   }	
					
					   if(b_item.goods_num>prom['goods_num']-prom['buy_num']){
					      b_item['goods_num']= b_item['goods_num']-b_item['buy_num'];
					   }
														 
					   if(b_item.goods_num>redis_num){
						   b_item.goods_num=redis_num;
					   }

					   if(prom['buy_limit']>0){
                           if(cart_num+promgoodsbuynum>=prom['buy_limit'] ){
                               err_text+= good['goods_name']+"超出活动限购\n";continue;
                           }
                           prom['buy_limit'] -= (cart_num+buynum);
                           if(b_item.goods_num>prom['buy_limit']){
                               b_item.goods_num=prom['buy_limit'];
                           }
                       }
													  												 												  

				  }
				  back_goods_arr.push(b_item);  //返回商品元素			  					  
			  }		  			
		  }
		  
		  //有重新购买的数据是时候，就要重新生成订单	  
		  if(back_goods_arr.length>0){
			  th.setData({back_goods_arr:back_goods_arr});
			  if(!err_text){
				  th.more_add_cart();
			  }else{
				  wx.hideLoading();	
				  // 如果没有获取用户信息
				  wx.showModal({
				  	title:'确定',
				  	content: err_text+',确定要买其他商品码？',
				  	success(res) {
				  		if (res.confirm) {
				  			//跳转到登录页
				  			 th.more_add_cart();
				  		}else{
                            th.data.moring=0;
                            wx.hideLoading();
                        }
				  	},fail(){
                          th.data.moring=0;
                          wx.hideLoading();
					}
				  });
				  
			  }		  
		  }else{
              th.data.moring=0;
			  wx.hideLoading();	
			  getApp().confirmBox(err_text);  
		  }	  
	},
	
	more_add_cart:async function(){	 
		  var back_goods_arr=this.data.back_goods_arr;     
		  var err_text="";		
		  var ok_num=0;
						  				  
		  for(var i in back_goods_arr){
			  //商品ID
			  var g_item=back_goods_arr[i];		  
			  var newd = {
			    goods_id: g_item.goods_id,
			    goods_num: g_item.goods_num,
			    pick_id: g_item.pickup_id,
			    user_id: oo.user_id,
			    store_id: os.stoid,
			    goods_price: g_item.price,
			    member_goods_price: g_item.price,
			    goods_name: g_item.goods_name,
			    goods_sn: g_item.goods_sn,
			    sku: g_item.sku,
			  };
			  
			  if(g_item.guide_id){
				  newd['guide_id']=g_item.guide_id;
				  newd['guide_type']=g_item.guide_type;
			  }
			  
			  if(g_item.is_integral_normal){
			  	   newd['is_integral_normal']=g_item.is_integral_normal;
			  }  
			  if(g_item.is_pd_normal){
			  	   newd['is_pd_normal']=g_item.is_pd_normal;	  
			  }
			  if(g_item.prom_type){
			  		newd['prom_type']=g_item.prom_type;	  
			  		newd['prom_id']=g_item.prom_id;	  
			  }
			  
			  
			 
			  var b_cart_goods=null; 
			  await getApp().request.promiseGet("/api/weshop/cart/page", {
			    data: {
			      store_id: os.stoid,
			      user_id: oo.user_id,
			      goods_id: g_item.goods_id,
			      pick_id: g_item.pickup_id,
			    },
			  }).then(res=>{
				  //-------如果购物车中有相关的数据-------
				  if (res.data.data.total > 0) {		  
				     b_cart_goods= res.data.data.pageData[0];
				  }			  
			  })	
			 
			  //-- 如果就加入过购物车 --
			  if(b_cart_goods){		  
				  var updata = {
				     id: b_cart_goods.id,
				     goods_num: newd.goods_num + b_cart_goods.goods_num,
				     goods_price: newd.goods_price,
				     member_goods_price:newd.goods_price,
				     store_id: os.stoid,
				  };
				  if(newd.guide_id){
					 updata.guide_id=newd.guide_id;
					 updata.guide_type=newd.guide_type;
				  }	  
				  await getApp().request.promisePut("/api/weshop/cart/update", {
				     data: updata
				  }).then(res=>{
					  if(res.data.code==0) {
						  ok_num++;
					  }else{
						  err_text+=newd.goods_name+"加入购物车失败\n"; 
					  }
				  })	 
				  			  
			  }else{
				  await getApp().request.promisePost("/api/weshop/cart/save", {
				     data: newd
				  }).then(res=>{
					  if(res.data.code==0) {
					  	 ok_num++;
					  }else{
					  	err_text+=newd.goods_name+"加入购物车失败\n"; 
					  }			  				  
				  })				  
			  }		 			  
		  }		
								
		  wx.hideLoading();
          this.data.moring=0;
		  if(ok_num==0){
			  //-- 如果没有获取用户信息 --
			  wx.showModal({
			  	title:'提示',
			  	content: err_text,
			  	success(res) {}
			  });
		  }else{
			  if(err_text==""){
				  getApp().goto("/pages/cart/cart/cart");
			  }else{
				  // 如果没有获取用户信息
				  wx.showModal({
				  	title:'提示',
				  	content: err_text,
				  	success(res) {
				  		if (res.confirm) {
				  			//跳转到登录页
				  			getApp().goto("/pages/cart/cart/cart");
				  		}
				  	}
				  });
				  
			  }
			  
		  }
	},

	toast(msg){
		wx.showToast({
			title: msg,
			icon: 'none',
			duration: 3000
		})
	},

    con_weixin:function () {
        getApp().con_wx(this);
    },

    //跳转到商品
    go_gd:function (e) {
	    var user_id=getApp().globalData.user_id;
	    if(!user_id) user_id=0;

        var gd= e.currentTarget.dataset.gd;
        var good_url="/pages/goods/goodsInfo/goodsInfo?goods_id=" + gd;
        var url="/api/weshop/activitylist/getGoodActInfo";
        var req_data={
            store_id:os.stoid,goodsidlist:gd,is_detail:1,user_id:user_id
        };
        //获取秒杀的多规格
        getApp().request.promiseGet(url, {data:req_data}).then(res=>{
            if(res.data.code==0 && res.data.data){
                if(res.data.data.length==1){
                    var item=res.data.data[0];
                    switch (item.prom_type){
                     	case 9:
                            good_url= '/packageC/pages/luckyGo/luckyGo_goodsInfo/luckyGo_goodsInfo?goods_id=' + gd + '&group_id=' + item.act_id;
                     		break;
                     	case 8:
                            good_url="/packageC/pages/presell/goodsInfo/goodsInfo?goods_id="+gd+"&prom_id="+item.act_id
                     		break;
                     	default:
                            good_url="/pages/goods/goodsInfo/goodsInfo?goods_id="+gd+"&prom_id="+item.act_id+"&prom_type=1";
                     	 	break
                     }
                }
                getApp().goto(good_url);

            }else{
                getApp().goto(good_url);
            }
        })
    },



    click_contact:function () {
        getApp().globalData.no_clear=1;
    }




});