All files / utils MiniProgramUrl.ts

100% Statements 19/19
71.43% Branches 10/14
100% Functions 8/8
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                      9x     9x 5x   4x         4x             10x 10x 1x   10x 10x 5x 5x           10x 10x       2x       3x       3x                     1x                     2x      
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://'
  }
}