微信小程序登录授权

This commit is contained in:
donqi 2022-05-09 23:10:42 +08:00
parent 6cd952abce
commit 3da0fdfcb6
2 changed files with 52 additions and 20 deletions

View File

@ -31,9 +31,9 @@ export default {
},
async login() {
// 从缓存中获取登录信息
let userInfo = uni.getStorageSync('userInfo');
let userInfo = uni.getStorageSync('userProfile');
if (userInfo) {
return;
return true;
}
// 获取微信登录凭证
@ -51,7 +51,15 @@ export default {
} else {
userInfo = loginRes.userInfo;
}
if (!userInfo) {
uni.showToast({
icon: 'error',
title: '微信用户信息获取失败,请退出小程序重试'
})
return false;
}
// 页面存储用户登录有效信息,以便其他页面调用
uni.setStorageSync('userInfo', userInfo);
uni.setStorageSync('userProfile', userInfo);
return true;
}
}

View File

@ -5,9 +5,9 @@
<view class="flex justify-between padding-bottom align-center solid-bottom">
<view class="flex justify-start align-center">
<view class="cu-avatar round"
:style="'width: 120rpx; height: 120rpx; background-image:url(' + myInfo.picUrl + ');'"></view>
:style="'width: 120rpx; height: 120rpx; background-image:url(' + curUserInfo.userInfo.avatarUrl + ');'"></view>
<view class="margin-lr-sm">
<view class="text-xl margin-bottom-xs">{{myInfo.name}}</view>
<view class="text-xl margin-bottom-xs">{{curUserInfo.userInfo.nickName}}</view>
<!-- <view class="padding-xs text-sm">
<view class='cu-tag bg-white radius'>编辑</view>
</view> -->
@ -100,6 +100,17 @@
</view>
</view>
</view>
<!-- 强制要求用户授权登录的弹窗 -->
<view class="cu-modal" :class="isAuthWxLoginModal?'show':''">
<view class="cu-dialog">
<view class="padding-xl">
需先授权微信登录才可正常使用功能
</view>
<view class="cu-bar bg-white">
<view class="action margin-0 flex-sub text-main-color solid-left" @tap="authWxLogin">确认授权</view>
</view>
</view>
</view>
</view>
</template>
@ -145,8 +156,10 @@
cuIcon: 'group',
pageUrl: '/pages/area-proxy/my-team'
}],
curUserInfo: {},
myInfo: {},
isShowSteer: false
isShowSteer: false,
isAuthWxLoginModal: false
}
},
onReady() {
@ -154,19 +167,23 @@
},
methods: {
async loadData() {
this.myInfo = await this.$api.data('myInfo');
this.isShowSteer = !this.myInfo.entryType;
let newServModules = this.servModules.concat();
if (this.myInfo.hasCheckedRule) {
newServModules[0].pageUrl = '/pages/demand-center/accept-demand-center';
} else {
let paramObj = {
hasCheckedRule: this.myInfo.hasCheckedRule,
navigate: true
this.curUserInfo = uni.getStorageSync('userProfile');
this.isAuthWxLoginModal = this.curUserInfo ? false : true;
if (!this.isAuthWxLoginModal) {
this.myInfo = await this.$api.data('myInfo');
this.isShowSteer = !this.myInfo.entryType;
let newServModules = this.servModules.concat();
if (this.myInfo.hasCheckedRule) {
newServModules[0].pageUrl = '/pages/demand-center/accept-demand-center';
} else {
let paramObj = {
hasCheckedRule: this.myInfo.hasCheckedRule,
navigate: true
}
newServModules[0].pageUrl = '/pages/demand-center/rule?paramObj=' + encodeURIComponent(JSON.stringify(paramObj));
}
newServModules[0].pageUrl = '/pages/demand-center/rule?paramObj=' + encodeURIComponent(JSON.stringify(paramObj));
this.servModules = newServModules;
}
this.servModules = newServModules;
},
chooseEntryType(typeCode) {
@ -182,6 +199,13 @@
uni.navigateTo({
url: '/pages/demand-center/rule?paramObj=' + encodeURIComponent(JSON.stringify(paramObj))
})
},
authWxLogin() {
this.$commonFun.login().then(res => {
if (res) {
this.loadData();
}
})
}
},
}