Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
web-lib
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
郑磊
web-lib
Commits
f6954f52
Commit
f6954f52
authored
Aug 17, 2026
by
郑磊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加app授权支持
parent
b61df7da
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
157 additions
and
1 deletion
+157
-1
auth.ts
web/auth.ts
+114
-0
cookie.ts
web/cookie.ts
+42
-1
index.ts
web/index.ts
+1
-0
No files found.
web/auth.ts
0 → 100644
View file @
f6954f52
import
{
api
}
from
'./api'
import
{
setCookie
}
from
'./cookie'
/**
* 网页拉起APP授权时接口返回的数据
*/
interface
AuthLoginData
{
/**
* 登录标识
*/
qr_code
:
string
/**
* 登录过期时间
*/
expires_in
:
number
/**
* 授权地址
*/
auth_url
:
string
}
/**
* 拉起APP发起授权
* @param returnUrl 授权完成/取消时需要返回到的页面地址,如果不传则使用当前页面地址
* @param
*/
export
async
function
authLogin
(
returnUrl
?:
string
):
Promise
<
true
|
string
>
{
//获取授权地址
const
ret
=
await
api
<
AuthLoginData
>
({
url
:
'/api/Login/getAuthLoginUrl'
,
})
if
(
ret
.
code
!==
200
)
{
//获取授权信息失败
return
ret
.
msg
}
//构建返回时的地址
const
backUrl
=
new
URL
(
returnUrl
??
location
.
href
)
backUrl
.
searchParams
.
append
(
'code'
,
ret
.
data
.
qr_code
)
//构建跳转地址
const
authUrl
=
new
URL
(
ret
.
data
.
auth_url
)
authUrl
.
searchParams
.
append
(
'url'
,
backUrl
.
href
)
//打开跳转地址
const
iframe
=
document
.
createElement
(
'iframe'
)
iframe
.
style
.
display
=
'none'
document
.
body
.
appendChild
(
iframe
)
return
true
}
/**
* 检查APP授权登录的结果
*/
export
function
checkLogin
():
Promise
<
false
|
string
>
/**
* 检查APP授权登录的结果
* @param code 登录码
* @param state 授权状态`success`/`cancel`
*/
export
function
checkLogin
(
code
:
string
|
null
|
undefined
,
state
?:
string
|
null
|
undefined
,
):
Promise
<
false
|
string
>
export
async
function
checkLogin
(
code
?:
string
|
null
,
state
?:
string
|
null
,
):
Promise
<
false
|
string
>
{
const
url
=
new
URL
(
location
.
href
)
if
(
typeof
code
!==
'string'
)
{
code
=
url
.
searchParams
.
get
(
'code'
)
}
if
(
typeof
code
!==
'string'
||
!
code
)
{
//缺少授权参数
return
false
}
if
(
typeof
state
!==
'string'
)
{
state
=
url
.
searchParams
.
get
(
'state'
)
}
if
(
state
===
'cancel'
)
{
//用户主动拒绝了授权
return
false
}
const
ret
=
await
api
<
{
code_info
:
{
data
:
{
token
:
string
}
}
}
>
({
url
:
'/api/qr/findQrCodeInfo'
,
data
:
{
code
,
},
})
if
(
ret
.
code
!==
200
)
return
false
const
token
=
ret
.
data
?.
code_info
?.
data
?.
token
if
(
typeof
token
!==
'string'
||
!
token
)
return
false
return
token
}
/**
* 将token值设置到cookie中
* @param token
*/
export
function
setTokenCookie
(
token
:
string
):
Promise
<
void
>
{
return
setCookie
(
'token'
,
token
)
}
web/cookie.ts
View file @
f6954f52
...
...
@@ -3,7 +3,7 @@
*/
const
cookies
=
(()
=>
{
const
data
=
new
Map
<
string
,
string
>
()
const
parts
=
document
.
cookie
.
split
(
'
;
'
)
const
parts
=
document
.
cookie
.
split
(
'
,
'
)
for
(
let
i
=
0
;
i
<
parts
.
length
;
i
++
)
{
const
part
=
parts
[
i
]
let
[
name
]
=
part
.
split
(
'='
,
1
)
...
...
@@ -31,3 +31,44 @@ export function getCookie(name: string): string | undefined
export
function
getCookie
(
name
:
string
,
defaultValue
?:
string
)
{
return
cookies
.
has
(
name
)
?
cookies
.
get
(
name
)
!
:
defaultValue
}
/**
* 设置cookie
* @param name
* @param value
* @param options
*/
export
function
setCookie
(
name
:
string
,
value
:
string
):
Promise
<
void
>
export
function
setCookie
(
cookie
:
CookieInit
):
Promise
<
void
>
export
function
setCookie
(
arg1
:
string
|
CookieInit
,
value
?:
string
)
{
let
cookie
:
CookieInit
if
(
typeof
arg1
===
'string'
)
{
cookie
=
{
name
:
arg1
,
value
:
String
(
value
),
}
}
else
{
cookie
=
arg1
}
if
(
window
.
cookieStore
)
{
//使用标准方式写入
return
cookieStore
.
set
(
cookie
)
}
//使用自定义方式写入
const
params
:
string
[]
=
[
encodeURIComponent
(
cookie
.
name
)
+
'='
+
encodeURIComponent
(
cookie
.
value
),
]
if
(
cookie
.
expires
)
{
params
.
push
(
'expires='
+
new
Date
(
cookie
.
expires
).
toUTCString
())
}
if
(
cookie
.
domain
)
{
params
.
push
(
'domain='
+
cookie
.
domain
)
}
if
(
cookie
.
path
)
{
params
.
push
(
'path='
+
cookie
.
path
)
}
document
.
cookie
=
params
.
join
(
'; '
)
return
Promise
.
resolve
()
}
web/index.ts
View file @
f6954f52
...
...
@@ -3,3 +3,4 @@ export * from './api'
export
*
from
'./environment'
export
*
from
'./native-api'
export
*
from
'./token'
export
*
from
'./auth'
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment