按官方通知调整微信登录

This commit is contained in:
chenbo 2021-04-20 14:54:43 +08:00
parent 441c1eaa68
commit 1929fc9c7d
3 changed files with 35 additions and 3 deletions

View File

@ -4,10 +4,17 @@ var user = require('../../../utils/user.js');
var app = getApp(); var app = getApp();
Page({ Page({
data: {
canIUseGetUserProfile: false, // 用于向前兼容
},
onLoad: function(options) { onLoad: function(options) {
// 页面初始化 options为页面跳转所带来的参数 // 页面初始化 options为页面跳转所带来的参数
// 页面渲染完成 // 页面渲染完成
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
}, },
onReady: function() { onReady: function() {
@ -23,6 +30,30 @@ Page({
// 页面关闭 // 页面关闭
}, },
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
user.checkLogin().catch(() => {
user.loginByWeixin(res.userInfo).then(res => {
app.globalData.hasLogin = true;
wx.navigateBack({
delta: 1
})
}).catch((err) => {
app.globalData.hasLogin = false;
util.showErrorToast('微信登录失败');
});
});
},
fail: (res) => {
app.globalData.hasLogin = false;
util.showErrorToast('微信登录失败');
}
});
},
wxLogin: function(e) { wxLogin: function(e) {
if (e.detail.userInfo == undefined) { if (e.detail.userInfo == undefined) {
app.globalData.hasLogin = false; app.globalData.hasLogin = false;

View File

@ -1,6 +1,7 @@
<view class="container"> <view class="container">
<view class="login-box"> <view class="login-box">
<button type="primary" open-type="getUserInfo" class="wx-login-btn" bindgetuserinfo="wxLogin">微信直接登录</button> <button wx:if="{{canIUseGetUserProfile}}" class="wx-login-btn" bindtap="getUserProfile">微信直接登录</button>
<button wx:else open-type="getUserInfo" class="wx-login-btn" bindgetuserinfo="wxLogin">微信直接登录</button>
<button type="primary" class="account-login-btn" bindtap="accountLogin">账号登录</button> <button type="primary" class="account-login-btn" bindtap="accountLogin">账号登录</button>
</view> </view>
</view> </view>

View File

@ -19,7 +19,7 @@
} }
}, },
"compileType": "miniprogram", "compileType": "miniprogram",
"libVersion": "2.9.4", "libVersion": "2.16.0",
"appid": "wx4901b319ace4f88a", "appid": "wx4901b319ace4f88a",
"projectname": "dts-shop", "projectname": "dts-shop",
"simulatorType": "wechat", "simulatorType": "wechat",