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

1.登录相关

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

参考地址:

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

代码

复制代码
login() {
				let _this = this;
				// 获取登录用户code

				uni.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
							});
						}
					},
				});
			},
相关推荐
为毛呀几秒前
小程序上传时出现缺少rules字段的报错
小程序
我是程序汪7 分钟前
程序汪3万18天接的塔防射击游戏小程序
游戏·小程序
qq_124987075312 小时前
基于微信小程序的线下点餐系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计
少云清18 小时前
【功能测试】5_小程序项目 _抓包修改轮播图(重要)
小程序
深红18 小时前
玩转小程序AR-基础篇
前端·微信小程序·webvr
從南走到北18 小时前
JAVA代驾小程序源码代驾跑腿APP源码
java·开发语言·微信·微信小程序·小程序
小小王app小程序开发19 小时前
招工招聘小程序运营风险清单:资质备案 + 信息审核 + 服务履约避坑实操
小程序
2501_9159214320 小时前
混合开发应用安全方案,在多技术栈融合下构建可持续、可回滚的保护体系
android·安全·ios·小程序·uni-app·iphone·webview
q_191328469521 小时前
基于SpringBoot2+Vue2+uniapp的考研社区论坛网站及小程序
java·vue.js·spring boot·后端·小程序·uni-app·毕业设计
pearbing21 小时前
多平台发力:小程序搜索优化的实用策略指南
小程序·小程序搜索排名·小程序优化·小程序搜索排名优化·小程序搜索优化·小程序seo