get-user-info.vue
4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<view>
<page-head :title="title"></page-head>
<view class="uni-padding-wrap">
<view style="background:#FFF; padding:40rpx;">
<block v-if="hasUserInfo === false">
<view class="uni-hello-text uni-center">
<text>请点击下方按钮获取用户头像及昵称或手机号</text>
</view>
</block>
<block v-if="hasUserInfo === true">
<view class="uni-h4 uni-center uni-common-mt">{{userInfo.nickName || userInfo.nickname || userInfo.gender || userInfo.email || userInfo.phoneNumber}}</view>
<view v-if="userInfo.avatarUrl || userInfo.avatar_url " style="padding:30rpx 0; text-align:center;">
<image class="userinfo-avatar" :src="userInfo.avatarUrl||userInfo.avatar_url"></image>
</view>
</block>
</view>
<view class="uni-btn-v">
<!-- #ifdef APP-PLUS || MP-ALIPAY || MP-TOUTIAO -->
<button type="primary" :loading="btnLoading" @click="getUserInfo">获取用户信息</button>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-JD -->
<button type="primary" open-type="getUserInfo" @getuserinfo="mpGetUserInfo">获取用户信息</button>
<!-- #endif -->
<button @click="clear">清空</button>
</view>
</view>
</view>
</template>
<script>
import {
mapState,
mapMutations,
mapActions
} from 'vuex'
export default {
data() {
return {
title: 'getUserInfo',
hasUserInfo: false,
userInfo: {},
btnLoading: false
}
},
computed: {
...mapState([
'loginProvider',
'isUniverifyLogin'
])
},
methods: {
...mapActions(['getPhoneNumber']),
// 获取用户信息 API 在小程序可直接使用,在 5+App 里面需要先登录才能调用
getUserInfo() {
this.btnLoading = true;
if (this.isUniverifyLogin) {
// 一键登录
this.getPhoneNumber(uni.getStorageSync('univerifyInfo')).then(phoneNumber => {
this.hasUserInfo = true;
this.userInfo = {
phoneNumber
};
}).catch(err => {
console.error('getUserInfo fail:', err);
this.hasUserInfo = false;
}).finally(() => {
this.btnLoading = false;
})
return;
}
if(this.loginProvider === 'apple'){
const nickname = uni.getStorageSync('apple_nickname')
if(nickname){
this.hasUserInfo = true;
this.userInfo = { nickName:nickname }
this.btnLoading = false;
return;
}
}
uni.getUserInfo({
provider: this.loginProvider,
success: (result) => {
this.hasUserInfo = true;
this.userInfo = result.userInfo;
},
fail: (error) => {
console.log('getUserInfo fail', error);
let content = error.errMsg;
if (~content.indexOf('uni.login')) {
content = '请在登录页面完成登录操作';
}
// #ifndef APP-PLUS
uni.getSetting({
success: (res) => {
let authStatus = res.authSetting['scope.userInfo'];
if (!authStatus) {
uni.showModal({
title: '授权失败',
content: 'Hello uni-app需要获取您的用户信息,请在设置界面打开相关权限',
success: (res) => {
if (res.confirm) {
uni.openSetting()
}
}
})
} else {
uni.showModal({
title: '获取用户信息失败',
content: '错误原因' + content,
showCancel: false
});
}
}
})
// #endif
// #ifdef APP-PLUS
uni.showModal({
title: '获取用户信息失败',
content: '错误原因' + content,
showCancel: false
});
// #endif
},
complete: () => {
this.btnLoading = false;
}
});
},
mpGetUserInfo(result) {
console.log('mpGetUserInfo', result);
if (result.detail.errMsg !== 'getUserInfo:ok') {
uni.showModal({
title: '获取用户信息失败',
content: '错误原因' + result.detail.errMsg,
showCancel: false
});
return;
}
this.hasUserInfo = true;
if(result.detail && result.detail.userInfo){
this.userInfo = result.detail.userInfo;
}else{
// #ifdef MP-JD
this.userInfo = result.detail.user_info;
// #endif
}
},
clear() {
this.hasUserInfo = false;
this.userInfo = {};
}
}
}
</script>
<style>
.userinfo-avatar {
border-radius: 128rpx;
width: 128rpx;
height: 128rpx;
}
</style>