SSOLogin.vue 5.3 KB

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