openLocation.ts 1.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
export const API_OPEN_LOCATION = 'openLocation'
export type API_TYPE_OPEN_LOCATION = typeof uni.openLocation
fxy060608's avatar
fxy060608 已提交
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17
const checkProps = (key: string, value: unknown): string | void => {
  if (value === undefined) {
    return `${key} should not be empty.`
  }

  if (typeof value !== 'number') {
    let receivedType: string = typeof value
    receivedType = receivedType[0].toUpperCase() + receivedType.substring(1)
    return `Expected Number, got ${receivedType} with value ${JSON.stringify(
      value
    )}.`
  }
}

fxy060608's avatar
fxy060608 已提交
18
export const OpenLocationOptions: ApiOptions<API_TYPE_OPEN_LOCATION> = {
fxy060608's avatar
fxy060608 已提交
19
  formatArgs: {
20
    latitude(value, params) {
21 22 23
      const checkedInfo = checkProps('latitude', value)
      if (checkedInfo) {
        return checkedInfo
24
      }
25

26 27 28
      params.latitude = value
    },
    longitude(value, params) {
29 30 31
      const checkedInfo = checkProps('longitude', value)
      if (checkedInfo) {
        return checkedInfo
32
      }
33

34 35
      params.longitude = value
    },
fxy060608's avatar
fxy060608 已提交
36 37
    scale(value, params) {
      value = Math.floor(value!)
fxy060608's avatar
fxy060608 已提交
38
      params.scale = value >= 5 && value <= 18 ? value : 18
39 40
    },
  },
fxy060608's avatar
fxy060608 已提交
41 42
}

fxy060608's avatar
fxy060608 已提交
43
export const OpenLocationProtocol: ApiProtocol<API_TYPE_OPEN_LOCATION> = {
44 45
  latitude: Number,
  longitude: Number,
fxy060608's avatar
fxy060608 已提交
46 47 48
  scale: Number,
  name: String,
  address: String,
fxy060608's avatar
fxy060608 已提交
49
}