微信,钉钉小程序账号绑定微信id【免密登录】

文章讲述了在微信和钉钉小程序中获取code值,通过后端接口获取并存储用户的openid和dingId,检查用户是否已绑定,未绑定时引导用户进行相应操作的过程。

1.先获取微信小程序和钉钉小程序的code值

async getcode(){
				const wxLoginRes = await uni.login({provider: "weixin"})//微信
				const ddLoginRes = await uni.login({provider: "alipay"})//钉钉
				console.log(wxLoginRes.code,'weixin')
				console.log(ddLoginRes,'alipay')
				if(!wxLoginRes.code){
					this.$show("获取微信code失败", 'none');
										return;
				}else{
					// #ifdef MP-WEIXIN
					uni.setStorageSync('wxcode',wxLoginRes.code)
					this.authCode=wxLoginRes.code
					// #endif
					
					// #ifdef MP-ALIPAY
					uni.setStorageSync('ddcode',ddLoginRes.code)
					this.authCode=ddLoginRes.code
					// #endif
					console.log(this.authCode)
					// #ifdef MP-ALIPAY
					this.getDingId()
					// #endif
					// #ifdef MP-WEIXIN
					this.getWechatId()
					// #endif
				}
			},

2.获取微信小程序和钉钉小程序的id,需要走后端接口 要把微信小程序的appid和钉钉小程序的dingid配置到后端接口文件里,不然绑定不成功

// #ifdef MP-ALIPAY  
			getDingId(){
				getDingId({authCode:this.authCode}).then(res=>{
					console.log(res,'获取钉钉小程序的id')					if(res.data.code==0){
						this.dingdId=res.data.data.dingid
						uni.setStorageSync('dingdingId',res.data.data.dingid)
						this.dingdcheckUserIsBind()
					}
				})
			},
				// #endif
				
				// #ifdef MP-WEIXIN
			getWechatId(){
				getWechatId({authCode:this.authCode}).then(res=>{
					console.log(res,'获取微信小程序的id')
					if(res.data.code==0){
						this.wechatId=res.data.data.openid
						uni.setStorageSync('wechatId',res.data.data.openid)
						this.checkUserIsBind() 
					}
				})
			},
			// #endif

3.此时微信id和钉钉id都已经获取到,页面初始化加载的时候把微信id和钉钉id用作参数传回后端,判断返回值是否有信息,有信息代表已绑定,直接进入下一个页面即可,没有返回值则没有绑定,需要走登录页面进行登录

// #ifdef MP-WEIXIN
			checkUserIsBind(){
				checkUser({wechatId:this.wechatId}).then(res=>{
					console.log(res)
					if(res.data.data==null){
						uni.redirectTo({
							url:'/pages/noregist/noregist'
						})
					}else{
						uni.setStorageSync('token',res.data.data.token)
						uni.redirectTo({
							url:'/pages/index/index?type='+res.data.data.type
						})
					}
				})
			}
		// #endif
		// #ifdef MP-ALIPAY
		   dingdcheckUserIsBind(){
		   	dingdcheckUser({dingId:this.dingdId}).then(res=>{
		   		console.log(res,'token')
		   		if(res.data.data==null){
		   			uni.redirectTo({
		   				url:'/subpackages/pages/bind/bind'
		   			})
		   		}else{
		   			uni.setStorageSync('token',res.data.data.token)
		   			uni.redirectTo({
		   				url:'/pages/index/index?type='+res.data.data.type
		   			})
		   		}
		   	})
		   }
		// #endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值