cb_ggadd.js 9.19 KB
//logs.js
var util = require('../../utils/util.js')
var uri=require('../../utils/uri.js')

Page({
    data: {
        specs:[],
        slist:[],
    },
    onLoad:function(opt){
        var that=this;
        console.log('opt:',opt);
        util.setBarTitle('添加规格')
        this.setData({
          sellerid: util.cache("sellerid"),
          optid:opt.id
        })
    },
    onShow:function(){
        if(!util.cache('spec') && this.data.optid){
              this.cookdetail();
              util.cache('ggadd',true)
        }else{
              this.gglist();
        }
        var that=this;
        util.postSuccessFun=function(json,uri_code){
            switch(uri_code){
                case uri.CGI__USER_GETSELLERSPECIFICATIONSBYID__CODE:
                    if(json.ret==200){
                        var list=json.data[0];
                        list.forEach(function(v,i){
                            v.status=0
                        })
                    }
                    if(util.cache('spec')){
                        var spec=util.cache('spec');
                        spec.forEach(function(v,i){
                            list.forEach(function(r,n){
                                if(v.id==r.id){
                                    r.status=1;
                                    r.price=v.price;
                                    r.member_price=v.member_price;
                                }
                            })
                        })
                    }
                    that.setData({
                       gglist:list,
                    })

                    break
                case  uri.CGI__USER_GETCOOKBOOKBYID__CODE:   //详情
                    var spec=json.data.data.spec;
                    var cachelist=[];
                    spec.forEach(function(v,i){
                        cachelist.push({
                            id:v.specid,
                            specs:v.specs,
                            price:v.price,
                            member_price:v.member_price!=0 ? v.member_price : ''
                        })
                    })
                    if(cachelist.length>0){
                        util.cache('spec',cachelist)
                    }
                    that.gglist();

                break;

            }
        }
    },
    cookdetail:function(){
        util.post(uri.CGI__USER_GETCOOKBOOKBYID,{id:this.data.optid},uri.CGI__USER_GETCOOKBOOKBYID__CODE)
    },
    gglist:function(){
        util.post(uri.CGI__USER_GETSELLERSPECIFICATIONSBYID,{sellerid:this.data.sellerid},uri.CGI__USER_GETSELLERSPECIFICATIONSBYID__CODE)
    },
    //填写规格普通价
    priceModalInput:function(e){
        var id=e.currentTarget.dataset.id;
        var price=e.detail.value;
        if(e.detail.value!=''){
            if(price.indexOf('.')!=-1){
                if(price[0]=='.'){
                    if(price.length==1){
                        price='0'+price;
                    }else{
                        var arr=price.split('.');
                        if(arr[arr.length-1]!=''){
                            if(arr[0]=='' && arr[1]==''){
                                price='.'+arr[arr.length-1]
                            }
                        }else{
                            price='0.'+arr[1]
                        }

                    }
                }else{
                    var temp=price.slice(0,-1)
                    if(temp.indexOf('.')!=-1){
                        if(price[price.length-1]=='.'){
                            price=price.slice(0,-1);
                        }
                        if(price[price.length-1]!='.'){
                            var arr=price.split('.');
                            if(arr[1].length>2){
                                price=arr[0]+'.'+arr[1].slice(0,2)
                            }
                        }
                    }
                }
            }
        }
        var gglist=this.data.gglist;
        var cachelist=util.cache('spec');
        if(price!=''){
            if(price.indexOf('.')==-1 && Number(price)==0){
                price=0
            }
        }

        cachelist.forEach(function(v,i){
            if(v.id==id){
                v.price=price;
            }
        })
        gglist.forEach(function(v,i){
            if(v.id==id){
                v.price=price;
            }
        })
        this.setData({
            gglist:gglist
        })
        util.cache('spec',cachelist)
    },


    //价格判断
    condition:function(e){
        var  _key = e.currentTarget.dataset.id;
        var  _name = e.currentTarget.dataset.name;
        var _value = e.detail.value;
        if(_value.indexOf('.')!=-1){
            if(_value[_value[_value.length-1]]=='.'){
                _value+='00'
            }else{
                if(Number(_value)==0){
                    _value=0
                }
            }
        }else{
            if(Number(_value)==0){
                _value=0
            }
        }
        var gglist=this.data.gglist;
        var cachelist=util.cache('spec');
        cachelist.forEach(function(v,i){
            if(v.id==_key){
                v[_name]=_value;
            }
        })
        gglist.forEach(function(v,i){
            if(v.id==_key){
                v[_name]=_value;
            }
        })
        this.setData({
            gglist:gglist
        })
        util.cache('spec',cachelist)
        if(_value==0){
            this.setData({
                dtn_ishidden:false,
                reminderInfo:_name+'价格不能为0'
            })
        }else if(_value==''){
            this.setData({
                dtn_ishidden:false,
                reminderInfo:_name+'价格不能为空'
            })
        }
    },
    confirm:function(){
        this.setData({
            dtn_ishidden:true,
        })
    },

    //填写规格会员价
    memberpriceModalInput:function(e){
        var id=e.currentTarget.dataset.id;
        var member_price=e.detail.value;
        if(e.detail.value!=''){
            if(member_price.indexOf('.')!=-1){
                if(member_price[0]=='.'){
                    console.log('xxxx')
                    if(member_price.length==1){
                        member_price='0'+member_price;
                    }else{
                        var arr=member_price.split('.');
                        if(arr[arr.length-1]!=''){
                            if(arr[0]=='' && arr[1]==''){
                                member_price='.'+arr[arr.length-1]
                            }
                        }else{
                            member_price='0.'+arr[1]
                        }

                    }
                }else{
                    var temp=member_price.slice(0,-1)
                    if(temp.indexOf('.')!=-1){
                        if(member_price[member_price.length-1]=='.'){
                            member_price=member_price.slice(0,-1);
                        }
                        if(member_price[member_price.length-1]!='.'){
                            var arr=member_price.split('.');
                            if(arr[1].length>2){
                                member_price=arr[0]+'.'+arr[1].slice(0,2)
                            }
                        }
                    }
                }
            }
        }
        if(member_price.indexOf('.')==-1 && Number(member_price)==0){
            member_price=0
        }
        var gglist=this.data.gglist;
        var cachelist=util.cache('spec');
        cachelist.forEach(function(v,i){
            if(v.id==id){
                v.member_price=member_price;
            }
        })
        gglist.forEach(function(v,i){
            if(v.id==id){
                v.member_price=member_price;
            }
        })
        this.setData({
            gglist:gglist
        })
        util.cache('spec',cachelist)
    },

    //关闭规格选项
    switch1Change: function (e) {
      var id=e.currentTarget.dataset.id;
      var gglist=this.data.gglist;
      var cachelist=util.cache('spec');
      gglist.forEach(function(v,i){
          if(v.id==id){
              v.status=0
          }
      })
      cachelist.forEach(function(v,i){
         if(v.id==id){
             cachelist.splice(i,1)
         }
      })
      if(cachelist.length>0){
          util.cache('spec',cachelist)
      }else{
          wx.removeStorage({key:'spec'})
      }
      this.setData({
          gglist:gglist
      })

    },

    //开启规格项
    switch2Change:function(e){
        var id=e.currentTarget.dataset.id;
        var name=e.currentTarget.dataset.name;
        var gglist=this.data.gglist;
        var cachelist=util.cache('spec') ? util.cache('spec') : [];
        var price=0;
        var member_price=0;
        gglist.forEach(function(v,i){
            if(v.id==id){
                v.status=1;
                price=v.price ? v.price : 0
                member_price=v.member_price ? v.member_price : 0
            }
        })
        cachelist.push({
            id:id,
            specs:name,
            price:price,
            member_price:member_price
        })
        util.cache('spec',cachelist)
        this.setData({
            gglist:gglist
        })
    },
    netchange:function(){
        this.onShow();
    }
});