social-bindings.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div class="social-bindings">
  3. <div class="section-title">第三方账号</div>
  4. <div class="description">绑定 QQ 和微信后,无论使用哪种方式登录,都会进入当前 RunForge 账户。</div>
  5. <div class="account-list">
  6. <div v-for="item in accounts" :key="item.type" class="account-card">
  7. <div class="account-info">
  8. <a-avatar :size="44" v-if="getBinding(item.type)?.avatar" :imageUrl="getBinding(item.type)?.avatar"></a-avatar>
  9. <a-avatar :size="44" :style="{ backgroundColor: '#FE82A5' }" v-else>
  10. <icon-qq v-if="item.type === 'qq'" />
  11. <icon-wechat v-else />
  12. </a-avatar>
  13. <div>
  14. <div class="account-name">{{ item.name }}</div>
  15. <a-tag :color="isBound(item.type) ? 'green' : 'gray'" size="small">
  16. {{ isBound(item.type) ? '已绑定' : '未绑定' }}
  17. </a-tag>
  18. <div v-if="isBound(item.type)" class="account-nickname">
  19. 昵称:{{ getBinding(item.type)?.nickname || '未获取' }}
  20. </div>
  21. </div>
  22. </div>
  23. <a-button
  24. v-if="!isBound(item.type)"
  25. type="primary"
  26. :loading="state.loadingType === item.type"
  27. @click="bind(item.type)"
  28. >
  29. 立即绑定
  30. </a-button>
  31. <a-button
  32. v-else
  33. status="danger"
  34. :loading="state.loadingType === item.type"
  35. @click="unbind(item.type)"
  36. >
  37. 解绑
  38. </a-button>
  39. </div>
  40. </div>
  41. </div>
  42. </template>
  43. <script setup>
  44. import { onBeforeMount, reactive, ref } from 'vue'
  45. import { getLoginUrl } from '@/api/login'
  46. import { useUserStore } from '@/store/modules/user'
  47. import { isElectron } from '@/utils/electron'
  48. import { Message, Modal, Notification } from '@arco-design/web-vue'
  49. const userStore = useUserStore()
  50. const user = ref({})
  51. const state = reactive({
  52. loadingType: ''
  53. })
  54. const accounts = [
  55. { type: 'qq', name: 'QQ' },
  56. { type: 'wx', name: '微信' }
  57. ]
  58. const refreshUser = async () => {
  59. user.value = await userStore.getInfoFromServer()
  60. }
  61. const isBound = (type) => {
  62. return user.value?.boundTypes?.includes(type) ||
  63. user.value?.socialBindings?.some(item => item.type === type && item.bound)
  64. }
  65. const getBinding = (type) => {
  66. return user.value?.socialBindings?.find(item => item.type === type && item.bound) || null
  67. }
  68. const bind = async (type) => {
  69. try {
  70. state.loadingType = type
  71. const res = await getLoginUrl({
  72. type,
  73. mode: 'uniLogin',
  74. action: 'bind',
  75. from: '/user/setting'
  76. })
  77. if (!res || res.code !== 0)
  78. throw new Error(res?.msg ?? '获取绑定链接失败!')
  79. if (isElectron())
  80. window.electron.openNewWindow(res.data)
  81. else
  82. window.location.href = res.data
  83. } catch (error) {
  84. Notification.error({
  85. title: '获取绑定链接失败',
  86. content: error.message || '请稍后再试'
  87. })
  88. } finally {
  89. state.loadingType = ''
  90. }
  91. }
  92. const unbind = (type) => {
  93. const accountName = type === 'qq' ? 'QQ' : '微信'
  94. Modal.confirm({
  95. title: `解绑${accountName}`,
  96. content: `解绑后将无法继续通过该${accountName}账号登录当前账户,是否继续?`,
  97. onOk: async () => {
  98. try {
  99. state.loadingType = type
  100. await userStore.unbindSocial(type)
  101. await refreshUser()
  102. Message.success(`${accountName}解绑成功`)
  103. } catch (error) {
  104. Notification.error({
  105. title: `${accountName}解绑失败`,
  106. content: error.message || '请稍后再试'
  107. })
  108. } finally {
  109. state.loadingType = ''
  110. }
  111. }
  112. })
  113. }
  114. onBeforeMount(refreshUser)
  115. </script>
  116. <style scoped lang="less">
  117. .social-bindings {
  118. width: 560px;
  119. margin: 0 auto;
  120. }
  121. .description {
  122. margin-bottom: 18px;
  123. color: var(--color-text-3);
  124. }
  125. .account-list {
  126. display: flex;
  127. flex-direction: column;
  128. gap: 16px;
  129. }
  130. .account-card {
  131. display: flex;
  132. align-items: center;
  133. justify-content: space-between;
  134. padding: 18px;
  135. border: 1px solid var(--color-border-2);
  136. border-radius: 8px;
  137. }
  138. .account-info {
  139. display: flex;
  140. align-items: center;
  141. gap: 14px;
  142. }
  143. .account-name {
  144. margin-bottom: 6px;
  145. font-size: 16px;
  146. font-weight: 600;
  147. }
  148. .account-nickname {
  149. margin-top: 6px;
  150. color: var(--color-text-3);
  151. font-size: 12px;
  152. }
  153. </style>