您的位置:首页 > 科技 > 能源 > 微信小程序记录(持续更新)

微信小程序记录(持续更新)

2024/10/5 23:22:45 来源:https://blog.csdn.net/weixin_43204464/article/details/141421873  浏览:    关键词:微信小程序记录(持续更新)

1.登录相关

登录简单来说就是用uni.login获取登陆凭证code。然后调用后台登录接口传递给后台,后台解码再返回token等一系列信息给前端

参考地址:

微信小程序获取用户手机号码教程(前端+后端)_微信小程序获取手机号-CSDN博客

代码

login() {let _this = this;// 获取登录用户codeuni.login({provider: 'weixin',success: function(res) {console.log(res,'res')if (res.code) {let code = res.code;//微信授权的人员信息uni.setStorageSync("wxUserInfo", _this.userInfo);uni.request({url: $newApi.AuthLoginByWeixin,method: 'GET',data: {code: code,// userInfo: _this.userInfo,tenant:_this.tenant},header: {'content-type': 'application/json', //自定义请求头信息'tenant':_this.tenant,							},success: function(res) {console.log("授权登录", res);uni.setStorageSync("userInfo", res.data.userInfo);_this.openid = res.data.userInfo.openid;if (res.data.token && res.data.token != '') {uni.setStorageSync("token", res.data.token);}if (_this.pageName == '/pages/home/studentHome') {uni.switchTab({url: _this.pageName})} else {//存在sessionId则直接去扫码的页面,没有的话就去获取手机号码if (res.data.userInfo.sessionId && res.data.userInfo.sessionId.length > 0) {console.log("if", _this.pageName)uni.navigateTo({url: _this.pageName,success(res) {console.log(res)},fail(err) {console.log(err);uni.switchTab({url: _this.pageName})}})} else {console.log("else")// _this.updateUserInfo()}}},fail: (error) => {console.log("授权登录失败", error)},complete: (res) => {}});} else {uni.showToast({title: '登录失败!',duration: 2000});}},});},

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com