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
<template>
<el-table :data="socialUsers" :show-header="false">
<el-table-column label="社交平台" align="left" width="120">
<template slot-scope="scope">
<img style="height:20px;vertical-align: middle;" :src="scope.row.img" /> {{ scope.row.title }}
</template>
</el-table-column>
<el-table-column label="操作" align="left" >
<template slot-scope="scope">
<div v-if="scope.row.unionId">
已绑定
<el-button size="large" type="text" @click="unbind(scope.row)">(解绑)</el-button>
</div>
<div v-else>
未绑定
<el-button size="large" type="text" @click="bind(scope.row)">(绑定)</el-button>
</div>
</template>
</el-table-column>
</el-table>
</template>
<script>
import {SystemUserSocialTypeEnum} from "@/utils/constants";
import {socialAuthRedirect, socialBind, socialUnbind} from "@/api/login";
export default {
props: {
user: {
type: Object
},
getUser: { // 刷新用户
type: Function
},
setActiveTab: { // 设置激活的
type: Function
}
},
data() {
return {
};
},
computed: {
socialUsers (){
const socialUsers = [];
for (const i in SystemUserSocialTypeEnum) {
const socialUser = {...SystemUserSocialTypeEnum[i]};
socialUsers.push(socialUser);
if (this.user.socialUsers) {
for (const j in this.user.socialUsers) {
if (socialUser.type === this.user.socialUsers[j].type) {
socialUser.unionId = this.user.socialUsers[j].unionId;
break;
}
}
}
}
return socialUsers;
}
},
created() {
// 社交绑定
const type = this.$route.query.type;
const code = this.$route.query.code;
const state = this.$route.query.state;
if (!code) {
return;
}
socialBind(type, code, state).then(resp => {
this.$modal.msgSuccess("绑定成功");
this.$router.replace('/user/profile');
// 调用父组件, 刷新
this.getUser();
this.setActiveTab('userSocial');
});
},
methods: {
bind(socialUser) {
// 计算 redirectUri
const redirectUri = location.origin + '/user/profile?type=' + socialUser.type;
// 进行跳转
socialAuthRedirect(socialUser.type, encodeURIComponent(redirectUri)).then((res) => {
// console.log(res.url);
window.location.href = res.data;
});
},
unbind(socialUser) {
socialUnbind(socialUser.type, socialUser.unionId).then(resp => {
this.$modal.msgSuccess("解绑成功");
socialUser.unionId = undefined;
});
},
close() {
this.$tab.closePage();
}
}
};
</script>