微信小程序登录授权

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() { async login() {
// 从缓存中获取登录信息 // 从缓存中获取登录信息
let userInfo = uni.getStorageSync('userInfo'); let userInfo = uni.getStorageSync('userProfile');
if (userInfo) { if (userInfo) {
return; return true;
} }
// 获取微信登录凭证 // 获取微信登录凭证
@ -50,8 +50,16 @@ export default {
}); });
} else { } else {
userInfo = loginRes.userInfo; 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-between padding-bottom align-center solid-bottom">
<view class="flex justify-start align-center"> <view class="flex justify-start align-center">
<view class="cu-avatar round" <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="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="padding-xs text-sm">
<view class='cu-tag bg-white radius'>编辑</view> <view class='cu-tag bg-white radius'>编辑</view>
</view> --> </view> -->
@ -99,6 +99,17 @@
@click="chooseEntryType('serv')">服务商</view> @click="chooseEntryType('serv')">服务商</view>
</view> </view>
</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>
</view> </view>
</template> </template>
@ -144,29 +155,35 @@
color: 'olive', color: 'olive',
cuIcon: 'group', cuIcon: 'group',
pageUrl: '/pages/area-proxy/my-team' pageUrl: '/pages/area-proxy/my-team'
}], }],
curUserInfo: {},
myInfo: {}, myInfo: {},
isShowSteer: false isShowSteer: false,
isAuthWxLoginModal: false
} }
}, },
onReady() { onReady() {
this.loadData(); this.loadData();
}, },
methods: { methods: {
async loadData() { async loadData() {
this.myInfo = await this.$api.data('myInfo'); this.curUserInfo = uni.getStorageSync('userProfile');
this.isShowSteer = !this.myInfo.entryType; this.isAuthWxLoginModal = this.curUserInfo ? false : true;
let newServModules = this.servModules.concat(); if (!this.isAuthWxLoginModal) {
if (this.myInfo.hasCheckedRule) { this.myInfo = await this.$api.data('myInfo');
newServModules[0].pageUrl = '/pages/demand-center/accept-demand-center'; this.isShowSteer = !this.myInfo.entryType;
} else { let newServModules = this.servModules.concat();
let paramObj = { if (this.myInfo.hasCheckedRule) {
hasCheckedRule: this.myInfo.hasCheckedRule, newServModules[0].pageUrl = '/pages/demand-center/accept-demand-center';
navigate: true } 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) { chooseEntryType(typeCode) {
@ -182,6 +199,13 @@
uni.navigateTo({ uni.navigateTo({
url: '/pages/demand-center/rule?paramObj=' + encodeURIComponent(JSON.stringify(paramObj)) url: '/pages/demand-center/rule?paramObj=' + encodeURIComponent(JSON.stringify(paramObj))
}) })
},
authWxLogin() {
this.$commonFun.login().then(res => {
if (res) {
this.loadData();
}
})
} }
}, },
} }