uniapp 微信授权登陆

准备工作:

1.微信开发者账号
2.AppId 、AppSecret (这些可以在开放平台申请到)

第一步

添加移动应用,仔细添加上述信息:

审批通过后,即刻

第二步

打开uniapp,开启OAuth,打开微信登陆
配置基本上完成。

代码

<button type="primary" @click="appLogin" class="weixin" plain="true">
   <image class="ico" src="../../static/login/wx.png" mode="scaleToFill"></image>
   微信登陆
</button>
export default {
		data() {
			return {

			}
		},
		onLoad(options) {
			console.log(options);
		},
		methods: {
			appLogin: function() {
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						console.log(res.provider);
						//支持微信、qq和微博等
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									console.log('-------获取openid(unionid)-----');
									console.log(JSON.stringify(loginRes));
									// 获取用户信息
									uni.getUserInfo({
										provider: 'weixin',
										success: function(infoRes) {
											console.log('-------获取微信用户所有-----');
											console.log(JSON.stringify(infoRes.userInfo));
										}
									});
								},
								fail: function(res) {
									console.log("App微信获取用户信息失败", res);
								}
							});
						}
					}
				});
			},
		}
	}
//请求登录接口方法
			getApploginData(data) {
				var that = this
				//这边是前端自己去调微信用户信息的接口,根据接口需要请求,如果不需要前端去获取的话就交给后端,可省去次操作
				uni.request({
					url: "https://api.weixin.qq.com/sns/userinfo?access_token=" + data.authResult.access_token + "&openid=" + data.authResult
						.openid,
					method: 'GET',
					dataType: 'json',
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值
					},
					success(res) {
						console.log('【登录回调啾啾啾】', res)
						that.$api.ajax('smdc/index/applogin', res.data, function(ret) {
							console.log("登录接口成功回调:", ret)
						}, 'POST', true)
					},
					fail() {

					}
				})
			}
以上就是APP登录的完整操作流程。over~
posted @ 2020-11-12 15:53  Homegu  阅读(10353)  评论(0编辑  收藏  举报