2023-02-11 00:44:00 +08:00
|
|
|
<template>
|
|
|
|
<el-table :data="socialUsers" :show-header="false">
|
2023-04-14 21:47:58 +08:00
|
|
|
<el-table-column fixed="left" title="序号" type="seq" width="60" />
|
|
|
|
<el-table-column align="left" label="社交平台" width="120">
|
2023-02-11 00:44:00 +08:00
|
|
|
<template #default="{ row }">
|
2023-04-14 21:47:58 +08:00
|
|
|
<img :src="row.img" alt="" class="h-5 align-middle" />
|
2023-02-11 00:44:00 +08:00
|
|
|
<p class="mr-5">{{ row.title }}</p>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
2023-04-14 21:47:58 +08:00
|
|
|
<el-table-column align="center" label="操作">
|
2023-02-11 00:44:00 +08:00
|
|
|
<template #default="{ row }">
|
|
|
|
<template v-if="row.openid">
|
|
|
|
已绑定
|
2023-04-14 21:47:58 +08:00
|
|
|
<XTextButton class="mr-5" title="(解绑)" type="primary" @click="unbind(row)" />
|
2023-02-11 00:44:00 +08:00
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
未绑定
|
2023-04-14 21:47:58 +08:00
|
|
|
<XTextButton class="mr-5" title="(绑定)" type="primary" @click="bind(row)" />
|
2023-02-11 00:44:00 +08:00
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
</el-table>
|
|
|
|
</template>
|
2023-06-21 19:14:34 +08:00
|
|
|
<script lang="ts" setup>
|
2023-02-11 00:44:00 +08:00
|
|
|
import { SystemUserSocialTypeEnum } from '@/utils/constants'
|
2023-04-05 20:13:35 +08:00
|
|
|
import { getUserProfile, ProfileVO } from '@/api/system/user/profile'
|
2023-04-14 21:47:58 +08:00
|
|
|
import { socialAuthRedirect, socialBind, socialUnbind } from '@/api/system/user/socialUser'
|
2023-02-11 00:44:00 +08:00
|
|
|
|
2023-06-21 19:14:34 +08:00
|
|
|
defineOptions({ name: 'UserSocial' })
|
|
|
|
|
2023-02-11 00:44:00 +08:00
|
|
|
const message = useMessage()
|
|
|
|
const socialUsers = ref<any[]>([])
|
|
|
|
const userInfo = ref<ProfileVO>()
|
|
|
|
|
|
|
|
const initSocial = async () => {
|
2023-04-05 20:13:35 +08:00
|
|
|
const res = await getUserProfile()
|
2023-02-11 00:44:00 +08:00
|
|
|
userInfo.value = res
|
|
|
|
for (const i in SystemUserSocialTypeEnum) {
|
|
|
|
const socialUser = { ...SystemUserSocialTypeEnum[i] }
|
|
|
|
socialUsers.value.push(socialUser)
|
|
|
|
if (userInfo.value?.socialUsers) {
|
|
|
|
for (const j in userInfo.value.socialUsers) {
|
|
|
|
if (socialUser.type === userInfo.value.socialUsers[j].type) {
|
|
|
|
socialUser.openid = userInfo.value.socialUsers[j].openid
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 21:47:58 +08:00
|
|
|
const route = useRoute()
|
|
|
|
const bindSocial = () => {
|
|
|
|
// 社交绑定
|
|
|
|
const type = route.query.type
|
|
|
|
const code = route.query.code
|
|
|
|
const state = route.query.state
|
|
|
|
if (!code) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
socialBind(type, code, state).then(() => {
|
|
|
|
message.success('绑定成功')
|
|
|
|
initSocial()
|
|
|
|
})
|
|
|
|
}
|
2023-02-11 00:44:00 +08:00
|
|
|
const bind = (row) => {
|
|
|
|
const redirectUri = location.origin + '/user/profile?type=' + row.type
|
|
|
|
// 进行跳转
|
|
|
|
socialAuthRedirect(row.type, encodeURIComponent(redirectUri)).then((res) => {
|
2023-04-14 21:47:58 +08:00
|
|
|
window.location.href = res
|
2023-02-11 00:44:00 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const unbind = async (row) => {
|
|
|
|
const res = await socialUnbind(row.type, row.openid)
|
|
|
|
if (res) {
|
|
|
|
row.openid = undefined
|
|
|
|
}
|
|
|
|
message.success('解绑成功')
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
await initSocial()
|
|
|
|
})
|
2023-04-14 21:47:58 +08:00
|
|
|
|
|
|
|
watch(
|
|
|
|
() => route,
|
|
|
|
(newRoute) => {
|
|
|
|
bindSocial()
|
|
|
|
console.log(newRoute)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
immediate: true
|
|
|
|
}
|
|
|
|
)
|
2023-02-11 00:44:00 +08:00
|
|
|
</script>
|