s_confirm.js 3.15 KB
var util = require('../../utils/util.js')
import App from '../../utils/api.js'
//测试
var app=getApp();
Page({
    data:{
        loading:false,
    },
    onLoad:function(opt){
		console.log(opt)
        app.scence=1;
		this.canClick=true;
		let query=decodeURIComponent(opt.scene);
        console.log(query)
		query.split('&').forEach(q=>{
		    let _q=q.split('=');
		    opt[_q[0]]=_q[1];
		})
        this.setData({
            reminder1:'成为店员后,顾客点餐时,',
            reminder2:'您将会收到交易通知',
        })
		this.opt=opt;
        if(util.cache('openid')){
            this.setData({
                openid:util.cache('openid'),
				loading:true
            })
        }else{
            this.getopenid();
        }

    },
	getopenid(){
		wx.login({
		    //获取code
		    success: (login_res)=>{
		        console.log('cc',login_res.code)
		        var data = {
		            code: login_res.code,
		            appid: 'wx418f1d7cdeda26c1',
		        }
		        //获取openid
				App.Wechat.GetSellerOpenidByCode(data).then((json)=>{
					var openid = JSON.parse(json.data[0]).openid;//返回openid
					this.setData({
					    openid:openid,
						loading:true
					})
					util.cache('openid',openid);
				})
		
		    },
			fail:(res)=>{
		        console.log('dddddd',res)
		    }
		})
	},
	//重新调起授权后,想接口获取信息
	login_start(iv,encryptedData){
	    console.log(iv,encryptedData)
	    var that=this;
	    wx.login({
	        //获取code
	        success: (login_res)=>{
	            console.log('cc',login_res.code)
	            var data = {
	                code: login_res.code,
	                appid: 'wx418f1d7cdeda26c1',
	                iv:iv,
	                encryptedData:encryptedData
	            }
				App.Wechat.WxLogin(data).then((json)=>{
					if(json.ret==200){
						console.log(json);
						if(json.data && json.data.openid){
							this.setData({
								openid:json.data.openid
							})
							util.cache('openid',json.data.openid);
							this.adduser();
						}
					}
				})
	        }
	    })
	
	},
	getuserinfo(e){
	    console.log(e);
	    if(e.detail && e.detail.errMsg=='getUserInfo:ok'){
			this.opt.user_name=e.detail.userInfo.nickName;
	        var iv=e.detail.iv;
	        var encryptedData=e.detail.encryptedData;
	        this.login_start(iv,encryptedData)
	    }else{
	        wx.showModal({
				title:'添加店员',
				content:'需要授权之后才能成为店员',
				confirmText:'我知道了',
				showConcel:false,
				confirmColor:'#199095',
				success:()=>{
					this.canClick=true;
				}
			})
	    }
	},
	adduser(){
		let parmdata={
			...this.opt,
			openid:this.data.openid
		}
		App.Seller.BatchAddUser(parmdata).then((json)=>{
			if(json.ret==200){
				wx.showToast({
					title:'添加成功',
					duration:2000,
					mask:true,
				})
				setTimeout(()=>{
					wx.redirectTo({
						url:'../me/s_index?sellerid='+this.opt.sellerid
					})
				},2000)
			}else{
				wx.showToast({
					title:'账号已存在',
					duration:2000,
					mask:true,
				})
				setTimeout(()=>{
					wx.redirectTo({
						url:'../me/s_index?sellerid='+this.opt.sellerid
					})
				},2000)
			}
		})
	}
})