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

1151

被折叠的 条评论
为什么被折叠?



