cb_wayadd.js 5.47 KB
//logs.js
var util = require('../../utils/util.js')
var uri=require('../../utils/uri.js')

Page({
    data: {
        wlist:[],
        selectunitindex:0,
        specs :[],
        wayindex : '',
        first:'1',
        buttonhide:true,
        atn_ishidden:true,
        auto_focus:false,
        inputvalue:'',
        dtn_ishidden:true
    },
    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("practice") && this.data.optid){
            util.cache('wayadd',true)
            this.cookdetail()
        }else{
            this.waylist();
        }
        var that=this;
        util.postSuccessFun=function(json,uri_code){
            switch(uri_code){
                case uri.CGI__USER_GETSELLERPRACTICEBYID__CODE:     //做法列表
                    if(json.ret==200){
                        var list=json.data[0];
                        if(util.cache('practice')){
                            var cachelist=util.cache('practice');
                            cachelist.forEach(function(v,i){
                                list.forEach(function(r,n){
                                    if(v.id==r.id){
                                        r.status=true
                                    }
                                })
                            })
                        }
                        that.setData({
                            waylist:list,
                        })
                        console.log(list)
                    }
                    break
                case  uri.CGI__USER_GETCOOKBOOKBYID__CODE:   //详情
                    var kindlist=json.data.data.practice;
                    var cachelist=[];
                    kindlist.forEach(function(v,i){
                        cachelist.push({
                            id:v.practice,
                            name:v.name,
                        })
                    })
                    if(cachelist.length>0){
                        util.cache('practice',cachelist)
                    }
                    that.waylist();
                break;
                case  uri.CGI__USER_ADDPRACTICE__CODE:   //添加做法
                    console.log(json)
                    if(json.ret==200){
                        that.setData({
                            atn_ishidden:true,
                            auto_focus:true,
                            inputvalue:''
                        })
                        that.waylist();
                    }else{
                        that.setData({
                            dtn_ishidden:false,
                            reminderInfo:json.msg.split(':')[1]
                        })
                    }
                break;

            }
        }
    },

    waylist:function(){
        util.post(uri.CGI__USER_GETSELLERPRACTICEBYID,{sellerid:this.data.sellerid},uri.CGI__USER_GETSELLERPRACTICEBYID__CODE)
    },
    cookdetail:function(){
        util.post(uri.CGI__USER_GETCOOKBOOKBYID,{id:this.data.optid},uri.CGI__USER_GETCOOKBOOKBYID__CODE);
    },
    switch1Change:function(e){
        var id=e.currentTarget.dataset.id;
        var waylist=this.data.waylist;
        var cachelist=util.cache('practice');
        waylist.forEach(function(v,i){
           if(v.id==id) {
               v.status=false
           }
        })
        cachelist.forEach(function(v,i){
            if(v.id==id){
                cachelist.splice(i,1)
            }
        })
        if(cachelist.length==0){
            wx.removeStorage({key:'practice'})
        }else{
            util.cache('practice',cachelist)
        }
        this.setData({
            waylist:waylist
        })
    },

    switch2Change:function(e){
        console.log('开',e)
        var id=e.currentTarget.dataset.id;
        var name=e.currentTarget.dataset.name;
        var waylist=this.data.waylist;
        var cachelist=util.cache('practice') ? util.cache('practice') : [];
        waylist.forEach(function(v,i){
            if(v.id==id) {
                v.status=true
            }
        })
        cachelist.push({
            id:id,
            name:name
        })
        util.cache('practice',cachelist)
        this.setData({
            waylist:waylist
        })
    },
    //添加做法
    addWayName:function(){
        this.setData({
            atn_ishidden:false,
            auto_focus:true
        })
    },
    //做法去除空格
    modalInput:function(e){
        var val=''
        //32为空格code码
        if(e.detail.keyCode==32){
            val=''
        }else{
            val=e.detail.value
        }
        this.setData({
            inputvalue:val
        })
    },
    //取消弹框
    cancelModel:function(){
        this.setData({
            atn_ishidden:true,
            auto_focus:true
        })
    },
    //确认添加做法
    confirmModel:function(){
        if(this.data.inputvalue!=''){
            util.post(uri.CGI__USER_ADDPRACTICE,{sellerid:this.data.sellerid,name:this.data.inputvalue},uri.CGI__USER_ADDPRACTICE__CODE);
        }else{
            this.setData({
                dtn_ishidden:false,
                reminderInfo:'菜品做法不能为空'
            })
        }

    },
    confirm:function(){
        this.setData({
            dtn_ishidden:true,
        })
    },
    netchange:function(){
        this.onShow();
    }
});