uni-app微信登陆

<template>
	<div>
		登陆页面内容
		 
		<!-- #ifdef MP-WEIXIN -->
			<button @getuserinfo='getUserInfo' open-type="getUserInfo"   type="primary" class='login'>微信授权登录</button>
		<!-- #endif -->
	</div>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		methods: {
			getUserInfo(e) {
				let _this = this;
				let ret = e.detail;
				console.log(ret,'------------点击按钮')
				if (ret.errMsg == "getUserInfo:fail auth deny") {
					uni.showToast({
						title: '为了更好的服务,请同意授权',
						duration: 2000
					});
				} else if (ret.errMsg == "getUserInfo:ok") {
					_this.userLogin(ret);
				} else {
					uni.showToast({
						title: '授权失败',
						duration: 2000
					});
				}
			},
			userLogin(e) {
				let _this = this;
				uni.login({
					success: res => {
						let code = res.code;
						if (code) {
							//登录参数
							let d={
								appid:'wx4b6f4b1e5883f743',
								secret:'75ceb047014166507d9b5505ed2a278c',
								code:code
							}
							let data ='https://api.weixin.qq.com/sns/jscode2session?appid='+d.appid+'&secret='+d.secret+'&js_code='+res.code+'&grant_type=authorization_code';
							//调登录接口
							uni.request({
								url: data, //仅为示例,并非真实接口地址。
								data: {},
								header: {},
								success: (res) => {
									console.log(res,'---请求接口返回能力');
								}
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '没有获取到登陆信息,请重试',
							duration: 2000
						});
					}
				});
			}
		}
	}
</script>

<style>
.xj{
	border-radius: 8px;
}
</style>

注意这里一定要配置,不然在开发者攻击中运行会报错 forceUpdate
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值