togoin.js 5 KB
var t = require("../../utils/common.js"); 
var app=getApp();
var os = app.globalData.setting;

Page({
  data: {
    user:null,
	show_bind:0,
	imghots: os.imghost,
	store_logo:'',
	first_leader:'',   //-- 邀请人 --
  },
  onLoad: function (options) {
	   //--判断是否有接受到邀请人的ID--
	   if(options.first_leader){
	      this.setData({first_leader:options.first_leader});
	   }else if(getApp().globalData.first_leader){
		   this.setData({first_leader:getApp().globalData.first_leader});
		   
	   } 	  
	   var th=this;
	   app.getConfig(function (e) {
		    e.store_logo=th.data.imghots+e.store_logo;		   
	        th.setData({ store: e,store_logo:e.store_logo});
	   })	  
  },
  //绑定手机号
  getphone:function (e) {
	//接下来写业务代码登录
	e=e.detail;
	if (e.encryptedData==undefined){
	     return app.my_warnning("登录需要授权手机号码!", 0, this);
		 that.setData({show_bind:0})
	     return false;
	}  
    var th=this;  
    var sessionKey = getApp().globalData.sessionKey;
    var openid = getApp().globalData.openid;
	var r=getApp().globalData.getu;
  
	th.save_the_data(r,e,sessionKey,openid)
  },

  //-- 会员授权 --
  bindGetUserInfo: function(ee) {
    var that = this;
	//-- 点击授权 --
	that.getUserProfile(function(res){
		var user=res;
		//--拿下code--
		wx.login({
		  success: function (o) {
		    var dd = {
		      js_code:o.code,
			  store_id: os.stoid,
			  nickname: app.clear_word(app.globalData.getu.nickName),
			  head_pic: app.clear_word(app.globalData.getu.avatarUrl)      
		    };		
			//-- 导购会员ID --
			if(getApp().globalData.guide_id){
				dd.guide_id=getApp().globalData.guide_id;
			}
					
		    getApp().request.get("/api/weshop/users/openidandkey", {
		      data: dd,
		      success: function (e) {
		         if(e.data.code==0){				 
					 that.setData({user:user})				 
		             //如果有会员的话,没有sessionKey
		             if(!e.data.data.sessionKey){
						 getApp().showWarning("登录成功");
		                 getApp().globalData.userInfo=e.data.data;
						 getApp().globalData.user_id=e.data.data.user_id;
						 getApp().globalData.openid = e.data.data.weapp_openid;
						 //把会员的信息存在内存 
						 wx.setStorageSync("userinfo",e.data.data);
										 			 
		                 setTimeout(function () {
		                   wx.navigateBack({ delta: 1})  //返回上一页
		                 },1000)
		             }else{
		                getApp().globalData.sessionKey=e.data.data.sessionKey;
		                getApp().globalData.openid=e.data.data.openid;
						that.setData({show_bind:1});			
		             }
		         }else{
					   getApp().confirmBox(e.data.msg); 
				 }
		      }
		    });
		  }
		})	  
	})		
  },
  
  //-- 调整后的获取会员信息 --
  getUserProfile:function(func){
	  var that=this;	  
	  wx.getUserProfile({
	      desc:'获取用户相关信息',
	      success:res=>{
			  console.log("getUserProfile",res);
			  //此处授权得到userInfo
			  getApp().globalData.getu = res.userInfo;		
			  func(res.userInfo);		
	      }
	  });	 	 
  },
  
  //暂不登录,返回上一页
  cancle_bind:function () {
     wx.navigateBack({ delta: 1})
  },
    
  close_pop:function(){
	  this.setData({show_bind:0})
  },
  show_bind_mobile:function(){
	  this.setData({show_bind:1})
  },
  
  save_the_data:function (r,e,sessionKey,openid) {
    var app=getApp(),a=app,th=this;
    var dd = {
      oauth: "miniapp",
      nickname:a.clear_word(r.nickName),
      head_pic: r.avatarUrl,
      sex: r.gender,
      terminal: "miniapp",
      sessionKey: sessionKey,
      openid:openid,
      encryptedData: e.encryptedData,
      iv: e.iv,
      store_id: getApp().globalData.setting.stoid,
    };
  	 //--如果有邀请人的时候--
  	 if(th.data.first_leader){
  	 	dd.first_leader=th.data.first_leader;
  	 }	 
	 //-- 导购会员ID --
	 if(getApp().globalData.guide_id){
	 	dd.guide_id=getApp().globalData.guide_id;
	 }
	  
    console.log(dd);
    app.request.get("/api/weshop/users/thirdLogin", {
      data: dd,
      success: function (e) {
       
        if (e.data.code == 0) {
          app.globalData.user_id = e.data.data.user_id;
        } else {
          return app.showWarning("授权登入失败!"+e.data.msg);
        }
		 
        wx.setStorageSync("userinfo",e.data.data);
        wx.setStorageSync("isAuth", !0), app.globalData.userInfo = e.data.data, app.globalData.userInfo.head_pic = t.getFullUrl(a.globalData.userInfo.head_pic);
        wx.navigateBack({ delta: 1})
  
      },
      failStatus: function (t) {      
        return app.my_warnning("授权登入失败,请稍后再试!", 0, that);
      },
      fail: function (t) {
        return i.clearAuth(), i.alertLoginErrorAndGoHome(), !1;
      }
    });
  },

  bind_bnerr:function(e){
	  var _errImg = e.target.dataset.errorimg;
	  var _errObj = {};
	  _errObj[_errImg] = this.data.imghots+"/miniapp/images/logo.png";
	  this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
  }

})