userSocial.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <template>
  2. <el-table :data="socialUsers" :show-header="false">
  3. <el-table-column label="社交平台" align="left" width="120">
  4. <template v-slot="scope">
  5. <img style="height:20px;vertical-align: middle;" :src="scope.row.img" /> {{ scope.row.title }}
  6. </template>
  7. </el-table-column>
  8. <el-table-column label="操作" align="left" >
  9. <template v-slot="scope">
  10. <div v-if="scope.row.openid">
  11. 已绑定
  12. <el-button size="large" type="text" @click="unbind(scope.row)">(解绑)</el-button>
  13. </div>
  14. <div v-else>
  15. 未绑定
  16. <el-button size="large" type="text" @click="bind(scope.row)">(绑定)</el-button>
  17. </div>
  18. </template>
  19. </el-table-column>
  20. </el-table>
  21. </template>
  22. <script>
  23. import {SystemUserSocialTypeEnum} from "@/utils/constants";
  24. import {socialAuthRedirect} from "@/api/login";
  25. import {socialBind, socialUnbind} from "@/api/system/socialUser";
  26. export default {
  27. props: {
  28. user: {
  29. type: Object
  30. },
  31. getUser: { // 刷新用户
  32. type: Function
  33. },
  34. setActiveTab: { // 设置激活的
  35. type: Function
  36. }
  37. },
  38. data() {
  39. return {
  40. };
  41. },
  42. computed: {
  43. socialUsers (){
  44. const socialUsers = [];
  45. for (const i in SystemUserSocialTypeEnum) {
  46. const socialUser = {...SystemUserSocialTypeEnum[i]};
  47. socialUsers.push(socialUser);
  48. if (this.user.socialUsers) {
  49. for (const j in this.user.socialUsers) {
  50. if (socialUser.type === this.user.socialUsers[j].type) {
  51. socialUser.openid = this.user.socialUsers[j].openid;
  52. break;
  53. }
  54. }
  55. }
  56. }
  57. return socialUsers;
  58. }
  59. },
  60. created() {
  61. // 社交绑定
  62. const type = this.$route.query.type;
  63. const code = this.$route.query.code;
  64. const state = this.$route.query.state;
  65. if (!code) {
  66. return;
  67. }
  68. socialBind(type, code, state).then(resp => {
  69. this.$modal.msgSuccess("绑定成功");
  70. this.$router.replace('/user/profile');
  71. // 调用父组件, 刷新
  72. this.getUser();
  73. this.setActiveTab('userSocial');
  74. });
  75. },
  76. methods: {
  77. bind(socialUser) {
  78. // 计算 redirectUri
  79. const redirectUri = location.origin + '/user/profile?type=' + socialUser.type;
  80. // 进行跳转
  81. socialAuthRedirect(socialUser.type, encodeURIComponent(redirectUri)).then((res) => {
  82. // console.log(res.url);
  83. window.location.href = res.data;
  84. });
  85. },
  86. unbind(socialUser) {
  87. socialUnbind(socialUser.type, socialUser.openid).then(resp => {
  88. this.$modal.msgSuccess("解绑成功");
  89. socialUser.openid = undefined;
  90. });
  91. },
  92. close() {
  93. this.$tab.closePage();
  94. }
  95. }
  96. };
  97. </script>