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

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
							});
						}
					},
				});
			},
相关推荐
show4333 小时前
多格式导出怎么做?2026免费小程序TXT/SRT实践
开发语言·小程序·c#
AI砖家3 小时前
AI编程—微信小程序开发规范
微信小程序·小程序·notepad++·ai编程
show43315 小时前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy1 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349301 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it1 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发
weikecms2 天前
星巴克API接口,星巴克点餐接口开发
大数据·小程序
游戏开发爱好者82 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
2501_915909062 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
千舟软件2 天前
千舟软件介绍 | 顺便聊聊我们做了哪些产品
大数据·数据库·科技·小程序·业界资讯