UserSocial.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <el-table :data="socialUsers" :show-header="false">
  3. <el-table-column fixed="left" title="序号" type="seq" width="60" />
  4. <el-table-column align="left" label="社交平台" width="120">
  5. <template #default="{ row }">
  6. <img :src="row.img" alt="" class="h-5 align-middle" />
  7. <p class="mr-5">{{ row.title }}</p>
  8. </template>
  9. </el-table-column>
  10. <el-table-column align="center" label="操作">
  11. <template #default="{ row }">
  12. <template v-if="row.openid">
  13. 已绑定
  14. <XTextButton class="mr-5" title="(解绑)" type="primary" @click="unbind(row)" />
  15. </template>
  16. <template v-else>
  17. 未绑定
  18. <XTextButton class="mr-5" title="(绑定)" type="primary" @click="bind(row)" />
  19. </template>
  20. </template>
  21. </el-table-column>
  22. </el-table>
  23. </template>
  24. <script lang="ts" setup>
  25. import { SystemUserSocialTypeEnum } from '@/utils/constants'
  26. import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
  27. import { socialAuthRedirect, socialBind, socialUnbind } from '@/api/system/user/socialUser'
  28. defineOptions({ name: 'UserSocial' })
  29. const message = useMessage()
  30. const socialUsers = ref<any[]>([])
  31. const userInfo = ref<ProfileVO>()
  32. const initSocial = async () => {
  33. const res = await getUserProfile()
  34. userInfo.value = res
  35. for (const i in SystemUserSocialTypeEnum) {
  36. const socialUser = { ...SystemUserSocialTypeEnum[i] }
  37. socialUsers.value.push(socialUser)
  38. if (userInfo.value?.socialUsers) {
  39. for (const j in userInfo.value.socialUsers) {
  40. if (socialUser.type === userInfo.value.socialUsers[j].type) {
  41. socialUser.openid = userInfo.value.socialUsers[j].openid
  42. break
  43. }
  44. }
  45. }
  46. }
  47. }
  48. const route = useRoute()
  49. const bindSocial = () => {
  50. // 社交绑定
  51. const type = route.query.type
  52. const code = route.query.code
  53. const state = route.query.state
  54. if (!code) {
  55. return
  56. }
  57. socialBind(type, code, state).then(() => {
  58. message.success('绑定成功')
  59. initSocial()
  60. })
  61. }
  62. const bind = (row) => {
  63. const redirectUri = location.origin + '/user/profile?type=' + row.type
  64. // 进行跳转
  65. socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => {
  66. window.location.href = res
  67. })
  68. }
  69. const unbind = async (row) => {
  70. const res = await socialUnbind(row.type, row.openid)
  71. if (res) {
  72. row.openid = undefined
  73. }
  74. message.success('解绑成功')
  75. }
  76. onMounted(async () => {
  77. await initSocial()
  78. })
  79. watch(
  80. () => route,
  81. (newRoute) => {
  82. bindSocial()
  83. console.log(newRoute)
  84. },
  85. {
  86. immediate: true
  87. }
  88. )
  89. </script>