UserSocial.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <template>
  2. <el-table :data="socialUsers" :show-header="false">
  3. <el-table-column type="seq" title="序号" width="60" fixed="left" />
  4. <el-table-column label="社交平台" align="left" width="120">
  5. <template #default="{ row }">
  6. <img style="height: 20px; vertical-align: middle" :src="row.img" alt="" />
  7. {{ row.title }}
  8. </template>
  9. </el-table-column>
  10. <el-table-column label="操作" align="center">
  11. <template #default="{ row }">
  12. <template v-if="row.openid">
  13. 已绑定
  14. <XTextButton type="primary" @click="unbind(row)" title="(解绑)" />
  15. </template>
  16. <template v-else>
  17. 未绑定
  18. <XTextButton type="primary" @click="bind(row)" title="(绑定)" />
  19. </template>
  20. </template>
  21. </el-table-column>
  22. </el-table>
  23. </template>
  24. <script setup lang="ts">
  25. import { onMounted, ref } from 'vue'
  26. import { ElTable, ElTableColumn } from 'element-plus'
  27. import { SystemUserSocialTypeEnum } from '@/utils/constants'
  28. import { getUserProfileApi, ProfileVO } from '@/api/system/user/profile'
  29. import { socialAuthRedirect, socialUnbind } from '@/api/system/user/socialUser'
  30. import { ElMessage } from 'element-plus'
  31. const socialUsers = ref<any[]>([])
  32. const userInfo = ref<ProfileVO>()
  33. const initSocial = async () => {
  34. const res = await getUserProfileApi()
  35. userInfo.value = res
  36. for (const i in SystemUserSocialTypeEnum) {
  37. const socialUser = { ...SystemUserSocialTypeEnum[i] }
  38. socialUsers.value.push(socialUser)
  39. if (userInfo.value?.socialUsers) {
  40. for (const j in userInfo.value.socialUsers) {
  41. if (socialUser.type === userInfo.value.socialUsers[j].type) {
  42. socialUser.openid = userInfo.value.socialUsers[j].openid
  43. break
  44. }
  45. }
  46. }
  47. }
  48. }
  49. const bind = (row) => {
  50. const redirectUri = location.origin + '/user/profile?type=' + row.type
  51. // 进行跳转
  52. socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => {
  53. window.location.href = res.data
  54. })
  55. }
  56. const unbind = async (row) => {
  57. const res = await socialUnbind(row.type, row.openid)
  58. if (res) {
  59. row.openid = undefined
  60. }
  61. ElMessage.success('解绑成功')
  62. }
  63. onMounted(async () => {
  64. await initSocial()
  65. })
  66. </script>