-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathchat-user.ts
More file actions
154 lines (149 loc) · 4.49 KB
/
chat-user.ts
File metadata and controls
154 lines (149 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import {defineStore} from 'pinia'
import ChatAPI from '@/api/chat/chat'
import type {ChatProfile, ChatUserProfile} from '@/api/type/chat'
import type {LoginRequest} from '@/api/type/user'
import type {Ref} from 'vue'
import {getBrowserLang} from '@/locales/index'
interface ChatUser {
// 用户id
id: string
}
interface Chat {
chat_profile?: ChatProfile
application?: any
chatUserProfile?: ChatUserProfile
token?: string
accessToken?: string
}
const useChatUserStore = defineStore('chat-user', {
state: (): Chat => ({
chat_profile: undefined,
application: undefined,
accessToken: undefined,
}),
actions: {
getLanguage() {
return localStorage.getItem(`${this.accessToken}-locale`) || getBrowserLang()
},
setAccessToken(accessToken: string) {
this.accessToken = accessToken
},
getChatProfile() {
return ChatAPI.chatProfile(this.accessToken as string).then((ok) => {
this.chat_profile = ok.data
return this.chat_profile
})
},
async getChatUserProfile() {
const res = await ChatAPI.getChatUserProfile()
this.chatUserProfile = res.data
return res.data
},
applicationProfile() {
return ChatAPI.applicationProfile().then((ok) => {
this.application = ok.data
localStorage.setItem(`${this.accessToken}-locale`, ok.data?.language || this.getLanguage())
})
},
isAuthentication() {
if (this.chat_profile) {
return Promise.resolve(this.chat_profile.authentication)
} else {
return this.getChatProfile().then((ok) => {
return ok.authentication
})
}
},
getToken() {
if (this.token) {
return this.token
}
const token = sessionStorage.getItem(`${this.accessToken}-accessToken`)
if (token) {
this.token = token
return token
}
const local_token = localStorage.getItem(`${this.accessToken}-accessToken`)
if (local_token) {
this.token = local_token
return local_token
}
return localStorage.getItem(`accessToken`)
},
setToken(token: string) {
this.token = token
sessionStorage.setItem(`${this.accessToken}-accessToken`, token)
localStorage.setItem(`${this.accessToken}-accessToken`, token)
},
/**
*匿名认证
*/
anonymousAuthentication() {
return ChatAPI.anonymousAuthentication(this.accessToken as string).then((ok) => {
this.setToken(ok.data)
return this.token
})
},
passwordAuthentication(password: string) {
return ChatAPI.passwordAuthentication(this.accessToken as string, password).then((ok) => {
this.setToken(ok.data)
return this.token
})
},
login(request: any, loading?: Ref<boolean>) {
return ChatAPI.login(this.accessToken as string, request, loading).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
ldapLogin(request: LoginRequest, loading?: Ref<boolean>) {
return ChatAPI.ldapLogin(this.accessToken as string, request, loading).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
logout() {
return ChatAPI.logout().then(() => {
sessionStorage.removeItem(`${this.accessToken}-accessToken`)
localStorage.removeItem(`${this.accessToken}-accessToken`)
this.token = undefined
return true
})
},
async dingCallback(code: string, accessToken: string) {
return ChatAPI.getDingCallback(code, accessToken).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
async dingOauth2Callback(code: string, accessToken: string) {
return ChatAPI.getDingOauth2Callback(code, accessToken).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
async wecomCallback(code: string, accessToken: string) {
return ChatAPI.getWecomCallback(code, accessToken).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
async larkCallback(code: string, accessToken: string) {
return ChatAPI.getLarkCallback(code, accessToken).then((ok) => {
this.setToken(ok.data.token)
return this.token
})
},
async getQrType() {
return ChatAPI.getQrType().then((ok) => {
return ok.data
})
},
async getQrSource() {
return ChatAPI.getQrSource().then((ok) => {
return ok.data
})
},
},
})
export default useChatUserStore