14
14
15
15
<!-- 表单 -->
16
16
<div class =" form-cont" >
17
- <el-tabs class =" form" style =" float :none ; " >
17
+ <el-tabs class =" form" style =" float :none ; " value = " uname " >
18
18
<el-tab-pane label =" 三方授权" name =" uname" >
19
19
</el-tab-pane >
20
20
</el-tabs >
53
53
<span v-if =" !loading" >同意授权</span >
54
54
<span v-else >登 录 中...</span >
55
55
</el-button >
56
- <el-button size =" medium" style =" width :37 % " >拒绝</el-button >
56
+ <el-button size =" medium" style =" width :36 % " >拒绝</el-button >
57
57
</el-form-item >
58
58
</el-form >
59
59
</div >
69
69
70
70
<script >
71
71
import {getTenantIdByName } from " @/api/system/tenant" ;
72
- import Cookies from " js-cookie" ;
73
- import {SystemUserSocialTypeEnum } from " @/utils/constants" ;
74
72
import {getTenantEnable } from " @/utils/ruoyi" ;
75
- import {authorize } from " @/api/login" ;
73
+ import {authorize , getAuthorize } from " @/api/login" ;
74
+ import {getTenantName , setTenantId } from " @/utils/auth" ;
76
75
77
76
export default {
78
77
name: " Login" ,
@@ -82,6 +81,18 @@ export default {
82
81
loginForm: {
83
82
tenantName: " 芋道源码" ,
84
83
},
84
+ params: { // URL 上的 client_id、scope 等参数
85
+ responseType: undefined ,
86
+ clientId: undefined ,
87
+ redirectUri: undefined ,
88
+ state: undefined ,
89
+ scopes: [], // 优先从 query 参数获取;如果未传递,从后端获取
90
+ },
91
+ client: { // 客户端信息
92
+ name: ' ' ,
93
+ logo: ' ' ,
94
+ },
95
+ checkedScopes: [], // 已选中的 scope 数组
85
96
LoginRules: {
86
97
tenantName: [
87
98
{required: true , trigger: " blur" , message: " 租户不能为空" },
@@ -92,7 +103,7 @@ export default {
92
103
const tenantId = res .data ;
93
104
if (tenantId && tenantId >= 0 ) {
94
105
// 设置租户
95
- Cookies . set ( " tenantId" , tenantId);
106
+ setTenantId ( tenantId)
96
107
callback ();
97
108
} else {
98
109
callback (' 租户不存在' );
@@ -104,44 +115,84 @@ export default {
104
115
]
105
116
},
106
117
loading: false ,
107
- redirect: undefined ,
108
- // 枚举
109
- SysUserSocialTypeEnum: SystemUserSocialTypeEnum,
118
+ //
110
119
};
111
120
},
112
121
created () {
113
122
// 租户开关
114
123
this .tenantEnable = getTenantEnable ();
115
- // 重定向地址
116
- this .redirect = this .$route .query .redirect ;
117
124
this .getCookie ();
125
+
126
+ // 解析参数
127
+ // 例如说【自动授权不通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read%20user.write
128
+ // 例如说【自动授权通过】:client_id=default&redirect_uri=https%3A%2F%2Fwww.iocoder.cn&response_type=code&scope=user.read
129
+ this .params .responseType = this .$route .query .response_type
130
+ this .params .clientId = this .$route .query .client_id
131
+ this .params .redirectUri = this .$route .query .redirect_uri
132
+ this .params .state = this .$route .query .state
133
+ if (this .$route .query .scope ) {
134
+ this .params .scopes = this .$route .query .scope .split (' ' )
135
+ }
136
+
137
+ // 如果有 scope 参数,先执行一次自动授权,看看是否之前都授权过了。
138
+ if (this .params .scopes .length > 0 ) {
139
+ this .doAuthorize (true , this .params .scopes , []).then (res => {
140
+ const href = res .data
141
+ if (! href) {
142
+ console .log (' 自动授权未通过!' )
143
+ return ;
144
+ }
145
+ location .href = href
146
+ })
147
+ }
148
+
149
+ // 获取授权页的基本信息
150
+ getAuthorize (this .params .clientId ).then (res => {
151
+ this .client = res .data .client
152
+ // 解析 scope
153
+ let scopes
154
+ // 1.1 如果 params.scope 非空,则过滤下返回的 scopes
155
+ if (this .params .scopes .length > 0 ) {
156
+ scopes = []
157
+ for (const scope of res .data .scopes ) {
158
+ if (this .params .scopes .indexOf (scope .key ) >= 0 ) {
159
+ scopes .push (scope)
160
+ }
161
+ }
162
+ // 1.2 如果 params.scope 为空,则使用返回的 scopes 设置它
163
+ } else {
164
+ scopes = res .data .scopes
165
+ for (const scope of scopes) {
166
+ this .params .scopes .push (scope .key )
167
+ }
168
+ }
169
+ // 生成已选中的 checkedScopes
170
+ for (const scope of scopes) {
171
+ if (scope .value ) {
172
+ this .checkedScopes .push (scope .key )
173
+ }
174
+ }
175
+ })
118
176
},
119
177
methods: {
120
178
getCookie () {
121
- const tenantName = Cookies . get ( ' tenantName ' );
179
+ const tenantName = getTenantName ( );
122
180
this .loginForm = {
123
- tenantName: tenantName === undefined ? this .loginForm .tenantName : tenantName
181
+ tenantName: tenantName ? tenantName : this .loginForm .tenantName ,
124
182
};
125
183
},
126
184
handleLogin () {
127
- if (true ) {
128
- authorize ()
129
- return ;
130
- }
131
185
this .$refs .loginForm .validate (valid => {
132
- if (valid) {
133
- this .loading = true ;
134
- // 发起登陆
135
- console .log (" 发起登录" , this .loginForm );
136
- this .$store .dispatch (this .loginForm .loginType === " sms" ? " SmsLogin" : " Login" , this .loginForm ).then (() => {
137
- this .$router .push ({path: this .redirect || " /" }).catch (() => {
138
- });
139
- }).catch (() => {
140
- this .loading = false ;
141
- this .getCode ();
142
- });
186
+ if (! valid) {
187
+ return
143
188
}
144
- });
189
+
190
+
191
+ })
192
+ },
193
+ doAuthorize (autoApprove , checkedScopes , uncheckedScopes ) {
194
+ return authorize (this .params .responseType , this .params .clientId , this .params .redirectUri , this .params .state ,
195
+ autoApprove, checkedScopes, uncheckedScopes)
145
196
}
146
197
}
147
198
};
0 commit comments