feat: 首页查看更多
This commit is contained in:
@@ -40,3 +40,38 @@ const formatNumber = (n: number) => {
|
||||
const s = n.toString();
|
||||
return s[1] ? s : '0' + s;
|
||||
};
|
||||
|
||||
export type MiniProgramQuery = Record<string, string>;
|
||||
|
||||
export function parseMiniProgramUrl(input: string) {
|
||||
const trimmed = input.trim();
|
||||
const pathPart = trimmed.split(/[?#]/, 1)[0] || '';
|
||||
const query: MiniProgramQuery = {};
|
||||
|
||||
const questionIndex = trimmed.indexOf('?');
|
||||
const hashIndex = trimmed.indexOf('#');
|
||||
const queryStart =
|
||||
questionIndex >= 0 && (hashIndex < 0 || questionIndex < hashIndex)
|
||||
? questionIndex + 1
|
||||
: -1;
|
||||
const queryEnd = hashIndex >= 0 ? hashIndex : trimmed.length;
|
||||
|
||||
if (queryStart >= 0 && queryStart <= queryEnd) {
|
||||
const queryString = trimmed.slice(queryStart, queryEnd);
|
||||
for (const pair of queryString.split('&')) {
|
||||
if (!pair) continue;
|
||||
const [rawKey, rawValue = ''] = pair.split('=', 2);
|
||||
if (!rawKey) continue;
|
||||
const decode = (v: string) => {
|
||||
try {
|
||||
return decodeURIComponent(v.replace(/\+/g, ' '));
|
||||
} catch {
|
||||
return v;
|
||||
}
|
||||
};
|
||||
query[decode(rawKey)] = decode(rawValue);
|
||||
}
|
||||
}
|
||||
|
||||
return { path: pathPart, query };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user