Skip to content

Commit 9ec6587

Browse files
committed
三方登录的绑定,使用 /system/social-user/get-bind-list 接口
1 parent 9819580 commit 9ec6587

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/api/system/socialUser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import request from "@/utils/request";
22

3+
// 获取用户绑定的社交用户列表
4+
export function getBindSocialUserList() {
5+
return request({
6+
url: '/system/social-user/get-bind-list',
7+
method: 'get'
8+
})
9+
}
10+
311
// 社交绑定,使用 code 授权码
412
export function socialBind(type, code, state) {
513
return request({

src/views/system/user/profile/userSocial.vue

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
import {SystemUserSocialTypeEnum} from "@/utils/constants";
2626
import {socialAuthRedirect} from "@/api/login";
27-
import {socialBind, socialUnbind} from "@/api/system/socialUser";
27+
import {socialBind, socialUnbind, getBindSocialUserList} from "@/api/system/socialUser";
2828
2929
export default {
3030
props: {
@@ -40,27 +40,15 @@ export default {
4040
},
4141
data() {
4242
return {
43+
socialUsers: []
4344
};
4445
},
4546
computed: {
46-
socialUsers (){
47-
const socialUsers = [];
48-
for (const i in SystemUserSocialTypeEnum) {
49-
const socialUser = {...SystemUserSocialTypeEnum[i]};
50-
socialUsers.push(socialUser);
51-
if (this.user.socialUsers) {
52-
for (const j in this.user.socialUsers) {
53-
if (socialUser.type === this.user.socialUsers[j].type) {
54-
socialUser.openid = this.user.socialUsers[j].openid;
55-
break;
56-
}
57-
}
58-
}
59-
}
60-
return socialUsers;
61-
}
6247
},
63-
created() {
48+
async created() {
49+
// 初始化社交用户列表
50+
await this.initSocial();
51+
6452
// 社交绑定
6553
const type = this.$route.query.type;
6654
const code = this.$route.query.code;
@@ -72,17 +60,35 @@ export default {
7260
this.$modal.msgSuccess("绑定成功");
7361
this.$router.replace('/user/profile');
7462
// 调用父组件, 刷新
75-
this.getUser();
7663
this.setActiveTab('userSocial');
64+
// 重新初始化社交用户列表
65+
this.initSocial();
7766
});
7867
},
7968
methods: {
69+
async initSocial() {
70+
this.socialUsers = []; // 重置避免无限增长
71+
// 获取已绑定的社交用户列表
72+
const bindSocialUserList = await getBindSocialUserList();
73+
// 检查该社交平台是否已绑定
74+
for (const i in SystemUserSocialTypeEnum) {
75+
const socialUser = { ...SystemUserSocialTypeEnum[i] };
76+
this.socialUsers.push(socialUser);
77+
if (bindSocialUserList && bindSocialUserList.data && bindSocialUserList.data.length > 0) {
78+
for (const bindUser of bindSocialUserList.data) {
79+
if (socialUser.type === bindUser.type) {
80+
socialUser.openid = bindUser.openid;
81+
break;
82+
}
83+
}
84+
}
85+
}
86+
},
8087
bind(socialUser) {
8188
// 计算 redirectUri
8289
const redirectUri = location.origin + '/user/profile?type=' + socialUser.type;
8390
// 进行跳转
8491
socialAuthRedirect(socialUser.type, encodeURIComponent(redirectUri)).then((res) => {
85-
// console.log(res.url);
8692
window.location.href = res.data;
8793
});
8894
},

0 commit comments

Comments
 (0)