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

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
							});
						}
					},
				});
			},
相关推荐
kyriewen15 小时前
一个人+Cursor,7天上线付费小程序:第1天我就想放弃了
前端·微信小程序·cursor
暗不需求18 小时前
从路虎汽车小程序看微信小程序开发的最佳实践
前端·javascript·微信小程序
博客zhu虎康19 小时前
小程序:解决小程序发布上线后无分享功能
小程序
tuanyuan99o1 天前
2026商城小程序的安全怎么保障?防止黑客攻击和数据泄露
安全·小程序
facetarzan1 天前
微信小程序文件下载
小程序·文件预览·文件下载
aiguangyuan1 天前
微信小程序服务商
微信小程序·前端开发
一支帆1 天前
微信小程序-情侣点餐
java·微信小程序·情侣点餐
Slow菜鸟1 天前
AI开发-微信小程序(全流程提示词)
人工智能·微信小程序
橘子海全栈攻城狮1 天前
【最新源码】鸟博士微信小程序 023
spring boot·后端·web安全·微信小程序·小程序
Yuujs1 天前
微信小程序反编译保姆级教程
微信小程序·小程序