Skip to content

Commit 00dcea2

Browse files
committed
完成 SSO 登录的功能
1 parent 2138612 commit 00dcea2

File tree

1 file changed

+49
-33
lines changed

1 file changed

+49
-33
lines changed

src/views/sso.vue

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<!-- 表单 -->
1616
<div class="form-cont">
1717
<el-tabs class="form" style=" float:none;" value="uname">
18-
<el-tab-pane label="三方授权" name="uname">
18+
<el-tab-pane :label="'三方授权(' + client.name + ')'" name="uname">
1919
</el-tab-pane>
2020
</el-tabs>
2121
<div>
@@ -25,35 +25,23 @@
2525
<svg-icon slot="prefix" icon-class="tree" class="el-input__icon input-icon"/>
2626
</el-input>
2727
</el-form-item>
28-
<!-- 账号密码登录 -->
29-
<div v-if="loginForm.loginType === 'uname'">
30-
<el-form-item prop="username">
31-
<el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
32-
<svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
33-
</el-input>
34-
</el-form-item>
35-
<el-form-item prop="password">
36-
<el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
37-
@keyup.enter.native="handleLogin">
38-
<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
39-
</el-input>
40-
</el-form-item>
41-
<el-form-item prop="code" v-if="captchaEnable">
42-
<el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
43-
@keyup.enter.native="handleLogin">
44-
<svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/>
45-
</el-input>
46-
</el-form-item>
47-
</div>
48-
28+
<!-- 授权范围的选择 -->
29+
此第三方应用请求获得以下权限:
30+
<el-form-item prop="scopes">
31+
<el-checkbox-group v-model="loginForm.scopes">
32+
<el-checkbox v-for="scope in params.scopes" :label="scope" :key="scope"
33+
style="display: block; margin-bottom: -10px;">{{formatScope(scope)}}</el-checkbox>
34+
</el-checkbox-group>
35+
</el-form-item>
4936
<!-- 下方的登录按钮 -->
5037
<el-form-item style="width:100%;">
5138
<el-button :loading="loading" size="medium" type="primary" style="width:60%;"
52-
@click.native.prevent="handleLogin">
53-
<span v-if="!loading">同意授权</span>
54-
<span v-else>登 录 中...</span>
39+
@click.native.prevent="handleAuthorize(true)">
40+
<span v-if="!loading">统一授权</span>
41+
<span v-else>授 权 中...</span>
5542
</el-button>
56-
<el-button size="medium" style="width:36%">拒绝</el-button>
43+
<el-button size="medium" style="width:36%"
44+
@click.native.prevent="handleAuthorize(false)">拒绝</el-button>
5745
</el-form-item>
5846
</el-form>
5947
</div>
@@ -80,6 +68,7 @@ export default {
8068
tenantEnable: true,
8169
loginForm: {
8270
tenantName: "芋道源码",
71+
scopes: [], // 已选中的 scope 数组
8372
},
8473
params: { // URL 上的 client_id、scope 等参数
8574
responseType: undefined,
@@ -92,7 +81,6 @@ export default {
9281
name: '',
9382
logo: '',
9483
},
95-
checkedScopes: [], // 已选中的 scope 数组
9684
LoginRules: {
9785
tenantName: [
9886
{required: true, trigger: "blur", message: "租户不能为空"},
@@ -114,8 +102,7 @@ export default {
114102
}
115103
]
116104
},
117-
loading: false,
118-
//
105+
loading: false
119106
};
120107
},
121108
created() {
@@ -169,7 +156,7 @@ export default {
169156
// 生成已选中的 checkedScopes
170157
for (const scope of scopes) {
171158
if (scope.value) {
172-
this.checkedScopes.push(scope.key)
159+
this.loginForm.scopes.push(scope.key)
173160
}
174161
}
175162
})
@@ -178,21 +165,50 @@ export default {
178165
getCookie() {
179166
const tenantName = getTenantName();
180167
this.loginForm = {
168+
...this.loginForm,
181169
tenantName: tenantName ? tenantName : this.loginForm.tenantName,
182170
};
183171
},
184-
handleLogin() {
172+
handleAuthorize(approved) {
185173
this.$refs.loginForm.validate(valid => {
186174
if (!valid) {
187175
return
188176
}
189-
190-
177+
this.loading = true
178+
// 计算 checkedScopes + uncheckedScopes
179+
let checkedScopes;
180+
let uncheckedScopes;
181+
if (approved) { // 同意授权,按照用户的选择
182+
checkedScopes = this.loginForm.scopes
183+
uncheckedScopes = this.params.scopes.filter(item => checkedScopes.indexOf(item) === -1)
184+
} else { // 拒绝,则都是取消
185+
checkedScopes = []
186+
uncheckedScopes = this.params.scopes
187+
}
188+
// 提交授权的请求
189+
this.doAuthorize(false, checkedScopes, uncheckedScopes).then(res => {
190+
const href = res.data
191+
if (!href) {
192+
return;
193+
}
194+
location.href = href
195+
}).finally(() => {
196+
this.loading = false
197+
})
191198
})
192199
},
193200
doAuthorize(autoApprove, checkedScopes, uncheckedScopes) {
194201
return authorize(this.params.responseType, this.params.clientId, this.params.redirectUri, this.params.state,
195202
autoApprove, checkedScopes, uncheckedScopes)
203+
},
204+
formatScope(scope) {
205+
// 格式化 scope 授权范围,方便用户理解。
206+
// 这里仅仅是一个 demo,可以考虑录入到字典数据中,例如说字典类型 "system_oauth2_scope",它的每个 scope 都是一条字典数据。
207+
switch (scope) {
208+
case 'user.read': return '访问你的个人信息'
209+
case 'user.write': return '修改你的个人信息'
210+
default: return scope
211+
}
196212
}
197213
}
198214
};

0 commit comments

Comments
 (0)