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~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM