扫微信小程序码实现网站登陆实现解析(2)

watch:{ userid:function(a,b){ if(a==0){ return } this.loaddata() //如果clientid是空的说明不是扫码进来的 if(!this.clientid){ return } //如果是扫码进来的,那么我们还需要推送消息到服务器后端 server.PublishMsg(this.clientid,this.userid,this.role,this.avatarUrl,this.nickName).then(res=>{ tip.error(res.msg) },res=>{ tip.error(res.msg) }) } }

获取sceneid

我们通过onload方法获得sceneid

onLoad(arg) { if(!!arg && !!arg.scene){ this.clientid = decodeURIComponent(arg.scene) } },

核心代码如下

通过gotUserInfo获得用户头像、昵称等,通过uni.login获得code

gotUserInfo :function(e) { //获得用户头像、昵称、 uni.login( { success:(res) =>{ //通过wx.login获得code userInfo.code = res.code //统一传递到后端 this.authwithcode(userInfo) } } ) } }, authwithcode:function(userInfo){ //code获得openid在后端做,做了后继续去查User表 server.AuthWithCode(userInfo).then(res=>{ //如果这个openid绑定了用户 if(res.data.id>0){ //这里res.data 就是user对象,可以继续操作 return ; } //如果没有那么注册 server.RegisterWithOpenId(res.data.mini_openid,userInfo.avatarUrl,userInfo.nickName)#.then(res=>{ //注册成功则返回 //这里res.data 就是user对象可以继续操作 },res=>{ tip.error(res.msg) }) },res=>{ tip.error(res.msg||""); }) } },

体验地址

本文所有应用体验地址如下

互联网行业解决方案吧

关于代码

代码获得地址

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9929b72ab2496b0028460d85b74780c7.html