Commit f6954f52 authored by 郑磊's avatar 郑磊

增加app授权支持

parent b61df7da
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)
}
......@@ -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()
}
......@@ -3,3 +3,4 @@ export * from './api'
export * from './environment'
export * from './native-api'
export * from './token'
export * from './auth'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment