s_book.js 6.58 KB
var util = require('../../utils/util.js')
var uri=require('../../utils/uri.js')
var app=getApp();
Page({
    data:{
        canClick:true,
    },
    onLoad:function(opt){
        app.scence=1
        let query=decodeURIComponent(opt.scene)
        let arr=query.split(/[=&]/);
        console.log(query,arr)
        this.setData({
            sellerid:opt.sellerid ? opt.sellerid : arr[1],
            role_id:opt.role_id ? opt.role_id : arr[3]
        })
        this.getstoreInfo();
        if(util.cache('openid')){
            this.setData({
                openid:util.cache('openid'),
                canClick:false
            })
        }else{
            var that=this;
            wx.login({
                //获取code
                success: function(login_res) {
                    console.log('cc',login_res.code)
                    var data = {
                        code: login_res.code,
                        appid: 'wx28fa5be22ae0ad55',
                    }
                    //获取openid
                    wx.request({
                        url: "https://dcapi.colorcun.com/?service=App.Wechat.GetSellerOpenidByCode",
                        data: data,
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function(res) {
                            console.log('zzzzzz')
                            console.log(res)
                            var openid = JSON.parse(res.data.data[0]).openid;//返回openid
                            util.cache('openid',openid)
                            that.setData({
                                openid:openid,
                                canClick:false,
                            })



                        },
                        complete:function (res) {
                            var openid = JSON.parse(res.data.data[0]).openid;//返回openid
                            util.cache('openid',openid)
                        }
                    })

                },fail:function(res){
                    console.log('dddddd',res)
                }
            })
        }
    },
    onShow:function(){
        var that=this;
        util.postSuccessFun=function(json,uri_code){
            switch (uri_code){
                case  uri.CGI__USER_GETSELLERBYID__CODE:
                    if(json.ret==200){
                        that.setData({
                            sellername:json.data.data.sellername
                        })
                    }
                break;
                case uri.CGI_USER_BATCHADDUSER_CODE:
                    if(json.ret==200){
                        wx.hideToast();
                        wx.showToast({
                            title:'绑定成功',
                            duration:2000,
                            mask:true,
                            success:function () {
                                setTimeout(function(){
                                    wx.reLaunch({
                                        url:'../me/s_index?sellerid='+that.data.sellerid
                                    })
                                },2000)
                            }
                        })
                        wx.reLaunch({
                            url:'../me/s_index?sellerid='+that.data.sellerid
                        })
                    }else{
                        wx.showToast({
                            title:'该账号已存在',
                            icon:'none',
                            duration:2000,
                            success:function () {
                               setTimeout(function(){
                                   wx.reLaunch({
                                       url:'../me/s_index?sellerid='+that.data.sellerid
                                   })
                               },2000)
                            }
                        })

                    }
                break;

            }
        }
    },
    //重新调起授权后,想接口获取信息
    login_start:function(iv,encryptedData){
        console.log(iv,encryptedData)
        var that=this;
        wx.login({
            //获取code
            success: function(login_res) {
                console.log('cc',login_res.code,iv,encryptedData)
                if(login_res.code){
                    var data = {
                        code: login_res.code,
                        appid: 'wx28fa5be22ae0ad55',
                        //secret: 'cf7059061bb79d764a9bba55d7197b12',
                        iv:iv,
                        encryptedData:encryptedData
                    }
                    //获取openid
                    wx.request({
                        url: "https://xt.colorcun.com/WechatApi/Api/dcLogin",
                        data: data,
                        header: {
                            'content-type': 'application/json'
                        },
                        success: function(res) {
                            that.adduser();
                        },
                        fail:function () {
                            that.login_start(iv,encryptedData)
                        }
                    })
                }else{
                    that.login_start(iv,encryptedData)
                }


            }
        })

    },
    //通过sellerid获取商家信息
    getstoreInfo:function(){
        util.post(uri.CGI__USER_GETSELLERBYID,{sellerid:this.data.sellerid},uri.CGI__USER_GETSELLERBYID__CODE)
    },
    adduser:function(){
        var data={
            sellerid:this.data.sellerid,
            user_name:this.data.user_name,
            openid:this.data.openid,
            role_id:this.data.role_id,
        }
        util.post(uri.CGI_USER_BATCHADDUSER,data,uri.CGI_USER_BATCHADDUSER_CODE)
    },
    userInfoHandler: function (e) {
        var that = this;
        var etarget = e;
        console.log(etarget)
        if(util.userInfoHandler(etarget)==true){
            console.log('xxxx',e)
            this.setData({
                canClick:true,
                user_name:etarget.detail.userInfo.nickName
            })
            var iv=etarget.detail.iv;
            var encryptedData=etarget.detail.encryptedData;
            var nickName=etarget.detail.userInfo.nickName;
            wx.showToast({
                title:'正在绑定',
                icon:'loading',
                mask:true,
                duration:10000
            })
            this.login_start(iv,encryptedData);
        }else{
            this.setData({
                canClick:false
            })
        }
    },
})