103 lines
3.1 KiB
TypeScript
103 lines
3.1 KiB
TypeScript
// 导出HTTP模块
|
|
export * from './http';
|
|
|
|
// 导出SVG汉字数据获取模块
|
|
export * from './getWordsSvgJson';
|
|
|
|
export async function getMiniCodeImage(canvas: Canvas) {
|
|
return getImage(canvas, '/assets/imgs/doodle-mini-logo.jpg');
|
|
}
|
|
|
|
export async function getImage(canvas: Canvas, path: string) {
|
|
return new Promise((resolve, reject) => {
|
|
const image = canvas.createImage();
|
|
image.onload = () => {
|
|
resolve(image);
|
|
};
|
|
image.onerror = () => {
|
|
reject('图片加载异常');
|
|
};
|
|
image.src = path;
|
|
});
|
|
}
|
|
|
|
export const formatTime = (date: Date) => {
|
|
const year = date.getFullYear();
|
|
const month = date.getMonth() + 1;
|
|
const day = date.getDate();
|
|
const hour = date.getHours();
|
|
const minute = date.getMinutes();
|
|
const second = date.getSeconds();
|
|
|
|
return (
|
|
[year, month, day].map(formatNumber).join('/') +
|
|
' ' +
|
|
[hour, minute, second].map(formatNumber).join(':')
|
|
);
|
|
};
|
|
|
|
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 };
|
|
}
|
|
|
|
/** tabBar 无法用 switchTab 带 query:用 globalData 传一次性意图,避免 storage 磁盘同步读写 */
|
|
export function stashPendingCategoryTabId(id: string) {
|
|
const trimmed = String(id ?? '').trim();
|
|
if (!trimmed) return;
|
|
try {
|
|
getApp<IAppOption>().globalData.pendingCategoryTabId = trimmed;
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
}
|
|
|
|
/** 若无待处理 id 则返回 null;返回后清空,避免残留在全局 */
|
|
export function takePendingCategoryTabId(): string | null {
|
|
try {
|
|
const app = getApp<IAppOption>();
|
|
const raw = app.globalData.pendingCategoryTabId;
|
|
if (raw == null || String(raw).trim() === '') return null;
|
|
const v = String(raw).trim();
|
|
delete app.globalData.pendingCategoryTabId;
|
|
return v;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|