SSOLogin.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <!-- 表单 -->
  3. <div v-show="getShow" class="form-cont">
  4. <!-- <LoginFormTitle style="width: 100%" />-->
  5. <el-tabs class="form" style="float: none" value="uname">
  6. <el-tab-pane :label="'三方授权(' + client.name + ')'" name="uname" />
  7. </el-tabs>
  8. <div>
  9. <el-form ref="ssoForm" :model="loginForm" class="login-form">
  10. <!-- 授权范围的选择 -->
  11. 此第三方应用请求获得以下权限:
  12. <el-form-item prop="scopes">
  13. <el-checkbox-group v-model="loginForm.scopes">
  14. <el-checkbox
  15. v-for="scope in params.scopes"
  16. :key="scope"
  17. :label="scope"
  18. style="display: block; margin-bottom: -10px"
  19. >{{ formatScope(scope) }}
  20. </el-checkbox>
  21. </el-checkbox-group>
  22. </el-form-item>
  23. <!-- 下方的登录按钮 -->
  24. <el-form-item style="width: 100%">
  25. <el-button
  26. :loading="loading"
  27. size="small"
  28. style="width: 60%"
  29. type="primary"
  30. @click.prevent="handleAuthorize(true)"
  31. >
  32. <span v-if="!loading">同意授权</span>
  33. <span v-else>授 权 中...</span>
  34. </el-button>
  35. <el-button size="small" style="width: 36%" @click.prevent="handleAuthorize(false)"
  36. >拒绝
  37. </el-button>
  38. </el-form-item>
  39. </el-form>
  40. </div>
  41. </div>
  42. </template>
  43. <script lang="ts" name="SSOLogin" setup>
  44. // import LoginFormTitle from './LoginFormTitle.vue' // TODO 艿艿你看看要不要这个表头
  45. import { authorize, getAuthorize, paramsType, scopesType } from '@/api/login'
  46. import { LoginStateEnum, useLoginState } from './useLogin'
  47. import type { RouteLocationNormalizedLoaded } from 'vue-router'
  48. const { t } = useI18n()
  49. const ssoForm = ref() // 表单Ref
  50. const { getLoginState, setLoginState } = useLoginState()
  51. const getShow = computed(() => unref(getLoginState) === LoginStateEnum.SSO)
  52. const loginForm = reactive<{ scopes: scopesType }>({
  53. scopes: [] // 已选中的 scope 数组
  54. })
  55. const params = reactive<paramsType>({
  56. // URL 上的 client_id、scope 等参数
  57. responseType: '',
  58. clientId: '',
  59. redirectUri: '',
  60. state: '',
  61. scopes: [] // 优先从 query 参数获取;如果未传递,从后端获取
  62. }) // 表单Ref
  63. const client = ref({
  64. // 客户端信息
  65. name: '',
  66. logo: ''
  67. })
  68. const loading = ref(false)
  69. const handleAuthorize = (approved) => {
  70. ssoForm.value.validate((valid) => {
  71. if (!valid) {
  72. return
  73. }
  74. loading.value = true
  75. // 计算 checkedScopes + uncheckedScopes
  76. let checkedScopes
  77. let uncheckedScopes
  78. if (approved) {
  79. // 同意授权,按照用户的选择
  80. checkedScopes = loginForm.scopes
  81. uncheckedScopes = params.scopes.filter((item) => checkedScopes.indexOf(item) === -1)
  82. } else {
  83. // 拒绝,则都是取消
  84. checkedScopes = []
  85. uncheckedScopes = params.scopes
  86. }
  87. // 提交授权的请求
  88. doAuthorize(false, checkedScopes, uncheckedScopes)
  89. .then((res) => {
  90. const href = res.data
  91. if (!href) {
  92. return
  93. }
  94. location.href = href
  95. })
  96. .finally(() => {
  97. loading.value = false
  98. })
  99. })
  100. }
  101. const doAuthorize = (autoApprove, checkedScopes, uncheckedScopes) => {
  102. return authorize(
  103. params.responseType,
  104. params.clientId,
  105. params.redirectUri,
  106. params.state,
  107. autoApprove,
  108. checkedScopes,
  109. uncheckedScopes
  110. )
  111. }
  112. const formatScope = (scope) => {
  113. // 格式化 scope 授权范围,方便用户理解。
  114. // 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
  115. // TODO 这个之做了中文部分
  116. return t(`login.sso.${scope}`)
  117. }
  118. const route = useRoute()
  119. const init = () => {
  120. // 防止在没有登录的情况下循环弹窗
  121. if (typeof route.query.client_id === 'undefined') return
  122. // 解析参数
  123. // 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
  124. // 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
  125. params.responseType = route.query.response_type as string
  126. params.clientId = route.query.client_id as string
  127. params.redirectUri = route.query.redirect_uri as string
  128. params.state = route.query.state as string
  129. if (route.query.scope) {
  130. params.scopes = (route.query.scope as string).split(' ')
  131. }
  132. // 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
  133. if (params.scopes.length > 0) {
  134. doAuthorize(true, params.scopes, []).then((res) => {
  135. if (!res) {
  136. console.log('自动授权未通过!')
  137. return
  138. }
  139. location.href = res.data
  140. })
  141. }
  142. // 获取授权页的基本信息
  143. getAuthorize(params.clientId).then((res) => {
  144. client.value = res.client
  145. // 解析 scope
  146. let scopes
  147. // 1.1 如果 params.scope 非空,则过滤下返回的 scopes
  148. if (params.scopes.length > 0) {
  149. scopes = []
  150. for (const scope of res.scopes) {
  151. if (params.scopes.indexOf(scope.key) >= 0) {
  152. scopes.push(scope)
  153. }
  154. }
  155. // 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
  156. } else {
  157. scopes = res.scopes
  158. for (const scope of scopes) {
  159. params.scopes.push(scope.key)
  160. }
  161. }
  162. // 生成已选中的 checkedScopes
  163. for (const scope of scopes) {
  164. if (scope.value) {
  165. loginForm.scopes.push(scope.key)
  166. }
  167. }
  168. })
  169. }
  170. // =======SSO======
  171. const { currentRoute } = useRouter()
  172. // 监听当前路由
  173. watch(
  174. () => currentRoute.value,
  175. (route: RouteLocationNormalizedLoaded) => {
  176. if (route.name === 'SSOLogin') {
  177. setLoginState(LoginStateEnum.SSO)
  178. init()
  179. }
  180. },
  181. { immediate: true }
  182. )
  183. init()
  184. </script>