-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { MiniProgramUrl } from './MiniProgramUrl' | ||
|
||
describe('MiniProgramUrl', () => { | ||
test('ok', () => { | ||
expect( | ||
new MiniProgramUrl( | ||
'mp://{"appId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
).toJson(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl( | ||
'mp://{"appId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
).toString(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl( | ||
'mp://{"appId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
) | ||
.update({ | ||
path: '/pages/user?id=333', | ||
}) | ||
.toJson(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl( | ||
'mp://{"appId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
).toWxOpenLaunchWeappAttrs(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl( | ||
'mp://{"rawId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
).toWxOpenLaunchWeappAttrs(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl({ | ||
rawId: 'testApp', | ||
path: '/pages/home?from=223&se=33', | ||
}).toWxOpenLaunchWeappAttrs(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl({ | ||
rawId: 'testApp', | ||
path: '/pages/home?from=223&se=33', | ||
}).toString(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl({ | ||
rawId: 'testApp', | ||
path: 'pages/home?from=223&se=33', | ||
}).toString(), | ||
).toMatchSnapshot() | ||
expect( | ||
new MiniProgramUrl({ | ||
appId: 'testApp', | ||
path: '/pages/home?from=223&se=33', | ||
}).toWxNavigateToMiniProgramParams(), | ||
).toMatchSnapshot() | ||
expect( | ||
MiniProgramUrl.is( | ||
'mp://{"rawId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
), | ||
).toBeTrue() | ||
expect( | ||
MiniProgramUrl.is( | ||
// @ts-expect-error | ||
'https://{"rawId":"testApp","path":"/pages/index","query":{"x":"1"}}', | ||
), | ||
).toBeFalse() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { assign } from 'lodash-uni' | ||
import { createUrlQueryString } from './createUrlQueryString' | ||
import { parseUrlQueryString } from './parseUrlQueryString' | ||
|
||
export type MiniProgramUrlProvider = 'wechat' | ||
|
||
export type MiniProgramUrlVersion = 'release' | 'develop' | 'trial' | ||
|
||
export type MiniProgramUrlStringInput = `mp://${string}` | ||
|
||
export type MiniProgramUrlJsonInput = { | ||
/** 提供商 */ | ||
provider?: MiniProgramUrlProvider | ||
/** APPID */ | ||
appId?: string | ||
/** 原始ID */ | ||
rawId?: string | ||
/** 版本 */ | ||
version?: MiniProgramUrlVersion | ||
/** 路径 */ | ||
path?: string | ||
/** 查询参数 */ | ||
query?: Record<string, any> | ||
/** 额外数据 */ | ||
data?: any | ||
} | ||
|
||
export type MiniProgramUrlInput = | ||
| MiniProgramUrlStringInput | ||
| MiniProgramUrlJsonInput | ||
|
||
/** | ||
* 小程序链接。 | ||
*/ | ||
export class MiniProgramUrl { | ||
private payload: MiniProgramUrlJsonInput = {} | ||
|
||
constructor(payload: MiniProgramUrlInput) { | ||
if (typeof payload === 'string') { | ||
this.update(JSON.parse(payload.substring(5))) | ||
} else { | ||
this.update(payload) | ||
} | ||
} | ||
|
||
private getPath() { | ||
return ( | ||
(this.payload.path ? this.payload.path.substring(1) : '') + | ||
(this.payload.query ? `?${createUrlQueryString(this.payload.query)}` : '') | ||
) | ||
} | ||
|
||
update(payload: Partial<MiniProgramUrlJsonInput>): this { | ||
if (payload.path) { | ||
if (payload.path[0] !== '/') { | ||
payload.path = `/${payload.path}` | ||
} | ||
const [path, query] = payload.path.split('?') | ||
if (query) { | ||
payload.path = path | ||
payload.query = { | ||
...parseUrlQueryString(query), | ||
...payload.query, | ||
} | ||
} | ||
} | ||
assign(this.payload, payload) | ||
return this | ||
} | ||
|
||
toJson(): MiniProgramUrlJsonInput { | ||
return this.payload | ||
} | ||
|
||
toString(): string { | ||
return `mp://${JSON.stringify(this.payload)}` | ||
} | ||
|
||
toWxOpenLaunchWeappAttrs() { | ||
return { | ||
username: this.payload.rawId, | ||
path: this.getPath(), | ||
envVersion: this.payload.version, | ||
extraData: this.payload.data | ||
? JSON.stringify(this.payload.data) | ||
: undefined, | ||
} | ||
} | ||
|
||
toWxNavigateToMiniProgramParams() { | ||
return { | ||
appId: this.payload.appId, | ||
path: this.getPath(), | ||
envVersion: this.payload.version, | ||
extraData: this.payload.data, | ||
} | ||
} | ||
|
||
static is( | ||
value: MiniProgramUrlStringInput, | ||
): value is MiniProgramUrlStringInput { | ||
return value.substring(0, 5) === 'mp://' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MiniProgramUrl ok 1`] = ` | ||
Object { | ||
"appId": "testApp", | ||
"path": "/pages/index", | ||
"query": Object { | ||
"x": "1", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`MiniProgramUrl ok 2`] = `"mp://{\\"appId\\":\\"testApp\\",\\"path\\":\\"/pages/index\\",\\"query\\":{\\"x\\":\\"1\\"}}"`; | ||
|
||
exports[`MiniProgramUrl ok 3`] = ` | ||
Object { | ||
"appId": "testApp", | ||
"path": "/pages/user", | ||
"query": Object { | ||
"id": "333", | ||
}, | ||
} | ||
`; | ||
|
||
exports[`MiniProgramUrl ok 4`] = ` | ||
Object { | ||
"envVersion": undefined, | ||
"extraData": undefined, | ||
"path": "pages/index?x=1", | ||
"username": undefined, | ||
} | ||
`; | ||
|
||
exports[`MiniProgramUrl ok 5`] = ` | ||
Object { | ||
"envVersion": undefined, | ||
"extraData": undefined, | ||
"path": "pages/index?x=1", | ||
"username": "testApp", | ||
} | ||
`; | ||
|
||
exports[`MiniProgramUrl ok 6`] = ` | ||
Object { | ||
"envVersion": undefined, | ||
"extraData": undefined, | ||
"path": "pages/home?from=223&se=33", | ||
"username": "testApp", | ||
} | ||
`; | ||
|
||
exports[`MiniProgramUrl ok 7`] = `"mp://{\\"rawId\\":\\"testApp\\",\\"path\\":\\"/pages/home\\",\\"query\\":{\\"from\\":\\"223\\",\\"se\\":\\"33\\"}}"`; | ||
|
||
exports[`MiniProgramUrl ok 8`] = `"mp://{\\"rawId\\":\\"testApp\\",\\"path\\":\\"/pages/home\\",\\"query\\":{\\"from\\":\\"223\\",\\"se\\":\\"33\\"}}"`; | ||
|
||
exports[`MiniProgramUrl ok 9`] = ` | ||
Object { | ||
"appId": "testApp", | ||
"envVersion": undefined, | ||
"extraData": undefined, | ||
"path": "pages/home?from=223&se=33", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters