UserSocial.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 class="h-5 align-middle" :src="row.img" alt="" />
  7. <p class="mr-5">{{ row.title }}</p>
  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" class="mr-5" @click="unbind(row)" title="(解绑)" />
  15. </template>
  16. <template v-else>
  17. 未绑定
  18. <XTextButton type="primary" class="mr-5" @click="bind(row)" title="(绑定)" />
  19. </template>
  20. </template>
  21. </el-table-column>
  22. </el-table>
  23. </template>
  24. <script setup lang="ts">
  25. import { SystemUserSocialTypeEnum } from '@/utils/constants'
  26. import { getUserProfileApi, ProfileVO } from '@/api/system/user/profile'
  27. import { socialAuthRedirect, socialUnbind } from '@/api/system/user/socialUser'
  28. const message = useMessage()
  29. const socialUsers = ref<any[]>([])
  30. const userInfo = ref<ProfileVO>()
  31. const initSocial = async () => {
  32. const res = await getUserProfileApi()
  33. userInfo.value = res
  34. for (const i in SystemUserSocialTypeEnum) {
  35. const socialUser = { ...SystemUserSocialTypeEnum[i] }
  36. socialUsers.value.push(socialUser)
  37. if (userInfo.value?.socialUsers) {
  38. for (const j in userInfo.value.socialUsers) {
  39. if (socialUser.type === userInfo.value.socialUsers[j].type) {
  40. socialUser.openid = userInfo.value.socialUsers[j].openid
  41. break
  42. }
  43. }
  44. }
  45. }
  46. }
  47. const bind = (row) => {
  48. const redirectUri = location.origin + '/user/profile?type=' + row.type
  49. // 进行跳转
  50. socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => {
  51. window.location.href = res.data
  52. })
  53. }
  54. const unbind = async (row) => {
  55. const res = await socialUnbind(row.type, row.openid)
  56. if (res) {
  57. row.openid = undefined
  58. }
  59. message.success('解绑成功')
  60. }
  61. onMounted(async () => {
  62. await initSocial()
  63. })
  64. </script>