SSOLogin.vue 5.8 KB

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