完成url获取用户账号功能

This commit is contained in:
2023-03-01 17:31:20 +08:00
parent 83b256a832
commit 2a226831dc
10 changed files with 4787 additions and 4701 deletions

View File

@@ -2,8 +2,8 @@
* @Author: Kane
* @Date: 2023-02-28 19:30:40
* @LastEditors: Kane
* @FilePath: /task_schedule/src/utils/api/account.ts
* @Description:
* @FilePath: /deskop_task_schedule/code/web/task_schedule/src/utils/api/url.ts
* @Description: 对URL的操作
*
* Copyright (c) ${2022} by Kane, All Rights Reserved.
*/
@@ -11,17 +11,17 @@
/**
* 将url的参数拆分成对象
* @param url 访问的url
* @returns
* @returns
*/
function getURL(url: string)
function getURLParams(url: string)
{
const arr = url.split('?');
const params = arr[1].split('&');
const arr = url.split("?");
const params = arr[1].split("&");
const obj = {};
for (let i = 0; i < params.length; i++)
{
const param = params[i].split('=');
const param = params[i].split("=");
obj[param[0]] = param[1];
}
@@ -29,4 +29,54 @@ function getURL(url: string)
return obj;
}
export { getURL };
/**
* 对URL中的参数进行拆分拆分的参数名称和值作为一个对象属性的键值对。
* @param url url字符串
* @returns 返回包含url中参数作为key值作为value的对象。
*/
function getParamsFromURL(url: string): any
{
const indexOfQuestionMark: number = url.indexOf("?");
const indexOfSharp: number = url.indexOf("#");
const paramObj = {};
let paramString;
//url中没有问号说明没有参数
if (indexOfQuestionMark < 0)
{
return paramObj;
}
//检查是否有#号
if (indexOfSharp < 0)
{
//没有#号,可以直接截取参数字符串
paramString = url.substring(indexOfQuestionMark);
}
else
{
//有#号,截取?和#之间的字符串
const end: number = indexOfQuestionMark < indexOfSharp ? indexOfSharp : url.length;
paramString = url.substring(indexOfQuestionMark + 1, end);
}
//拆分属性
const paramArray: string[] = paramString.split("&");
paramArray.forEach((item) =>
{
if (item.length == 0)
{
return;
}
const param = item.split("=");
paramObj[param[0]] = param[1] || "";
});
return paramObj;
}
export { getURLParams, getParamsFromURL };