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)
}
}
})
}
})
},
}
}