Skip to content

Commit 35433b4

Browse files
authored
feat: chat auth (#3285)
1 parent fbc37ed commit 35433b4

File tree

5 files changed

+132
-198
lines changed

5 files changed

+132
-198
lines changed

ui/src/api/chat/chat.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Result} from '@/request/Result'
1+
import { Result } from '@/request/Result'
22
import {
33
get,
44
post,
@@ -9,16 +9,16 @@ import {
99
download,
1010
exportFile,
1111
} from '@/request/chat/index'
12-
import {type ChatProfile} from '@/api/type/chat'
13-
import {type Ref} from 'vue'
12+
import { type ChatProfile } from '@/api/type/chat'
13+
import { type Ref } from 'vue'
1414

1515
import useStore from '@/stores'
16-
import type {LoginRequest} from "@/api/type/user.ts";
16+
import type { LoginRequest } from '@/api/type/user'
1717

18-
const prefix: any = {_value: '/workspace/'}
18+
const prefix: any = { _value: '/workspace/' }
1919
Object.defineProperty(prefix, 'value', {
2020
get: function () {
21-
const {user} = useStore()
21+
const { user } = useStore()
2222
return this._value + user.getWorkspaceId() + '/application'
2323
},
2424
})
@@ -31,7 +31,6 @@ Object.defineProperty(prefix, 'value', {
3131
*/
3232
const open: (loading?: Ref<boolean>) => Promise<Result<string>> = (loading) => {
3333
return get('/open', {}, loading)
34-
3534
}
3635
/**
3736
* 对话
@@ -46,7 +45,7 @@ const chatProfile: (assessToken: string, loading?: Ref<boolean>) => Promise<Resu
4645
assessToken,
4746
loading,
4847
) => {
49-
return get('/profile', {access_token: assessToken}, loading)
48+
return get('/profile', { access_token: assessToken }, loading)
5049
}
5150
/**
5251
* 匿名认证
@@ -58,7 +57,7 @@ const anonymousAuthentication: (
5857
assessToken: string,
5958
loading?: Ref<boolean>,
6059
) => Promise<Result<any>> = (assessToken, loading) => {
61-
return post('/auth/anonymous', {access_token: assessToken}, {}, loading)
60+
return post('/auth/anonymous', { access_token: assessToken }, {}, loading)
6261
}
6362
/**
6463
* 获取应用相关信息
@@ -69,30 +68,28 @@ const applicationProfile: (loading?: Ref<boolean>) => Promise<Result<any>> = (lo
6968
return get('/application/profile', {}, loading)
7069
}
7170

72-
7371
/**
7472
* 登录
7573
* @param request 登录接口请求表单
7674
* @param loading 接口加载器
7775
* @returns 认证数据
7876
*/
79-
const login: (accessToken: string, request: LoginRequest, loading?: Ref<boolean>) => Promise<Result<any>> = (
77+
const login: (
8078
accessToken: string,
81-
request,
82-
loading,
83-
) => {
79+
request: LoginRequest,
80+
loading?: Ref<boolean>,
81+
) => Promise<Result<any>> = (accessToken: string, request, loading) => {
8482
return post('/auth/login/' + accessToken, request, undefined, loading)
8583
}
8684

87-
const ldapLogin: (accessToken: string, request: LoginRequest, loading?: Ref<boolean>) => Promise<Result<any>> = (
85+
const ldapLogin: (
8886
accessToken: string,
89-
request,
90-
loading,
91-
) => {
87+
request: LoginRequest,
88+
loading?: Ref<boolean>,
89+
) => Promise<Result<any>> = (accessToken: string, request, loading) => {
9290
return post('/auth/ldap/login/' + accessToken, request, undefined, loading)
9391
}
9492

95-
9693
/**
9794
* 获取验证码
9895
* @param loading 接口加载器
@@ -114,35 +111,38 @@ const getQrSource: (loading?: Ref<boolean>) => Promise<Result<any>> = (loading)
114111

115112
const getDingCallback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
116113
code,
117-
loading
114+
loading,
118115
) => {
119-
return get('dingtalk', {code}, loading)
116+
return get('dingtalk', { code }, loading)
120117
}
121118

122119
const getDingOauth2Callback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
123120
code,
124-
loading
121+
loading,
125122
) => {
126-
return get('dingtalk/oauth2', {code}, loading)
123+
return get('dingtalk/oauth2', { code }, loading)
127124
}
128125

129126
const getWecomCallback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
130127
code,
131-
loading
128+
loading,
132129
) => {
133-
return get('wecom', {code}, loading)
130+
return get('wecom', { code }, loading)
134131
}
135132
const getLarkCallback: (code: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
136133
code,
137-
loading
134+
loading,
138135
) => {
139-
return get('lark/oauth2', {code}, loading)
136+
return get('lark/oauth2', { code }, loading)
140137
}
141138

142139
/**
143140
* 获取认证设置
144141
*/
145-
const getAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (auth_type, loading) => {
142+
const getAuthSetting: (auth_type: string, loading?: Ref<boolean>) => Promise<Result<any>> = (
143+
auth_type,
144+
loading,
145+
) => {
146146
return get(`/chat_user/${auth_type}/detail`, undefined, loading)
147147
}
148148
export default {
@@ -160,5 +160,5 @@ export default {
160160
getLarkCallback,
161161
getQrSource,
162162
ldapLogin,
163-
getAuthSetting
163+
getAuthSetting,
164164
}

ui/src/request/chat/index.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@ instance.interceptors.response.use(
5353
MsgError(err.message)
5454
console.error(err)
5555
}
56-
if (err.response?.status === 401) {
57-
const { chatUser } = useStore()
58-
if (chatUser.accessToken) {
59-
router.push({ name: 'login', params: { accessToken: chatUser.accessToken } })
60-
} else {
61-
router.push('/404 ')
62-
}
63-
}
64-
if (err.response?.status === 403 && !err.response.config.url.includes('chat/open')) {
65-
MsgError(
66-
err.response.data && err.response.data.message ? err.response.data.message : '没有权限访问',
67-
)
68-
}
6956
return Promise.reject(err)
7057
},
7158
)

ui/src/router/chat/index.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ router.beforeEach(
3333
})
3434
return
3535
}
36-
const authentication = await chatUser.isAuthentication()
36+
let authentication = false
37+
try {
38+
authentication = await chatUser.isAuthentication()
39+
} catch (e: any) {
40+
next({
41+
path: '/404',
42+
})
43+
return
44+
}
3745
const token = chatUser.getToken()
3846
if (authentication) {
3947
if (!token && to.name != 'login') {
@@ -45,22 +53,46 @@ router.beforeEach(
4553
})
4654
return
4755
} else {
48-
next()
49-
return
56+
if (to.name == 'login') {
57+
next()
58+
return
59+
} else {
60+
try {
61+
await chatUser.applicationProfile()
62+
} catch (e: any) {
63+
if (e.response?.status === 401) {
64+
next({
65+
name: 'login',
66+
params: {
67+
accessToken: to.params.accessToken,
68+
},
69+
})
70+
}
71+
return
72+
}
73+
next()
74+
return
75+
}
5076
}
5177
} else {
5278
await chatUser.anonymousAuthentication()
5379
}
5480
if (!chatUser.application) {
55-
await chatUser.applicationProfile()
56-
}
57-
// 判断是否有菜单权限
58-
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
59-
next()
60-
} else {
61-
// 如果没有权限则直接取404页面
62-
next('404')
81+
try {
82+
await chatUser.applicationProfile()
83+
} catch (e: any) {
84+
if (e.response?.status === 401) {
85+
next({
86+
name: 'login',
87+
params: {
88+
accessToken: to.params.accessToken,
89+
},
90+
})
91+
}
92+
return
93+
}
6394
}
95+
next()
6496
},
6597
)
6698
router.afterEach(() => {

ui/src/stores/modules/chat-user.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { defineStore } from 'pinia'
22
import ChatAPI from '@/api/chat/chat'
33
import { type ChatProfile } from '@/api/type/chat'
4-
4+
import type { LoginRequest } from '@/api/type/user'
5+
import type { Ref } from 'vue'
56
interface ChatUser {
67
// 用户id
78
id: string
@@ -59,12 +60,29 @@ const useChatUserStore = defineStore('chat-user', {
5960
}
6061
return localStorage.getItem(`accessToken`)
6162
},
63+
setToken(token: string) {
64+
this.token = token
65+
sessionStorage.setItem(`${this.accessToken}-accessToken`, token)
66+
localStorage.setItem(`${this.accessToken}-accessToken`, token)
67+
},
6268
/**
6369
*匿名认证
6470
*/
6571
anonymousAuthentication() {
6672
return ChatAPI.anonymousAuthentication(this.accessToken as string).then((ok) => {
67-
this.token = ok.data
73+
this.setToken(ok.data)
74+
return this.token
75+
})
76+
},
77+
login(request: LoginRequest, loading?: Ref<boolean>) {
78+
return ChatAPI.login(this.accessToken as string, request, loading).then((ok) => {
79+
this.setToken(ok.data.token)
80+
return this.token
81+
})
82+
},
83+
ldapLogin(request: LoginRequest, loading?: Ref<boolean>) {
84+
return ChatAPI.ldapLogin(this.accessToken as string, request, loading).then((ok) => {
85+
this.setToken(ok.data.token)
6886
return this.token
6987
})
7088
},

0 commit comments

Comments
 (0)