Skip to content

Commit

Permalink
feat(utils): 新增 MiniProgramUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 7, 2022
1 parent 816c5fb commit 8907a29
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/utils/MiniProgramUrl.test.ts
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()
})
})
104 changes: 104 additions & 0 deletions src/utils/MiniProgramUrl.ts
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://'
}
}
63 changes: 63 additions & 0 deletions src/utils/__snapshots__/MiniProgramUrl.test.ts.snap
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",
}
`;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export * from './LocalStoragePlus'
export * from './loopUntil'
export * from './makeEnum'
export * from './md5'
export * from './MiniProgramUrl'
export * from './move'
export * from './omitStrict'
export * from './onceMeanwhile'
Expand Down

0 comments on commit 8907a29

Please sign in to comment.