UserSocial.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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" name="UserSocial" 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. const message = useMessage()
  29. const socialUsers = ref<any[]>([])
  30. const userInfo = ref<ProfileVO>()
  31. const initSocial = async () => {
  32. const res = await getUserProfile()
  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 route = useRoute()
  48. const bindSocial = () => {
  49. // 社交绑定
  50. const type = route.query.type
  51. const code = route.query.code
  52. const state = route.query.state
  53. if (!code) {
  54. return
  55. }
  56. socialBind(type, code, state).then(() => {
  57. message.success('绑定成功')
  58. initSocial()
  59. })
  60. }
  61. const bind = (row) => {
  62. const redirectUri = location.origin + '/user/profile?type=' + row.type
  63. // 进行跳转
  64. socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => {
  65. window.location.href = res
  66. })
  67. }
  68. const unbind = async (row) => {
  69. const res = await socialUnbind(row.type, row.openid)
  70. if (res) {
  71. row.openid = undefined
  72. }
  73. message.success('解绑成功')
  74. }
  75. onMounted(async () => {
  76. await initSocial()
  77. })
  78. watch(
  79. () => route,
  80. (newRoute) => {
  81. bindSocial()
  82. console.log(newRoute)
  83. },
  84. {
  85. immediate: true
  86. }
  87. )
  88. </script>