最新版微信小程序适配登录方案(uniapp开发)

news/2024/7/20 2:15:22 标签: 小程序, javascript, vue
javascript"><template>
	<view class="content">
        <!-- 登录适配 -->
             <!-- 最新版登录方法 -->
			<button 
            v-if="canIUseGetUserProfile" 
            type='primary' lang="zh_CN" @tap="getUserProfile">
            </button>
            <!-- 老版本登录方法 -->
			<button v-else type='primary' 
            open-type="getUserInfo" lang="zh_CN" @getuserinfo="bindGetUserInfo">
            </button>
	</view>
<template>
<script>
	export default {
		data() {
			return {
                //用来判断用哪个登录
                canIUseGetUserProfile: false,
            }
       },
       //监听页面加载
		onLoad() {
            //如果手机支持最新版登录就用新方法
			if (wx.getUserProfile) {
				this.canIUseGetUserProfile = true
			 }
		},
        methods: {
            //老版登录接口(不再弹出登录弹框)
            bindGetUserInfo(e){
				if (e.detail.userInfo) {
					// console.log(e.detail.userInfo);
					uni.showLoading({
						title: '登录中'
					});
					//业务逻辑
					// console.log(res1);
					wx.login({
						success: res => {
							if (res.code) {
								//在此发起网络请求
								//请求后端给的登录接口,把res.code等相关参数带上
							} else {
								// console.log('登录失败!' + res.errMsg)
							}
						}
					})
				}
			},
            // 弹出登录弹框(新版)
			getUserProfile() {
			//推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息                                              均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
				uni.getUserProfile({
					desc: '用于获取您的个人信息', // s声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
					success: res1 => {
						uni.showLoading({
							title: '登录中'
						});
						//业务逻辑
						// console.log(res1);
						wx.login({
							success: res => {
								if (res.code) {
									//发起网络请求
                                    //请求后端给的登录接口,把res.code等相关参数带上
								} else {
									// console.log('登录失败!' + res.errMsg)
								}
							}
						})
					}
				})
			},
        }
   }


http://www.niftyadmin.cn/n/903023.html

相关文章

JavaScript跳出多种双层循环方法

第一种: for循环 for (let arr 0; arr < 20; arr) {for (let i 0; i < arr; i) {if (i 6) {// 结束循环优化性能return false;}}//最外层打印到5结束console.log(arr); } 第二种&#xff1a;新语法 forEach map let allList1 [{arr: [1,2,3]},{arr: [1,2,3]},{arr…

微信小程序web-view嵌入h5页面调用微信jssdk接口

业务需求&#xff1a;我需要用小程序web-view嵌入h5地图页面&#xff0c;然后点击h5页面导航功能调起小程序内置地图 第一步: 现在你的h5项目导入或者链接式导入微信jssdk vue导入: npm install weixin-js-sdk --save 链接式导入&#xff1a; http://res.wx.qq.com/open/…

js遍历出从某一年到最新年份

//获取到从那一年开始this.smallYears res.msg//获取当前时间var date new Date;this.nowYears date.getFullYear()var Years this.nowYears - this.smallYearsvar arrYear [];for (var i 0; i < Years; i) {arrYear.push(this.nowYears--)}arrYear.forEach(v> {//…

uniapp做微信小程序身份证识别功能(百度云身份证识别api)

html: <!-- 拍照识别 --><uni-popup ref"photograph" type"center" :animation"false"><view class"popup-content photographCenter":style"height: scrollheightspx;box-sizing: border-box;border-radius:0&q…

用js把百度经纬度转换成腾讯经纬度

// 百度坐标系转腾讯坐标bMapToQQMap(lng, lat) {let x_pi (3.14159265358979324 * 3000.0) / 180.0;let x lng - 0.0065;let y lat - 0.006;let z Math.sqrt(x * x y * y) - 0.00002 * Math.sin(y * x_pi);let theta Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);…

uniapp微信小程序动态更新scroll-view标签的高度

onReady() {//精确动态计算轮播图高度//自定义tabbar获取方法uni.getSystemInfo({success: res > {const query uni.createSelectorQuery()query.select(.tabbars).boundingClientRect()query.exec(res1 > {this.scrollheight res.windowHeight - res.statusBarHeight …

uniapp小程序自定义tabbar适配个别手机底部塌陷问题

塌陷场景&#xff1a; 此处已塌陷 解决方法&#xff1a; // 适配手机底部塌陷问题 padding-bottom: env(safe-area-inset-bottom); 完整css代码&#xff1a; .tabbars {position: fixed;z-index: 99;left: 0;right: 0;bottom: 0;display: flex;justify-content: center;alig…

uni-app微信小程序封装全局判断是否登录方法结合全局变量

新建的uni-app项目会有个app.vue文件&#xff0c;在此文件下 onLaunch应用生命周期封装一个方法(把全局变量定义一下): 注意&#xff1a;onLaunch应用生命周期只会触发一次 app.vue&#xff1a; <script>export default {//全局变量globalData: {//用于判断用户是否登录…