前提:微信官方修改了 getUserInfo 接口,所以现在无法实现一进入微信小程序就弹出授权窗口,只能通过 button 去触发。
实现思路:
自定义封装一个页面底部的 tabBar切换组件,切换tab 时就实现了通过 button 组件去触发 getUserInof 接口。
判断openId存不存在,不存在,弹出授权弹窗,点击授权按钮,
1,通过wx.login获取code,
2,然后wx.request发送给后台,获取openid 以及用户信息
3,客户端使用wx.setstoragesync进行缓存
4,用wx.getstoragesync获取openid 如果存在,就已经登陆,不存在就未登陆(检验登陆态)
1,index.wxml
<!--index.wxml-->
<view class="container">
<view style="height:10rpx"></view>
<view class="banner">
<image src='../image/Banner.png' style="width:{{imgWidth}};height:{{imgHeight}}" bindload="imageLoad"></image>
</view>
<view class="content-box" bindtap="onGotoPage">
<image src='../image/order.png'></image>
快速下单
<text class="text">扫码/选择</text>
</view>
<view class="bottom-tab">
<bottom-tabs id="tabs"></bottom-tabs> //tab 组件
</view>
</view>
2,index.js
// component/bottom-tabs/bottom-tabs.js
const app = getApp();
Component({
properties: {
active: {
type: Number,
value: 0
}
},
data: {
urls: {
0: '/pages/index/index',
1: '/pages/search/search',
2: '/pages/my/my'
},
authorText: '',
authorBtn: '',
showAuthor: false,
showPhone: false,
storeNo: '',
storeName: ''
},
methods: {
onTabChange(e) {
const me = this,
idx = e.detail;
wx.redirectTo({
url: me.data.urls[idx]
});
me.triggerEvent("change", idx);
me.setData({
active: idx
})
},
showAuthorWin() {
this.setData({
showAuthor: true
})
},
closeAuthorWin() {
this.setData({
showAuthor: false
})
},
showPhoneWin() {
this.setData({
showPhone: true
});
},
getWeixiUser(e) {
const me = this;
if (me.data.storeNo) {
me.login();
} else {
app.scanStore().then(({
storeNo,
storeName
}) => {
if (!storeNo) {
wx.showModal({
content: '请扫描正确的二维码',
showCancel: false
});
} else {
me.setData({
storeNo: storeNo,
storeName: storeName || storeNo
});
me.login();
}
});
}
},
login() {
const me = this;
wx.showLoading({
title: '授权中...',
mask: true
});
wx.login({
success: re => {
//获取用户授权信息
wx.getUserInfo({
success: user => {
wx.request({
url: app.globalData.url + '/common/login',
method: 'POST',
header: {
'content-type': 'application/json' // 默认值
},
data: {
code: re.code,
rawData: JSON.parse(user.rawData),
encrypteData: user.encryptedData,
signature: user.signature,
iv: user.iv,
storeNo: me.data.storeNo,
flag: 1
},
complete: function (result) {
console.log(result)
wx.hideLoading();
try {
if (result.data.flag.retCode != '0') {
wx.showToast({
title: '授权失败,请重试',
icon: 'none'
})
return;
}
const data = result.data.data;
wx.setStorageSync('wxOpenId', data.wxOpenid || '');
wx.setStorageSync('ownerNo', data.ownerNo || '');
wx.setStorageSync('storeNo', data.defaultStore || me.data.storeNo);
wx.setStorageSync('storeName', data.defaultStoreName || me.data.storeName);
wx.setStorageSync('telephone', data.telephone || '');
wx.setStorageSync('nickName', data.nickname || user.userInfo.nickName);
wx.setStorageSync('userPic', data.avatarUrl || user.userInfo.avatarUrl);
app.initData();
me.setData({
showPhone: data.telephone ? false : true,
showAuthor: false
});
} catch (err) {
console.log(err);
wx.showToast({
title: '授权失败,请重试',
icon: 'none'
})
}
}
})
},
fail: error => {
console.log(error);
wx.hideLoading();
wx.showModal({
content: '请同意授权微信用户信息,否则无法使用部分功能',
showCancel: false
})
me.setData({
showAuthor: false
})
}
})
},
fail: error => {
console.log(error);
wx.hideLoading();
}
})
},
getPhone(e) {
const data = e.detail.encryptedData,
iv = e.detail.iv;
if (!data) return;
const param = {
encrypteData: data,
iv: iv
}
app.post('/common/updatePhone', param).then(({
data
}) => {
if (data.flag.retCode !== '0') {
wx.showToast({
title: data.flag.retMsg,
icon: 'none'
});
} else {
wx.setStorageSync('telephone', data.data.telephone);
app.globalData.userInfo.phone = data.data.telephone;
this.setData({
showPhone: false,
showAuthor: false
})
}
}).catch(error => {
wx.showModal({
content: error.message,
showCancel: false
})
})
}
},
lifetimes: {
attached() {
},
ready() {
const openId = app.globalData.openId,
storeNo = app.globalData.storeNo,
storeName = app.globalData.storeName,
text = storeNo ? '获取您公开信息(昵称、头像等)' : '请扫描配送点二维码进行绑定',
btn = storeNo ? '授权微信用户信息' : '确定并扫描';
this.setData({
authorText: text,
authorBtn: btn,
storeNo: storeNo,
storeName: storeName
});
if (!openId) {
this.setData({
showAuthor: true
})
}else if(!app.globalData.userInfo.phone){
this.showPhoneWin();
}
}
}
})
3,tab组件的封装
<!--component/bottom-tabs/bottom-tabs.wxml-->
<van-tabbar active="{{ active }}" bind:change="onTabChange">
<van-tabbar-item>
<image slot="icon" src="/pages/image/mail.png" mode="aspectFit" style="width: 30px; height: 18px;" />
<image slot="icon-active" src="/pages/image/mail-o.png" mode="aspectFit" style="width: 30px; height: 18px;" />
下单
</van-tabbar-item>
<van-tabbar-item>
<image slot="icon" src="/pages/image/search.png" mode="aspectFit" style="width: 30px; height: 18px;" />
<image slot="icon-active" src="/pages/image/search-o.png" mode="aspectFit" style="width: 30px; height: 18px;" />
查询</van-tabbar-item>
<van-tabbar-item>
<image slot="icon" src="/pages/image/person.png" mode="aspectFit" style="width: 30px; height: 18px;" />
<image slot="icon-active" src="/pages/image/person-o.png" mode="aspectFit" style="width: 30px; height: 18px;" />
我的</van-tabbar-item>
</van-tabbar>
<van-popup show="{{ showAuthor }}" closeable bind:close="closeAuthorWin">
<view class="author-wrapper">
<view>
{{authorText}}
</view>
<button open-type="getUserInfo" bindgetuserinfo="getWeixiUser">{{authorBtn}}</button>
</view>
</van-popup>
<van-popup show="{{ showPhone }}">
<view class="author-wrapper">
<view>
请授权获取微信电话号码
</view>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhone">确定</button>
</view>
</van-popup>
// component/bottom-tabs/bottom-tabs.js
const app = getApp();
Component({
properties: {
active: {
type: Number,
value: 0
}
},
data: {
urls: {
0: '/pages/index/index',
1: '/pages/search/search',
2: '/pages/my/my'
},
authorText: '',
authorBtn: '',
showAuthor: false,
showPhone: false,
storeNo: '',
storeName: ''
},
methods: {
onTabChange(e) {
const me = this,
idx = e.detail;
wx.redirectTo({
url: me.data.urls[idx]
});
me.triggerEvent("change", idx);
me.setData({
active: idx
})
},
showAuthorWin() {
this.setData({
showAuthor: true
})
},
closeAuthorWin() {
this.setData({
showAuthor: false
})
},
showPhoneWin() {
this.setData({
showPhone: true
});
},
getWeixiUser(e) {
const me = this;
if (me.data.storeNo) {
me.login();
} else {
app.scanStore().then(({
storeNo,
storeName
}) => {
if (!storeNo) {
wx.showModal({
content: '请扫描正确的二维码',
showCancel: false
});
} else {
me.setData({
storeNo: storeNo,
storeName: storeName || storeNo
});
me.login();
}
});
}
},
login() {
const me = this;
wx.showLoading({
title: '授权中...',
mask: true
});
wx.login({
success: re => {
//获取用户授权信息
wx.getUserInfo({
success: user => {
wx.request({
url: app.globalData.url + '/common/login',
method: 'POST',
header: {
'content-type': 'application/json' // 默认值
},
data: {
code: re.code,
rawData: JSON.parse(user.rawData),
encrypteData: user.encryptedData,
signature: user.signature,
iv: user.iv,
storeNo: me.data.storeNo,
flag: 1
},
complete: function (result) {
console.log(result)
wx.hideLoading();
try {
if (result.data.flag.retCode != '0') {
wx.showToast({
title: '授权失败,请重试',
icon: 'none'
})
return;
}
const data = result.data.data;
wx.setStorageSync('wxOpenId', data.wxOpenid || '');
wx.setStorageSync('ownerNo', data.ownerNo || '');
wx.setStorageSync('storeNo', data.defaultStore || me.data.storeNo);
wx.setStorageSync('storeName', data.defaultStoreName || me.data.storeName);
wx.setStorageSync('telephone', data.telephone || '');
wx.setStorageSync('nickName', data.nickname || user.userInfo.nickName);
wx.setStorageSync('userPic', data.avatarUrl || user.userInfo.avatarUrl);
app.initData();
me.setData({
showPhone: data.telephone ? false : true,
showAuthor: false
});
} catch (err) {
console.log(err);
wx.showToast({
title: '授权失败,请重试',
icon: 'none'
})
}
}
})
},
fail: error => {
console.log(error);
wx.hideLoading();
wx.showModal({
content: '请同意授权微信用户信息,否则无法使用部分功能',
showCancel: false
})
me.setData({
showAuthor: false
})
}
})
},
fail: error => {
console.log(error);
wx.hideLoading();
}
})
},
getPhone(e) {
const data = e.detail.encryptedData,
iv = e.detail.iv;
if (!data) return;
const param = {
encrypteData: data,
iv: iv
}
app.post('/common/updatePhone', param).then(({
data
}) => {
if (data.flag.retCode !== '0') {
wx.showToast({
title: data.flag.retMsg,
icon: 'none'
});
} else {
wx.setStorageSync('telephone', data.data.telephone);
app.globalData.userInfo.phone = data.data.telephone;
this.setData({
showPhone: false,
showAuthor: false
})
}
}).catch(error => {
wx.showModal({
content: error.message,
showCancel: false
})
})
}
},
lifetimes: {
attached() {
},
ready() {
const openId = app.globalData.openId,
storeNo = app.globalData.storeNo,
storeName = app.globalData.storeName,
text = storeNo ? '获取您公开信息(昵称、头像等)' : '请扫描配送点二维码进行绑定',
btn = storeNo ? '授权微信用户信息' : '确定并扫描';
this.setData({
authorText: text,
authorBtn: btn,
storeNo: storeNo,
storeName: storeName
});
if (!openId) {
this.setData({
showAuthor: true
})
}else if(!app.globalData.userInfo.phone){
this.showPhoneWin();
}
}
}
})