userSocial.vue 2.69 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2
<template>
  <el-table :data="socialUsers" :show-header="false">
Marcus's avatar
Marcus committed
3
    <el-table-column :label="$t('社交平台')" align="left" width="120">
sunhongwei's avatar
sunhongwei committed
4 5 6 7
      <template slot-scope="scope">
        <img style="height:20px;vertical-align: middle;" :src="scope.row.img" /> {{ scope.row.title }}
      </template>
    </el-table-column>
Marcus's avatar
Marcus committed
8
    <el-table-column :label="$t('操作')" align="left" >
sunhongwei's avatar
sunhongwei committed
9
      <template slot-scope="scope">
Marcus's avatar
Marcus committed
10
        <div v-if="scope.row.unionId">{{ $t('已绑定') }}<el-button size="large" type="text" @click="unbind(scope.row)">(解绑)</el-button>
sunhongwei's avatar
sunhongwei committed
11
        </div>
Marcus's avatar
Marcus committed
12
        <div v-else>{{ $t('未绑定') }}<el-button size="large" type="text" @click="bind(scope.row)">(绑定)</el-button>
sunhongwei's avatar
sunhongwei committed
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
        </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>