使用 oak-domain的URL
This commit is contained in:
parent
236ad330f7
commit
0d60a47f49
|
|
@ -1,3 +1,4 @@
|
|||
/// <reference types="node" />
|
||||
import { Feature } from '../types/Feature';
|
||||
export declare class Navigator extends Feature {
|
||||
namespace: string;
|
||||
|
|
@ -5,7 +6,7 @@ export declare class Navigator extends Feature {
|
|||
constructor();
|
||||
setNamespace(namespace: string): void;
|
||||
getNamespace(): string;
|
||||
urlParse(path: string): URL;
|
||||
urlParse(path: string): import("url").URL;
|
||||
urlFormat(url: URL): string;
|
||||
constructSearch(search?: string | null, state?: Record<string, any>): string;
|
||||
constructUrl(url: string, state?: Record<string, any>, disableNamespace?: boolean): string;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { Feature } from '../types/Feature';
|
||||
import { url as URL, urlSearchParams as URLSearchParams, } from 'oak-domain/lib/utils/url';
|
||||
export class Navigator extends Feature {
|
||||
namespace;
|
||||
base;
|
||||
|
|
@ -19,7 +20,7 @@ export class Navigator extends Feature {
|
|||
return urlParse;
|
||||
}
|
||||
urlFormat(url) {
|
||||
const urlParse = new URL(url, this.base);
|
||||
const urlParse = new URL(url.toString(), this.base);
|
||||
const url2 = urlParse.toString();
|
||||
return url2.replace(this.base, '');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,18 @@
|
|||
/// <reference types="wechat-miniprogram" />
|
||||
import { Feature } from '../types/Feature';
|
||||
import { OakNavigateToParameters } from '../types/Page';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { EntityDict } from 'oak-domain/lib/types';
|
||||
import { Navigator as CommonNavigator } from './navigator.common';
|
||||
type Location = {
|
||||
pathname: string;
|
||||
state: unknown;
|
||||
state?: Record<string, any>;
|
||||
key: string;
|
||||
};
|
||||
export declare class Navigator extends Feature {
|
||||
namespace: string;
|
||||
export declare class Navigator extends CommonNavigator {
|
||||
history: WechatMiniprogram.Wx;
|
||||
constructor();
|
||||
setNamespace(namespace: string): void;
|
||||
getLocation(): Location;
|
||||
getNamespace(): string;
|
||||
getCurrentUrl(needParams?: boolean): string;
|
||||
private constructSearch;
|
||||
private constructUrl;
|
||||
private constructNamespace;
|
||||
getPathname(pathname: string, namespace?: string): string;
|
||||
private getUrlAndProps;
|
||||
navigateTo<ED extends EntityDict & BaseEntityDict, T2 extends keyof ED>(options: {
|
||||
|
|
|
|||
|
|
@ -1,17 +1,10 @@
|
|||
import { assert } from 'oak-domain/lib/utils/assert';
|
||||
import URL from 'url';
|
||||
import { Feature } from '../types/Feature';
|
||||
export class Navigator extends Feature {
|
||||
namespace;
|
||||
import { Navigator as CommonNavigator } from './navigator.common';
|
||||
export class Navigator extends CommonNavigator {
|
||||
history;
|
||||
constructor() {
|
||||
super();
|
||||
this.history = wx;
|
||||
this.namespace = '';
|
||||
}
|
||||
setNamespace(namespace) {
|
||||
this.namespace = namespace;
|
||||
this.publish();
|
||||
}
|
||||
getLocation() {
|
||||
const pages = getCurrentPages(); //获取加载的页面
|
||||
|
|
@ -28,80 +21,18 @@ export class Navigator extends Feature {
|
|||
key: `${pages.length - 1}`,
|
||||
};
|
||||
}
|
||||
getNamespace() {
|
||||
return this.namespace;
|
||||
}
|
||||
getCurrentUrl(needParams) {
|
||||
const pages = getCurrentPages(); //获取加载的页面
|
||||
const currentPage = pages[pages.length - 1]; //获取当前页面的对象
|
||||
const url = currentPage.route; //当前页面url
|
||||
const options = currentPage.options; //如果要获取url中所带的参数可以查看options
|
||||
const { pathname, state } = this.getLocation();
|
||||
if (!needParams) {
|
||||
return url;
|
||||
return pathname;
|
||||
}
|
||||
// 构建search
|
||||
const search2 = this.constructSearch('', options);
|
||||
const url2 = URL.format({
|
||||
pathname: url,
|
||||
search: search2,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
constructSearch(search, state) {
|
||||
let search2 = search;
|
||||
if (state) {
|
||||
for (const param in state) {
|
||||
if (!search2) {
|
||||
search2 = '?';
|
||||
}
|
||||
if (state[param] !== undefined ||
|
||||
state[param] !== 'undefined') {
|
||||
search2 += `&${param}=${typeof state[param] === 'string'
|
||||
? state[param]
|
||||
: JSON.stringify(state[param])}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
return search2;
|
||||
}
|
||||
constructUrl(url, state, disableNamespace) {
|
||||
const urlParse = URL.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2;
|
||||
if (disableNamespace) {
|
||||
pathname2 = this.getPathname(pathname);
|
||||
}
|
||||
else {
|
||||
pathname2 = this.getPathname(pathname, this.namespace);
|
||||
}
|
||||
// 构建search
|
||||
const search2 = this.constructSearch(search, state);
|
||||
const url2 = URL.format({
|
||||
pathname: pathname2,
|
||||
search: search2,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
constructNamespace(url, namespace) {
|
||||
if (namespace) {
|
||||
const urlParse = URL.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2 = pathname;
|
||||
if (namespace === '/') {
|
||||
pathname2 = pathname;
|
||||
}
|
||||
else if (pathname === namespace) {
|
||||
pathname2 = pathname;
|
||||
}
|
||||
else {
|
||||
pathname2 = namespace + pathname;
|
||||
}
|
||||
const url2 = URL.format({
|
||||
pathname: pathname2,
|
||||
search,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
const search2 = this.constructSearch('', state);
|
||||
const urlParse = this.urlParse(pathname);
|
||||
urlParse.pathname = pathname;
|
||||
urlParse.search = search2;
|
||||
urlParse.searchParams.delete('oakFrom'); //把上层传入的oakFrom排除
|
||||
const url = this.urlFormat(urlParse);
|
||||
return url;
|
||||
}
|
||||
getPathname(pathname, namespace) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ export declare class Navigator extends CommonNavigator {
|
|||
setHistory(history: BrowserHistory): void;
|
||||
getLocation(): import("history").Location;
|
||||
getCurrentUrl(needParams?: boolean): string;
|
||||
getPathname(pathname: string, namespace?: string): string;
|
||||
private getUrlAndProps;
|
||||
navigateTo<ED extends EntityDict & BaseEntityDict, T2 extends keyof ED>(options: {
|
||||
url: string;
|
||||
|
|
|
|||
|
|
@ -30,14 +30,6 @@ export class Navigator extends CommonNavigator {
|
|||
const url = this.urlFormat(urlParse);
|
||||
return url;
|
||||
}
|
||||
getPathname(pathname, namespace) {
|
||||
let pathname2 = pathname;
|
||||
if (namespace) {
|
||||
pathname2 = this.constructNamespace(pathname, namespace);
|
||||
return pathname2;
|
||||
}
|
||||
return pathname2;
|
||||
}
|
||||
getUrlAndProps(options, state, disableNamespace) {
|
||||
const { url, ...rest } = options;
|
||||
const url2 = this.constructUrl(url, rest, disableNamespace);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
/// <reference types="node" />
|
||||
import { Feature } from '../types/Feature';
|
||||
export declare class Navigator extends Feature {
|
||||
namespace: string;
|
||||
|
|
@ -5,7 +6,7 @@ export declare class Navigator extends Feature {
|
|||
constructor();
|
||||
setNamespace(namespace: string): void;
|
||||
getNamespace(): string;
|
||||
urlParse(path: string): URL;
|
||||
urlParse(path: string): import("url").URL;
|
||||
urlFormat(url: URL): string;
|
||||
constructSearch(search?: string | null, state?: Record<string, any>): string;
|
||||
constructUrl(url: string, state?: Record<string, any>, disableNamespace?: boolean): string;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Navigator = void 0;
|
||||
const Feature_1 = require("../types/Feature");
|
||||
const url_1 = require("oak-domain/lib/utils/url");
|
||||
class Navigator extends Feature_1.Feature {
|
||||
namespace;
|
||||
base;
|
||||
|
|
@ -18,16 +19,16 @@ class Navigator extends Feature_1.Feature {
|
|||
return this.namespace;
|
||||
}
|
||||
urlParse(path) {
|
||||
const urlParse = new URL(path, this.base);
|
||||
const urlParse = new url_1.url(path, this.base);
|
||||
return urlParse;
|
||||
}
|
||||
urlFormat(url) {
|
||||
const urlParse = new URL(url, this.base);
|
||||
const urlParse = new url_1.url(url.toString(), this.base);
|
||||
const url2 = urlParse.toString();
|
||||
return url2.replace(this.base, '');
|
||||
}
|
||||
constructSearch(search, state) {
|
||||
const searchParams = new URLSearchParams(search || '');
|
||||
const searchParams = new url_1.urlSearchParams(search || '');
|
||||
if (state) {
|
||||
for (const param in state) {
|
||||
if (state[param] !== undefined ||
|
||||
|
|
|
|||
|
|
@ -1,24 +1,18 @@
|
|||
/// <reference types="wechat-miniprogram" />
|
||||
import { Feature } from '../types/Feature';
|
||||
import { OakNavigateToParameters } from '../types/Page';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { EntityDict } from 'oak-domain/lib/types';
|
||||
import { Navigator as CommonNavigator } from './navigator.common';
|
||||
type Location = {
|
||||
pathname: string;
|
||||
state: unknown;
|
||||
state?: Record<string, any>;
|
||||
key: string;
|
||||
};
|
||||
export declare class Navigator extends Feature {
|
||||
namespace: string;
|
||||
export declare class Navigator extends CommonNavigator {
|
||||
history: WechatMiniprogram.Wx;
|
||||
constructor();
|
||||
setNamespace(namespace: string): void;
|
||||
getLocation(): Location;
|
||||
getNamespace(): string;
|
||||
getCurrentUrl(needParams?: boolean): string;
|
||||
private constructSearch;
|
||||
private constructUrl;
|
||||
private constructNamespace;
|
||||
getPathname(pathname: string, namespace?: string): string;
|
||||
private getUrlAndProps;
|
||||
navigateTo<ED extends EntityDict & BaseEntityDict, T2 extends keyof ED>(options: {
|
||||
|
|
|
|||
|
|
@ -1,21 +1,13 @@
|
|||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.Navigator = void 0;
|
||||
const tslib_1 = require("tslib");
|
||||
const assert_1 = require("oak-domain/lib/utils/assert");
|
||||
const url_1 = tslib_1.__importDefault(require("url"));
|
||||
const Feature_1 = require("../types/Feature");
|
||||
class Navigator extends Feature_1.Feature {
|
||||
namespace;
|
||||
const navigator_common_1 = require("./navigator.common");
|
||||
class Navigator extends navigator_common_1.Navigator {
|
||||
history;
|
||||
constructor() {
|
||||
super();
|
||||
this.history = wx;
|
||||
this.namespace = '';
|
||||
}
|
||||
setNamespace(namespace) {
|
||||
this.namespace = namespace;
|
||||
this.publish();
|
||||
}
|
||||
getLocation() {
|
||||
const pages = getCurrentPages(); //获取加载的页面
|
||||
|
|
@ -32,80 +24,18 @@ class Navigator extends Feature_1.Feature {
|
|||
key: `${pages.length - 1}`,
|
||||
};
|
||||
}
|
||||
getNamespace() {
|
||||
return this.namespace;
|
||||
}
|
||||
getCurrentUrl(needParams) {
|
||||
const pages = getCurrentPages(); //获取加载的页面
|
||||
const currentPage = pages[pages.length - 1]; //获取当前页面的对象
|
||||
const url = currentPage.route; //当前页面url
|
||||
const options = currentPage.options; //如果要获取url中所带的参数可以查看options
|
||||
const { pathname, state } = this.getLocation();
|
||||
if (!needParams) {
|
||||
return url;
|
||||
return pathname;
|
||||
}
|
||||
// 构建search
|
||||
const search2 = this.constructSearch('', options);
|
||||
const url2 = url_1.default.format({
|
||||
pathname: url,
|
||||
search: search2,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
constructSearch(search, state) {
|
||||
let search2 = search;
|
||||
if (state) {
|
||||
for (const param in state) {
|
||||
if (!search2) {
|
||||
search2 = '?';
|
||||
}
|
||||
if (state[param] !== undefined ||
|
||||
state[param] !== 'undefined') {
|
||||
search2 += `&${param}=${typeof state[param] === 'string'
|
||||
? state[param]
|
||||
: JSON.stringify(state[param])}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
return search2;
|
||||
}
|
||||
constructUrl(url, state, disableNamespace) {
|
||||
const urlParse = url_1.default.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2;
|
||||
if (disableNamespace) {
|
||||
pathname2 = this.getPathname(pathname);
|
||||
}
|
||||
else {
|
||||
pathname2 = this.getPathname(pathname, this.namespace);
|
||||
}
|
||||
// 构建search
|
||||
const search2 = this.constructSearch(search, state);
|
||||
const url2 = url_1.default.format({
|
||||
pathname: pathname2,
|
||||
search: search2,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
constructNamespace(url, namespace) {
|
||||
if (namespace) {
|
||||
const urlParse = url_1.default.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2 = pathname;
|
||||
if (namespace === '/') {
|
||||
pathname2 = pathname;
|
||||
}
|
||||
else if (pathname === namespace) {
|
||||
pathname2 = pathname;
|
||||
}
|
||||
else {
|
||||
pathname2 = namespace + pathname;
|
||||
}
|
||||
const url2 = url_1.default.format({
|
||||
pathname: pathname2,
|
||||
search,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
const search2 = this.constructSearch('', state);
|
||||
const urlParse = this.urlParse(pathname);
|
||||
urlParse.pathname = pathname;
|
||||
urlParse.search = search2;
|
||||
urlParse.searchParams.delete('oakFrom'); //把上层传入的oakFrom排除
|
||||
const url = this.urlFormat(urlParse);
|
||||
return url;
|
||||
}
|
||||
getPathname(pathname, namespace) {
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ export declare class Navigator extends CommonNavigator {
|
|||
setHistory(history: BrowserHistory): void;
|
||||
getLocation(): import("history").Location;
|
||||
getCurrentUrl(needParams?: boolean): string;
|
||||
getPathname(pathname: string, namespace?: string): string;
|
||||
private getUrlAndProps;
|
||||
navigateTo<ED extends EntityDict & BaseEntityDict, T2 extends keyof ED>(options: {
|
||||
url: string;
|
||||
|
|
|
|||
|
|
@ -33,14 +33,6 @@ class Navigator extends navigator_common_1.Navigator {
|
|||
const url = this.urlFormat(urlParse);
|
||||
return url;
|
||||
}
|
||||
getPathname(pathname, namespace) {
|
||||
let pathname2 = pathname;
|
||||
if (namespace) {
|
||||
pathname2 = this.constructNamespace(pathname, namespace);
|
||||
return pathname2;
|
||||
}
|
||||
return pathname2;
|
||||
}
|
||||
getUrlAndProps(options, state, disableNamespace) {
|
||||
const { url, ...rest } = options;
|
||||
const url2 = this.constructUrl(url, rest, disableNamespace);
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@
|
|||
"react-responsive": "^9.0.2",
|
||||
"rmc-pull-to-refresh": "^1.0.13",
|
||||
"socket.io-client": "^4.7.2",
|
||||
"url": "^0.11.0",
|
||||
"uuid": "^8.3.2",
|
||||
"weapp.socket.io": "^2.2.1"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@ import { Feature } from '../types/Feature';
|
|||
import { OakNavigateToParameters } from '../types/Page';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { EntityDict } from 'oak-domain/lib/types';
|
||||
import {
|
||||
url as URL,
|
||||
urlSearchParams as URLSearchParams,
|
||||
} from 'oak-domain/lib/utils/url';
|
||||
|
||||
export class Navigator extends Feature {
|
||||
namespace: string;
|
||||
|
|
@ -29,7 +33,7 @@ export class Navigator extends Feature {
|
|||
}
|
||||
|
||||
urlFormat(url: URL) {
|
||||
const urlParse = new URL(url, this.base);
|
||||
const urlParse = new URL(url.toString(), this.base);
|
||||
const url2 = urlParse.toString();
|
||||
return url2.replace(this.base, '');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,22 @@
|
|||
import { assert } from 'oak-domain/lib/utils/assert';
|
||||
import URL from 'url';
|
||||
import { Feature } from '../types/Feature';
|
||||
import { OakNavigateToParameters } from '../types/Page';
|
||||
import { EntityDict as BaseEntityDict } from 'oak-domain/lib/base-app-domain';
|
||||
import { EntityDict } from 'oak-domain/lib/types';
|
||||
import { Navigator as CommonNavigator } from './navigator.common';
|
||||
|
||||
type Location = {
|
||||
pathname: string;
|
||||
state: unknown;
|
||||
state?: Record<string, any>;
|
||||
key: string;
|
||||
};
|
||||
|
||||
export class Navigator extends Feature {
|
||||
namespace: string;
|
||||
export class Navigator extends CommonNavigator {
|
||||
history: WechatMiniprogram.Wx;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.history = wx;
|
||||
this.namespace = '';
|
||||
}
|
||||
|
||||
setNamespace(namespace: string) {
|
||||
this.namespace = namespace;
|
||||
this.publish();
|
||||
}
|
||||
|
||||
getLocation(): Location {
|
||||
|
|
@ -42,96 +35,19 @@ export class Navigator extends Feature {
|
|||
};
|
||||
}
|
||||
|
||||
getNamespace() {
|
||||
return this.namespace;
|
||||
}
|
||||
|
||||
getCurrentUrl(needParams?: boolean) {
|
||||
const pages = getCurrentPages(); //获取加载的页面
|
||||
const currentPage = pages[pages.length - 1]; //获取当前页面的对象
|
||||
const url = currentPage.route; //当前页面url
|
||||
const options = currentPage.options; //如果要获取url中所带的参数可以查看options
|
||||
const { pathname, state } = this.getLocation();
|
||||
|
||||
if (!needParams) {
|
||||
return url;
|
||||
return pathname;
|
||||
}
|
||||
// 构建search
|
||||
const search2 = this.constructSearch('', options);
|
||||
const url2 = URL.format({
|
||||
pathname: url,
|
||||
search: search2,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
|
||||
private constructSearch(
|
||||
search?: string | null,
|
||||
state?: Record<string, any>
|
||||
) {
|
||||
let search2 = search;
|
||||
if (state) {
|
||||
for (const param in state) {
|
||||
if (!search2) {
|
||||
search2 = '?';
|
||||
}
|
||||
if (
|
||||
state[param] !== undefined ||
|
||||
state[param] !== 'undefined'
|
||||
) {
|
||||
search2 += `&${param}=${
|
||||
typeof state[param] === 'string'
|
||||
? state[param]
|
||||
: JSON.stringify(state[param])
|
||||
}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
return search2;
|
||||
}
|
||||
|
||||
private constructUrl(
|
||||
url: string,
|
||||
state?: Record<string, any>,
|
||||
disableNamespace?: boolean
|
||||
) {
|
||||
const urlParse = URL.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2: string;
|
||||
if (disableNamespace) {
|
||||
pathname2 = this.getPathname(pathname!);
|
||||
} else {
|
||||
pathname2 = this.getPathname(pathname!, this.namespace);
|
||||
}
|
||||
|
||||
// 构建search
|
||||
const search2 = this.constructSearch(search, state);
|
||||
const url2 = URL.format({
|
||||
pathname: pathname2,
|
||||
search: search2,
|
||||
});
|
||||
|
||||
return url2;
|
||||
}
|
||||
|
||||
private constructNamespace(url: string, namespace?: string) {
|
||||
if (namespace) {
|
||||
const urlParse = URL.parse(url, true);
|
||||
const { pathname, search } = urlParse;
|
||||
let pathname2 = pathname;
|
||||
if (namespace === '/') {
|
||||
pathname2 = pathname;
|
||||
} else if (pathname === namespace) {
|
||||
pathname2 = pathname;
|
||||
} else {
|
||||
pathname2 = namespace + pathname;
|
||||
}
|
||||
|
||||
const url2 = URL.format({
|
||||
pathname: pathname2,
|
||||
search,
|
||||
});
|
||||
return url2;
|
||||
}
|
||||
const search2 = this.constructSearch('', state);
|
||||
const urlParse = this.urlParse(pathname);
|
||||
urlParse.pathname = pathname;
|
||||
urlParse.search = search2;
|
||||
urlParse.searchParams.delete('oakFrom'); //把上层传入的oakFrom排除
|
||||
const url = this.urlFormat(urlParse);
|
||||
return url;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,15 +45,6 @@ export class Navigator extends CommonNavigator {
|
|||
return url;
|
||||
}
|
||||
|
||||
getPathname(pathname: string, namespace?: string) {
|
||||
let pathname2 = pathname;
|
||||
if (namespace) {
|
||||
pathname2 = this.constructNamespace(pathname, namespace);
|
||||
return pathname2;
|
||||
}
|
||||
return pathname2;
|
||||
}
|
||||
|
||||
private getUrlAndProps<
|
||||
ED extends EntityDict & BaseEntityDict,
|
||||
T2 extends keyof ED
|
||||
|
|
|
|||
Loading…
Reference in New Issue