2424
2525import {SystemUserSocialTypeEnum } from " @/utils/constants" ;
2626import {socialAuthRedirect } from " @/api/login" ;
27- import {socialBind , socialUnbind } from " @/api/system/socialUser" ;
27+ import {socialBind , socialUnbind , getBindSocialUserList } from " @/api/system/socialUser" ;
2828
2929export 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